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 

线程“主”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>
4

3 回答 3

5

问题来自以下行:

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

简而言之,这一行正在创建一个隐式继承自类的内部类的新实例Student。这个类是TestClass$1由 Java 的编译器命名的,因为没有映射,Hibernate 抱怨它。你需要做的是:

s = new Student();
s.setFirstName("blah");
s.setLastName("blah2");
s.setSchool((School) db.getByID(School.class, 1));
于 2013-11-07T06:19:50.660 回答
0

您的实体的声明Student似乎不正确。如果您正在使用注释,请正确注释您的类@javax.persistence.Entity注释。

于 2013-11-07T04:37:32.903 回答
0

我遇到了类似的问题,我的 Eclipse 遇到了 java 版本问题,它无法构建代码,所以当我在 hibernate.cfg.xml 中添加新实体条目时,它没有刷新旧实体条目,因此当 hibernate 试图寻找它无法得到的实体。

我通过删除工作区的 bin 位置中的旧 hibernate.cfg.xml 解决了这个问题,然后粘贴了新的 hibernate.cfg.xml 和宾果游戏。

于 2015-11-03T19:39:25.557 回答