Preston
Preston

Reputation: 1346

Hibernate unknown entity error when saving new object

    Student s = (Student) db.getByID(Student.class,1);
    s.setFirstName("Preston");
    db.saveOrUpdate(s); <--Successful save
    s = new Student(){{setFirstName("asdf");setLastName("asdf1");}};
    s.setSchool((School) db.getByID(School.class, 1));
    db.saveOrUpdate(s); <--Error occurs 

Exception in thread "main" org.hibernate.MappingException: Unknown entity: project.databaseio.TestClass$1

public void saveOrUpdate(Object object)
{
    Transaction  tx = sessionFactory.getCurrentSession().beginTransaction();
    sessionFactory.getCurrentSession().saveOrUpdate(object);
    tx.commit();
}

Stack Trace

Exception in thread "main" org.hibernate.MappingException: Unknown entity: project.databaseio.TestClass$1
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:548)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1338)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:180)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:487)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:84)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at com.sun.proxy.$Proxy0.saveOrUpdate(Unknown Source)
at project.databaseio.DatabaseConnection.saveOrUpdate(DatabaseConnection.java:117)
at project.databaseio.TestClass.main(TestClass.java:21)

Hibernate Config for Student

<hibernate-mapping>
    <class name="project.databaseio.Student" table="Student" catalog="fblaem">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <many-to-one name="school" class="project.databaseio.School" fetch="select">
            <column name="SchoolID" not-null="true" />
        </many-to-one>
        <property name="firstName" type="string">
            <column name="FirstName" not-null="true" />
        </property>
        <property name="lastName" type="string">
            <column name="LastName" not-null="true" />
        </property>
        <property name="middleName" type="string">
            <column name="MiddleName" />
        </property>
        <property name="createDate" type="timestamp">
            <column name="CreateDate" length="0" />
        </property>
        <set name="studentEventTeams" table="StudentEventTeam" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="StudentID" not-null="true" />
            </key>
            <one-to-many class="project.databaseio.StudentEventTeam" />
        </set>
    </class>
</hibernate-mapping>

Upvotes: 2

Views: 3349

Answers (3)

Kalyan
Kalyan

Reputation: 1

I ran into similar problem, where my eclipse has got version problem with java and it couldnt build the code so when i added the new entity entry in my hibernate.cfg.xml it did not refresh the old one and hence when hibernate is trying to look for entity it couldnt get it.

I resolved it by deleting the old hibernate.cfg.xml in my bin location of the workspace and then pasted the new hibernate.cfg.xml and bingo it worked..

Upvotes: 0

Ean V
Ean V

Reputation: 5459

The problem is coming from the following line:

s = new Student(){{setFirstName("asdf");setLastName("asdf1");}};

Briefly this line is creating a new instance of an inner class which implicitly inherits from Student class. This class is named TestClass$1 by Java's compiler and because there is no mapping for that, Hibernate is complaining about it. What you need to do is:

s = new Student();
s.setFirstName("blah");
s.setLastName("blah2");
s.setSchool((School) db.getByID(School.class, 1));

Upvotes: 5

Juned Ahsan
Juned Ahsan

Reputation: 68715

The declaration of your entity Student seems to be incorrect. If you are using annotations then properly annotate your class @javax.persistence.Entity annotaton.

Upvotes: 0

Related Questions