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
线程“主”org.hibernate.MappingException 中的异常:未知实体:project.databaseio.TestClass$1
public void saveOrUpdate(Object object)
{
Transaction tx = sessionFactory.getCurrentSession().beginTransaction();
sessionFactory.getCurrentSession().saveOrUpdate(object);
tx.commit();
}
堆栈跟踪
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-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>