gpt4 book ai didi

java - Hibernate MappingException 未知实体 : $Proxy2

转载 作者:行者123 更新时间:2023-11-29 09:25:46 25 4
gpt4 key购买 nike

我正在使用 Hibernate 注释并且有一个非常基本的数据对象:

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class State implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;

@Id
private String stateCode;

private String stateFullName;

public String getStateCode() {
return stateCode;
}
public void setStateCode(String stateCode) {
this.stateCode = stateCode;
}
public String getStateFullName() {
return stateFullName;
}
public void setStateFullName(String stateFullName) {
this.stateFullName = stateFullName;
}

}

并且正在尝试运行以下测试用例:

public void testCreateState(){
Session s = HibernateUtil.getSessionFactory().getCurrentSession();

Transaction t = s.beginTransaction();

State state = new State();
state.setStateCode("NE");
state.setStateFullName("Nebraska");

s.save(s);

t.commit();

}

并得到一个

org.hibernate.MappingException: Unknown entity: $Proxy2
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:628)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1366)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
....

我无法找到任何引用错误的 $Proxy 部分的内容 - 并且不知所措.. 任何指向我遗漏的内容的指针将不胜感激。

hibernate .cfg.xml

<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost/xdb</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>

<property name="current_session_context_class">thread</property>

<property name="dialect">org.hibernate.dialect.HSQLDialect</property>

<property name="show_sql">true</property>

<property name="hbm2ddl.auto">update</property>

<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

<mapping class="com.test.domain.State"/>

在 HibernateUtil.java 中

public static SessionFactory getSessionFactory(boolean testing ) {

if ( sessionFactory == null ){
try {


String configPath = HIBERNATE_CFG;


AnnotationConfiguration config = new AnnotationConfiguration();
config.configure(configPath);
sessionFactory = config.buildSessionFactory();
} catch (Exception e){
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}

return sessionFactory;
}

最佳答案

如果将代码更改为以下内容,应用程序的输出是什么

Transaction t = s.beginTransaction();

State state = new State();
System.out.println(state.getClass().getName());
state.setStateCode("NE");

关于java - Hibernate MappingException 未知实体 : $Proxy2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2073223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com