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
线程“main”中的异常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 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>
答案 0 :(得分:4)
问题来自以下几行:
s = new Student(){{setFirstName("asdf");setLastName("asdf1");}};
简单地说,这一行是创建一个内部类的新实例,它隐式继承自Student
类。这个类由Java的编译器命名为TestClass$1
,因为没有映射,Hibernate抱怨它。你需要做的是:
s = new Student();
s.setFirstName("blah");
s.setLastName("blah2");
s.setSchool((School) db.getByID(School.class, 1));
答案 1 :(得分:0)
您的实体Student
的声明似乎不正确。如果您正在使用注释,请正确注释您的班级@javax.persistence.Entity
annotaton。
答案 2 :(得分:0)
我遇到了类似的问题,我的eclipse出现了java的版本问题而且它无法构建代码所以当我在hibernate.cfg.xml中添加新的实体条目时它没有刷新旧的,因此当hibernate正试图寻找它无法获得它的实体。
我通过删除工作区bin位置中的旧hibernate.cfg.xml解决了这个问题,然后粘贴了新的hibernate.cfg.xml和bingo工作..