I'm trying to learn Hibernate with a book that states:
Hibernate provides features, such as lazy loading, cascading, caching, and others, that must be configured. This information is represented as object/relational mapping metadata.
Hibernate supports the following three different strategies to describe this metadata:
- Setting up XML mapping files
- Annotating persistent classes with Hibernate XDoclet tags
- Annotating persistent classes with Hibernate annotations
So I figured each of the three methods are interdependent. I am trying t use the third method with annotation. I have a following class
@Entity
@Table(name = "SOME_TABLE")
public class SomeClass implements Serializeable {
/// ...
}
I try to run it without any mapping in the hibernate.cfg.xml, and I get this exception
org.hibernate.MappingException: Unknown entity:com...SomeClass
So I figured there need to be some mapping in the hibernate.cfg.xml file. So I tried to create an empty mapping file SomeClass.hbm.xml
<hibernate-mapping>
<class name="somepackage.SomeClass" table="SOME_TABLE">
</class>
</hibernate-mapping>
And map it in the hibernate.gfg.xml
<hibernate-configuration>
<session-factory>
... propert tags
<mapping resource="somepackage/SomeClass.hbm.xml"/>
</session-factory>
</hibernate-configuration>
But then I get a SaxParseException
SAXParseException ... The content of element type "class" is incomplete
which makes complete sense, because the contents are incomplete.
But when I fill out all the necessary properties, it all works fine, table is updated.
<hibernate-mapping>
<class name="com.underdogdevs.hibernatepractice.StudentEntity" table="STUDENT">
.. id and property elements all in place.
</class>
</hibernate-mapping>
From the statement above from the book, I figure I could do one or the other, either use the Annotation metadata mapping or the XML metdadata mappimg. It works fine just using the XML mapping without the annotation mapping, so why not the other way arround? Am I not mapping the Entity class correctly in the hibernate.cfg.xml file by using the SomeClass.hbm.xml file? if so, what is the correct way to may a class using annotation mapping? The book doesn't really explain this. It goes off into a new topic before ever doing so.