Reputation: 9232
I am trying to create two simple Tables with a couple of records in a Postgresql database running a simple Java application and using Hibernate. I have created the entities Employee
and Department
as described in http://viralpatel.net/blogs/hibernate-one-to-many-annotation-tutorial/ . I do not use Maven. My main method lying in the same package as the two Entities looks like:
@SuppressWarnings("deprecation")
public static void main(String[] args) {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Department department = new Department();
department.setDepartmentName("Sales");
session.save(department);
Employee emp1 = new Employee("Nina", "Mayers", "111");
Employee emp2 = new Employee("Tony", "Almeida", "222");
emp1.setDepartment(department);
emp2.setDepartment(department);
session.save(emp1);
session.save(emp2);
session.getTransaction().commit();
session.close();
}
My hibernate.cfg.xml file is:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/testDB</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2dll.auto">create-drop</property>
</session-factory>
After running the application, the following error comes up:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.al.hibernatetest.Department
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1063)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1433)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:116)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:205)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:190)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:91)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:764)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:756)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:752)
at com.al.hibernatetest.Main.main(Main.java:24)
Due to unknown to me reason, the hibernate cannot recognize the entities? What could be the problem? Anything in the configuration? A missing jar? Anything in the creation of the session?
Upvotes: 1
Views: 1540
Reputation: 76
<mapping class="net.viralpatel.hibernate.Department"/>
<mapping class="net.viralpatel.hibernate.Employee"
You forgot to copy these two lines in hibernate.cfg.xml.
Upvotes: 1