XML : Hibernate Unknown Entity exception when saving object to database

I've been trying to work with hibernate to read and write to a mysql database, but I keep getting a Unknown entity error when I attempt session.save() to save a Dept object to the dept mysql table. I've been looking for days for a solution and I have tried many different things, including annotating my code, changing the mapping xml file, the hibernate config xml file, etc. Most of the other people getting this error are using spring but I am not, so a lot of the other answers arent applicable. I feel like theres something small that I'm missing.

Launcher Class:

  final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()                  .configure() // configures settings from hibernate.cfg.xml                  .build();          try {              SessionFactory sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();                System.out.println("sessionfactory made, now creating session");              Session session = sessionFactory.openSession();              System.out.println("made session");              session.beginTransaction();              System.out.println("making first dept");              session.save( new Dept(1, "Math",  "CORE"));              session.save( new Dept(2, "Science", "CORE"));              System.out.println("commiting transaction");              session.getTransaction().commit();              session.close();              System.out.println("SUCCESS");          }          catch (Exception e) {              // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory              // so destroy it manually.              e.printStackTrace();              StandardServiceRegistryBuilder.destroy( registry );              System.out.println("exception, destroyed registry");          }    

Dept.java:

  package org.myschool.timeline.Timeline_App.domain;  // Generated Jan 16, 2016 1:48:30 AM by Hibernate Tools 4.3.1.Final    import java.util.HashSet;  import java.util.Set;    /**   * Dept generated by hbm2java   */  public class Dept implements java.io.Serializable {        private int deptId;      private String name;      private String type;      private Set<Course> courses = new HashSet<Course>(0);        public Dept() {      }        public Dept(int deptId, String name, String type) {          this.deptId = deptId;          this.name = name;          this.type = type;      }        public Dept(int deptId, String name, String type, Set<Course> courses) {          this.deptId = deptId;          this.name = name;          this.type = type;          this.courses = courses;      }      //Getters and Setters cut for brevity  }    

hibernate.cfg.xml:

  <?xml version="1.0" encoding="utf-8"?>  <!DOCTYPE hibernate-configuration PUBLIC  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  <hibernate-configuration>      <session-factory>          <property name="hibernate.bytecode.use_reflection_optimizer">false</property>          <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>          <property name="hibernate.connection.password">pass</property>          <property name="hibernate.connection.url">jdbc:mysql://serverip</property>          <property name="hibernate.connection.username">user</property>          <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>          <property name="hibernate.search.autoregister_listeners">false</property>          <property name="hibernate.validator.apply_to_ddl">false</property>          <property name="current_session_context_class">thread</property>          <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>          <mapping resource="org/myschool/timeline/Timeline_App/domain/Admin.hbm.xml" />          <mapping resource="org/myschool/timeline/Timeline_App/domain/Course.hbm.xml" />          <mapping resource="org/myschool/timeline/Timeline_App/domain/Teacher.hbm.xml" />          <mapping resource="org/myschool/timeline/Timeline_App/domain/Dept.hbm.xml" />          <mapping resource="org/myschool/timeline/Timeline_App/domain/Student.hbm.xml" />      </session-factory>  </hibernate-configuration>    

And Dept.hbm.xml:

  <?xml version="1.0"?>  <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  <!-- Generated Jan 16, 2016 1:52:21 AM by Hibernate Tools 3.5.0.Final -->  <hibernate-mapping>      <class name="org.myschool.timeline.Timeline_App.domain.Dept" table="DEPT">          <id name="deptId" type="int">              <column name="DEPTID" />              <generator class="assigned" />          </id>          <property name="name" type="java.lang.String">              <column name="NAME" />          </property>          <property name="type" type="java.lang.String">              <column name="TYPE" />          </property>          <set name="courses" table="COURSE" inverse="false" lazy="true">              <key>                  <column name="DEPTID" />              </key>              <one-to-many class="org.myschool.timeline.Timeline_App.domain.Course" />          </set>      </class>  </hibernate-mapping>    

This is the error I get when i launch:

  making first dept  org.hibernate.MappingException: Unknown entity: org.myschool.timeline.Timeline_App.domain.Dept      at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1096)      at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1479)      at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)      at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)      at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)      at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)      at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)      at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)      at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)      at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)      at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)      at org.myschool.timeline.Timeline_App.App.main(App.java:120)  17/01/2016 02:10:41.860 [main] INFO o.h.e.j.c.i.DriverManagerConnectionProviderImpl - HHH000030: Cleaning up connection pool   17/01/2016 02:10:41.862 [main] DEBUG o.h.b.r.i.BootstrapServiceRegistryImpl - Implicitly destroying Boot-strap registry on de-registration of all child ServiceRegistries  exception, destroyed registry    

What do I need to do to fix this and be able to save objects to the database?

No comments:

Post a Comment