Hibernate Unknown Entity exception when saving object to database
<p>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.</p>

<p>Launcher Class:</p>

<pre><code>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");
        }
</code></pre>

<p>Dept.java:</p>

<pre><code>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&lt;Course&gt; courses = new HashSet&lt;Course&gt;(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&lt;Course&gt; courses) {
        this.deptId = deptId;
        this.name = name;
        this.type = type;
        this.courses = courses;
    }
    //Getters and Setters cut for brevity
}
</code></pre>

<p>hibernate.cfg.xml:</p>

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

<p>And Dept.hbm.xml: </p>

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

<p>This is the error I get when i launch:</p>

<pre><code>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
</code></pre>

<p>Full output:</p>

<pre><code>17/01/2016 02:10:35.891 [main] DEBUG org.jboss.logging - Logging Provider: org.jboss.logging.Slf4jLoggerProvider
17/01/2016 02:10:35.943 [main] DEBUG o.h.i.i.IntegratorServiceImpl - Adding Integrator [org.hibernate.cfg.beanvalidation.BeanValidationIntegrator].
17/01/2016 02:10:35.949 [main] DEBUG o.h.i.i.IntegratorServiceImpl - Adding Integrator [org.hibernate.secure.spi.JaccIntegrator].
17/01/2016 02:10:35.953 [main] DEBUG o.h.i.i.IntegratorServiceImpl - Adding Integrator [org.hibernate.cache.internal.CollectionCacheInvalidator].
17/01/2016 02:10:35.987 [main] DEBUG o.h.b.r.s.i.StrategySelectorImpl - Registering named strategy selector [org.hibernate.dialect.Dialect] : [MySQL5] -&gt; [org.hibernate.dialect.MySQL5Dialect] (replacing [org.hibernate.dialect.MySQL5Dialect])
17/01/2016 02:10:35.987 [main] DEBUG o.h.b.r.s.i.StrategySelectorImpl - Registering named strategy selector [org.hibernate.dialect.Dialect] : [MySQL5InnoDB] -&gt; [org.hibernate.dialect.MySQL5InnoDBDialect] (replacing [org.hibernate.dialect.MySQL5InnoDBDialect])
17/01/2016 02:10:36.082 [main] INFO  org.hibernate.Version - HHH000412: Hibernate Core {4.3.11.Final}
17/01/2016 02:10:36.086 [main] INFO  o.h.cfg.Environment - HHH000206: hibernate.properties not found
17/01/2016 02:10:36.089 [main] INFO  o.h.cfg.Environment - HHH000021: Bytecode provider name : javassist
17/01/2016 02:10:37.928 [main] DEBUG o.h.s.i.JaxbProcessor - cfg.xml document did not define namespaces; wrapping in custom event reader to introduce namespace information
17/01/2016 02:10:38.405 [main] DEBUG o.h.s.spi.ServiceBinding - Overriding existing service binding [org.hibernate.secure.spi.JaccService]
adds all the types
17/01/2016 02:10:38.651 [main] DEBUG o.h.i.f.i.DefaultIdentifierGeneratorFactory - Registering IdentifierGenerator strategy [uuid2] -&gt; [org.hibernate.id.UUIDGenerator]
17/01/2016 02:10:38.652 [main] DEBUG o.h.i.f.i.DefaultIdentifierGeneratorFactory - Registering IdentifierGenerator strategy [guid] -&gt; [org.hibernate.id.GUIDGenerator]
17/01/2016 02:10:38.654 [main] DEBUG o.h.i.f.i.DefaultIdentifierGeneratorFactory - Registering IdentifierGenerator strategy [uuid] -&gt; [org.hibernate.id.UUIDHexGenerator]
17/01/2016 02:10:38.654 [main] DEBUG o.h.i.f.i.DefaultIdentifierGeneratorFactory - Registering IdentifierGenerator strategy [uuid.hex] -&gt; [org.hibernate.id.UUIDHexGenerator]
17/01/2016 02:10:38.657 [main] DEBUG o.h.i.f.i.DefaultIdentifierGeneratorFactory - Registering IdentifierGenerator strategy [hilo] -&gt; [org.hibernate.id.TableHiLoGenerator]
17/01/2016 02:10:38.658 [main] DEBUG o.h.i.f.i.DefaultIdentifierGeneratorFactory - Registering IdentifierGenerator strategy [assigned] -&gt; [org.hibernate.id.Assigned]
17/01/2016 02:10:38.660 [main] DEBUG o.h.i.f.i.DefaultIdentifierGeneratorFactory - Registering IdentifierGenerator strategy [identity] -&gt; [org.hibernate.id.IdentityGenerator]
17/01/2016 02:10:38.661 [main] DEBUG o.h.i.f.i.DefaultIdentifierGeneratorFactory - Registering IdentifierGenerator strategy [select] -&gt; [org.hibernate.id.SelectGenerator]
17/01/2016 02:10:38.662 [main] DEBUG o.h.i.f.i.DefaultIdentifierGeneratorFactory - Registering IdentifierGenerator strategy [sequence] -&gt; [org.hibernate.id.SequenceGenerator]
17/01/2016 02:10:38.663 [main] DEBUG o.h.i.f.i.DefaultIdentifierGeneratorFactory - Registering IdentifierGenerator strategy [seqhilo] -&gt; [org.hibernate.id.SequenceHiLoGenerator]
17/01/2016 02:10:38.665 [main] DEBUG o.h.i.f.i.DefaultIdentifierGeneratorFactory - Registering IdentifierGenerator strategy [increment] -&gt; [org.hibernate.id.IncrementGenerator]
17/01/2016 02:10:38.666 [main] DEBUG o.h.i.f.i.DefaultIdentifierGeneratorFactory - Registering IdentifierGenerator strategy [foreign] -&gt; [org.hibernate.id.ForeignGenerator]
17/01/2016 02:10:38.667 [main] DEBUG o.h.i.f.i.DefaultIdentifierGeneratorFactory - Registering IdentifierGenerator strategy [sequence-identity] -&gt; [org.hibernate.id.SequenceIdentityGenerator]
17/01/2016 02:10:38.668 [main] DEBUG o.h.i.f.i.DefaultIdentifierGeneratorFactory - Registering IdentifierGenerator strategy [enhanced-sequence] -&gt; [org.hibernate.id.enhanced.SequenceStyleGenerator]
17/01/2016 02:10:38.670 [main] DEBUG o.h.i.f.i.DefaultIdentifierGeneratorFactory - Registering IdentifierGenerator strategy [enhanced-table] -&gt; [org.hibernate.id.enhanced.TableGenerator]
17/01/2016 02:10:38.686 [main] WARN  o.h.e.j.c.i.DriverManagerConnectionProviderImpl - HHH000402: Using Hibernate built-in connection pool (not for production use!)
17/01/2016 02:10:38.700 [main] INFO  o.h.e.j.c.i.DriverManagerConnectionProviderImpl - HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://67.190.147.230:3306/school]
17/01/2016 02:10:38.701 [main] INFO  o.h.e.j.c.i.DriverManagerConnectionProviderImpl - HHH000006: Autocommit mode: false
17/01/2016 02:10:38.704 [main] INFO  o.h.e.j.c.i.DriverManagerConnectionProviderImpl - HHH000115: Hibernate connection pool size: 20 (min=1)
17/01/2016 02:10:38.705 [main] DEBUG o.h.e.j.c.i.DriverManagerConnectionProviderImpl - Initializing Connection pool with 1 Connections
17/01/2016 02:10:40.892 [main] DEBUG o.h.e.j.i.JdbcServicesImpl - JDBC version : 4.0
17/01/2016 02:10:40.897 [main] DEBUG o.h.e.j.c.i.StandardRefCursorSupport - Unexpected error trying to gauge level of JDBC REF_CURSOR support : null
17/01/2016 02:10:40.963 [main] INFO  o.h.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
17/01/2016 02:10:41.268 [main] DEBUG o.h.i.SessionFactoryImpl - Building session factory
17/01/2016 02:10:41.275 [main] DEBUG o.h.e.t.j.p.i.JtaPlatformInitiator - No JtaPlatform was specified, checking resolver
17/01/2016 02:10:41.276 [main] DEBUG o.h.e.t.j.p.i.JtaPlatformResolverInitiator - No JtaPlatformResolver was specified, using default [org.hibernate.engine.transaction.jta.platform.internal.StandardJtaPlatformResolver]
17/01/2016 02:10:41.282 [main] DEBUG o.h.e.t.j.p.i.StandardJtaPlatformResolver - Could not resolve JtaPlatform, using default [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
17/01/2016 02:10:41.292 [main] DEBUG o.h.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
17/01/2016 02:10:41.292 [main] DEBUG o.h.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
17/01/2016 02:10:41.293 [main] DEBUG o.h.cfg.SettingsFactory - JDBC batch size: 15
17/01/2016 02:10:41.293 [main] DEBUG o.h.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
17/01/2016 02:10:41.293 [main] DEBUG o.h.cfg.SettingsFactory - Scrollable result sets: enabled
7/01/2016 02:10:41.293 [main] DEBUG o.h.cfg.SettingsFactory - Wrap result sets: disabled
17/01/2016 02:10:41.293 [main] DEBUG o.h.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
17/01/2016 02:10:41.294 [main] DEBUG o.h.cfg.SettingsFactory - multi-tenancy strategy : NONE
17/01/2016 02:10:41.294 [main] DEBUG o.h.cfg.SettingsFactory - Connection release mode: auto
17/01/2016 02:10:41.294 [main] INFO  o.h.e.t.i.TransactionFactoryInitiator - HHH000399: Using default transaction strategy (direct JDBC transactions)
17/01/2016 02:10:41.300 [main] DEBUG o.h.cfg.SettingsFactory - Using BatchFetchStyle : LEGACY
17/01/2016 02:10:41.300 [main] DEBUG o.h.cfg.SettingsFactory - Maximum outer join fetch depth: 2
17/01/2016 02:10:41.300 [main] DEBUG o.h.cfg.SettingsFactory - Default batch fetch size: 1
17/01/2016 02:10:41.301 [main] DEBUG o.h.cfg.SettingsFactory - Generate SQL with comments: disabled
17/01/2016 02:10:41.301 [main] DEBUG o.h.cfg.SettingsFactory - Order SQL updates by primary key: disabled
17/01/2016 02:10:41.301 [main] DEBUG o.h.cfg.SettingsFactory - Order SQL inserts for batching: disabled
17/01/2016 02:10:41.301 [main] DEBUG o.h.cfg.SettingsFactory - Default null ordering: none
17/01/2016 02:10:41.302 [main] DEBUG o.h.cfg.SettingsFactory - Query translator: org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory
17/01/2016 02:10:41.306 [main] INFO  o.h.h.i.a.ASTQueryTranslatorFactory - HHH000397: Using ASTQueryTranslatorFactory
17/01/2016 02:10:41.307 [main] DEBUG o.h.cfg.SettingsFactory - Query language substitutions: {}
17/01/2016 02:10:41.307 [main] DEBUG o.h.cfg.SettingsFactory - JPA-QL strict compliance: disabled
17/01/2016 02:10:41.307 [main] DEBUG o.h.cfg.SettingsFactory - Second-level cache: enabled
17/01/2016 02:10:41.307 [main] DEBUG o.h.cfg.SettingsFactory - Query cache: disabled
17/01/2016 02:10:41.309 [main] DEBUG o.h.c.i.RegionFactoryInitiator - Cache region factory : org.hibernate.cache.internal.NoCachingRegionFactory
17/01/2016 02:10:41.314 [main] DEBUG o.h.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
17/01/2016 02:10:41.314 [main] DEBUG o.h.cfg.SettingsFactory - Structured second-level cache entries: disabled
17/01/2016 02:10:41.314 [main] DEBUG o.h.cfg.SettingsFactory - Second-level cache direct-reference entries: disabled
17/01/2016 02:10:41.314 [main] DEBUG o.h.cfg.SettingsFactory - Automatic eviction of collection cache: disabled
17/01/2016 02:10:41.314 [main] DEBUG o.h.cfg.SettingsFactory - Statistics: disabled
17/01/2016 02:10:41.315 [main] DEBUG o.h.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
17/01/2016 02:10:41.316 [main] DEBUG o.h.cfg.SettingsFactory - Default entity-mode: pojo
17/01/2016 02:10:41.317 [main] DEBUG o.h.cfg.SettingsFactory - Named query checking : enabled
17/01/2016 02:10:41.317 [main] DEBUG o.h.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): enabled
17/01/2016 02:10:41.331 [main] DEBUG o.h.cfg.SettingsFactory - Allow initialization of lazy state outside session : : disabled
17/01/2016 02:10:41.332 [main] DEBUG o.h.cfg.SettingsFactory - JTA Track by Thread: enabled
17/01/2016 02:10:41.357 [main] DEBUG o.h.i.SessionFactoryImpl - Session factory constructed with filter configurations : {}
17/01/2016 02:10:41.384 [main] DEBUG o.h.s.spi.JaccIntegrator - Skipping JACC integration as it was not enabled
17/01/2016 02:10:41.592 [main] DEBUG o.h.i.SessionFactoryRegistry - Initializing SessionFactoryRegistry : org.hibernate.internal.SessionFactoryRegistry@36916eb0
17/01/2016 02:10:41.597 [main] DEBUG o.h.i.SessionFactoryRegistry - Registering SessionFactory: 6757530c-07f1-45bd-8c6f-a3a9697d6341 (&lt;unnamed&gt;)
17/01/2016 02:10:41.597 [main] DEBUG o.h.i.SessionFactoryRegistry - Not binding SessionFactory to JNDI, no JNDI name configured
17/01/2016 02:10:41.597 [main] DEBUG o.h.i.SessionFactoryImpl - Instantiated session factory
17/01/2016 02:10:41.598 [main] DEBUG o.h.i.NamedQueryRepository - Checking 0 named HQL queries
17/01/2016 02:10:41.598 [main] DEBUG o.h.i.NamedQueryRepository - Checking 0 named SQL queries
17/01/2016 02:10:41.610 [main] DEBUG o.h.s.i.StatisticsInitiator - Statistics initialized [enabled=false]
sessionfactory made, now creating session
made session
17/01/2016 02:10:41.739 [main] DEBUG o.h.e.t.s.AbstractTransactionImpl - begin
17/01/2016 02:10:41.740 [main] DEBUG o.h.e.j.i.LogicalConnectionImpl - Obtaining JDBC connection
17/01/2016 02:10:41.740 [main] DEBUG o.h.e.j.i.LogicalConnectionImpl - Obtained JDBC connection
17/01/2016 02:10:41.740 [main] DEBUG o.h.e.t.i.j.JdbcTransaction - initial autocommit status: false
</code></pre>

<p>What do I need to do to fix this and be able to save objects to the database?</p>