I have successfully created a simple test project using hibernate and am now in the process of learning hibernate annotations. When I set the precedence of the AnnotationConfiguration to the default "hbm", I don't get any errors. However, when I change it to "class" so that it will use my code annotations, I get an unknown entity error in my Person class.
Hibernate version:
hibernate v3.1.1
hibernate-annotations 3.1 beta8
Mapping documents:
This is my hbm.xml for my Person class
Code:
<class name="contacts.Person" table="PERSONS">
<id name="id" column="PERSON_ID">
<generator class="native"/>
</id>
<property name="birthDay" type="date" column="BIRTHDAY"/>
<property name="firstName"/>
<property name="lastName"/>
my hibernate.cfg.xml
Code:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping class="contacts.Person"/>
<mapping resource="contacts/Person.hbm.xml"/>
</session-factory>
my HibernateUtil
Code:
public class HibernateUtil
{
private static final SessionFactory sessionFactory;
static
{
try
{
AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.configure("hibernate.cfg.xml");
cfg.setPrecedence("class");
sessionFactory = cfg.buildSessionFactory();
}
catch (Throwable ex)
{
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession()
throws HibernateException
{
return sessionFactory.openSession();
}
}
When I set the precedence to "hbm", it works fine. But when I set it to "class", I get the error
Full stack trace of any exception that occurs:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: ingeni.training.contacts.Person
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:513)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1319)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:89)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:557)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:545)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:541)
at ingeni.training.manager.PersonManager.addPerson(PersonManager.java:32)
at ingeni.training.manager.PersonManager.main(PersonManager.java:16)
Can you guys tell me what I did wrong in the way I used hibernate annotations?