2
    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>
2
  • Please post stack trace Commented Nov 7, 2013 at 4:36
  • @kark posted (not enough chars) Commented Nov 7, 2013 at 4:38

3 Answers 3

5

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));
Sign up to request clarification or add additional context in comments.

1 Comment

That's the ticket, thank you! FYI, TestClass is the name of the class all this was being ran inside of. So that's there that came from and the $1 was tacked on by jvm. Thanks.
0

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

4 Comments

I'm sorry, what class? I'm not sure if you're asking me to annotate the variable "s" or the actual Student class or the testclass I'm using?
@Preston Student class
Added "@Entity public class Student {" as per docs here docs.jboss.org/hibernate/annotations/3.5/reference/en/html/… Also tried it with @javax.persistence.Entity which is the same. It still throws the error, shouldn't this annotation be taken care of by the hbm config files for each entity?
You don't need annotations as you are using hbm.
0

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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.