Not able to get the Hibernate working using hbm file

I am using Hibernate 4.3.11. Created a simple prototype with 2 tables Book and Author and 2 entities mapped to these tables.
When I used annotations, the things work fine.
When I remove annotations and start using hbm.xml files for the entities, I get below exception:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.avaya.test.hibernate.Book
	at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1096)
	at org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2601)
	at org.hibernate.internal.SessionImpl.access$1900(SessionImpl.java:176)
	at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2540)
	at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2544)
	at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2531)
	at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1079)
	at org.hibernate.internal.SessionImpl.get(SessionImpl.java:991)
	at com.avaya.test.hibernate.BookManager.read(BookManager.java:78)
	at com.avaya.test.hibernate.BookManager.main(BookManager.java:130)

Couple of questions:

  1. Does it requires a different SessionFactory or is there an issue with the way I instantiate SessionFactory?
  2. Does annotations and hbm files work simultaneously e.g. I also want to use L2 cache which I would want to use through annotations and rest of the ORM will be picked up from hbm.xml file?
  1. Does it requires a different SessionFactory or is there an issue with the way I instantiate SessionFactory?

It’s an issue with the mapping file. Most likely Hibernate cannot locate the file.

  1. Does annotations and hbm files work simultaneously e.g. I also want to use L2 cache which I would want to use through annotations and rest of the ORM will be picked up from hbm.xml file?

Yes, they can be combined and HBM will override annotations. You can control that with the scan options property. For more details, check out this article.

Thanks vlad…

It’s an issue with the mapping file. Most likely Hibernate cannot locate the file.

–> can you elaborate on why the hbm file may not be located. I have put it in the classpath in the same package where the java entity class resides. I also tried to put it in the same folder where hibernate configuration files resides.

Yes, they can be combined and HBM will override annotations. You can control that with the scan options property.
–> I don’t want to override mapping defined in hbm using annotations. I want to add annotations for a different purpose to make an entity cacheable through hibernate l2 cache.
I hope annotations and hbm files would complement each other in this case?

can you elaborate on why the hbm file may not be located. I have put it in the classpath in the same package where the java entity class resides. I also tried to put it in the same folder where hibernate configuration files resides.

Just debug it and see why the hbm file was not located. The beauty of OSS is that you have the source classes so you can easily debug to figure out why something is not working.

I want to add annotations for a different purpose to make an entity cacheable through hibernate l2 cache.

I don’t really understand what you want to do. Usually, you either use one or the other. You can also mix them, which would probably make sense when your entities are annotated for most production envs, but on a specific environment you want to override soemthing. That’s bets done with XML-based configs.

If you have different configs, you can use different XML mappings. Just make sure you provide the right ones at build time.

The reason I want to add annotation is very simple. I have moved recently to a project that uses hibernate and it has used hbm files for entity mapping. The project doesn’t leverage the hibernate l2 cache which I want to introduce now for which I need to add annotations to the entity to make it cacheable. Since the project already uses hbm files for entity mapping, I don’t want to touch that but just add new annotations for making entities cacheable.

Hibernate does not combine annotations and XML. It can use either one or the other. So, if you define a @Column annotation, that will override the XML property configuration.

There’s no merging going on. Nevertheless, you can also define the cache element:

<hibernate-mapping>
	<class name="..." table="...">
	<cache usage="read-write" />
	<id name="id" column="id" type="java.lang.Integer" >
		<generator class="assigned" />
	</id>
	<property name="title" type="java.lang.String" column="title"/>
	</class> 
</hibernate-mapping>

Why do you think that mixing HBM and annotations is going to be much easier than just adding the cache element to the already existing HBM files?

Okay… I tried using the cache inside hbm file itself and that seems to be working. I don’t think I will need annotations now. Thanks.

As an aside, can you also guide me on how relationship will be loaded by hibernate. I am trying a simple example of Book and Author where Book entity has a reference to Author entity using following property in hbm:

  <many-to-one name = "author" column = "author_id" class="com.avaya.test.hibernate.Author" not-null="true"/>

However, calling getAuthor on the Book entity returns me a null.

In order to understand how Hibernate works, it’s best to activate the JDBC log and observe the executed statements.

Also, if you are using bidirectional associations, make sure you synchronize both ends of the association.

To make sure you won’t stumble into various usability issues, it’s mandatory to read the Hibernate User Guide entirely.