0

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.

0

2 Answers 2

2

Is XML mapping still required when using annotation mapping?

Absolutely not.

The two are mutually exclusive for a given entity.


Without going into detail about the particular application server you're running in, and consequently the various different ways to configure Hibernate, all you should need is persistence.xml and the annotations.

Since the persistence annotations aren't working for you, it's up to you to figure out why.

You might find the Java EE 7 tutorial more up-to-date than whatever book you're using.

Sign up to request clarification or add additional context in comments.

Comments

0

The XML mapping is not needed for annotation based mapping, the whole idea of the annotations was to get rid of the cumbersome XML.

It's been a while since I've used pure Hibernate (instead of using it through JPA), but it should require very little, if any XML configuration, since you can configure the session factories through annotations as well.

Now this is an incomplete answer, since I'm thinking of this mainly through JPA, but since Hibernate implements JPA, it should hold.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.