Hibernate MappingException:会话保存时的未知实体
Hibernate MappingException: Unknown entity on session save
当我从另一个 class.
调用 addEventAction
方法时,使用休眠对 postgresql 数据库进行保存操作时出现异常
EventDAO.java:
package com.sessionpoint.session.sessiondr.core;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.sessionpoint.session.sessiondr.db.DrAction;
/**
* Logic for the database operations related to the actions.
*
*/
public class EventDAO {
//To get the logger for class
Logger log = Logger.getLogger("EventDAO");
/**
* Add dispatched event action into the database.
*
* @param eventAction
*/
public void addEventAction(DrAction eventAction) {
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
log.info("Connection with the database created successfuly.");
try {
trns = session.beginTransaction();
session.save(eventAction); //exception here
session.getTransaction().commit();
} catch (RuntimeException e) {
if (trns != null) {
trns.rollback();
}
e.printStackTrace();
} finally {
session.flush();
session.close();
}
}
}
HibernateUtil.java:
public class HibernateUtil {
private static SessionFactory sessionFactory;
//To get the logger for class
static Logger log = Logger.getLogger("HibernateUtil");
static {
log.info("Trying to create a connection with the database.");
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(ssrb.build());
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
异常跟踪:
org.hibernate.MappingException: Unknown entity: com.sessionpoint.session.sessiondr.db.DrAction
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1094)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1439)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:116)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:711)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:703)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:698)
at com.sessionpoint.session.sessiondr.core.EventDAO.addEventAction(EventDAO.java:35)
更新:
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">user</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/drcore</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
</session-factory>
</hibernate-configuration>
DrAction.java:
/**
* DrAction for the dr_action table
*/
public class DrAction implements java.io.Serializable {
/**
* Default serial version Id.
*/
private static final long serialVersionUID = 1L;
/**
* Auto increment drActionId.
*/
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long drActionId;
private String drVName;
private String drVRequestId;
private Long vKey;
public long getDrActionId() {
return drActionId;
}
public void setDrActionId(long drActionId) {
this.drActionId = drActionId;
}
public Long getvKey() {
return vKey;
}
public void setvKey(Long vKey) {
this.vKey = vKey;
}
public String getDrVName() {
return drVName;
}
public void setDrVName(String drVName) {
this.drVName = drVName;
}
public String getDrVRequestId() {
return drVRequestId;
}
public void setDrVRequestId(String drVRequestId) {
this.drVRequestId = drVRequestId;
}
}
更新:
异常跟踪 2:
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:89)
at org.hibernate.id.SequenceGenerator.generateHolder(SequenceGenerator.java:122)
at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:115)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:711)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:703)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:698)
at com.sessionpoint.session.sessiondr.core.EventDAO.addEventAction(EventDAO.java:35)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.postgresql.util.PSQLException: ERROR: relation "hibernate_sequence" does not exist
Position: 17
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:302)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:80)
更新:
异常跟踪 3:
Caused by: org.hibernate.MappingNotFoundException: resource: com/sessionpoint/session/sessiondr/db/DrAction.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:767)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2255)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2227)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2207)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
at com.sessionpoint.session.sessiondr.core.HibernateUtil.<clinit>(HibernateUtil.java:24)
将@Entity
添加到DrAction
class,并将<mapping class="your.package.DrAction"/>
添加到hibernate.cfg.xml
。
@Entity
@Table(name = "dr_action")
public class DrAction implements java.io.Serializable {
hibernate.cfg.xml
...
<session-factory>
....
<mapping class="your.package.DrAction"/>
</session-factory>
....
更新
您可能正在使用 Oracle 作为数据库,而 Oracle 不支持自动生成 ID。您必须指定将用于生成 ID 的序列。
在数据库中执行这个
create sequence DR_ACTION_SEQ;
并将 id 映射更改为此
@Id
@GeneratedValue(generator="drActionIdSeq")
@SequenceGenerator(name="drActionIdSeq",sequenceName="DR_ACTION_SEQ", allocationSize=5)
private long drActionId;
请注意,这需要您的 id 列被命名为 dractionid
,如果不是,那么您必须向该字段添加一个 @Column
注释,类似这样
@Column(name = "dr_action_id")
您必须对名称与数据库中的列不同的每个列执行此操作 table。
将以下行添加到您的 hibernate.cfg.xml
<mapping class="xxx.yyy.DrAction"/>//provide full package name
和@Entity
和@Table
(name="dr_action") annotation on
DrAction` class
Postgre 中的 @GeneratedValue
注释使用序列 tables。由于您没有明确提供名称 hibernate 默认情况下会查找 hibernate_sequence
序列 table.
在 postgre 架构中按名称 hibernate_sequence
或任何其他名称添加序列 table,然后在注释中添加该名称。
当我从另一个 class.
调用addEventAction
方法时,使用休眠对 postgresql 数据库进行保存操作时出现异常
EventDAO.java:
package com.sessionpoint.session.sessiondr.core;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.sessionpoint.session.sessiondr.db.DrAction;
/**
* Logic for the database operations related to the actions.
*
*/
public class EventDAO {
//To get the logger for class
Logger log = Logger.getLogger("EventDAO");
/**
* Add dispatched event action into the database.
*
* @param eventAction
*/
public void addEventAction(DrAction eventAction) {
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
log.info("Connection with the database created successfuly.");
try {
trns = session.beginTransaction();
session.save(eventAction); //exception here
session.getTransaction().commit();
} catch (RuntimeException e) {
if (trns != null) {
trns.rollback();
}
e.printStackTrace();
} finally {
session.flush();
session.close();
}
}
}
HibernateUtil.java:
public class HibernateUtil {
private static SessionFactory sessionFactory;
//To get the logger for class
static Logger log = Logger.getLogger("HibernateUtil");
static {
log.info("Trying to create a connection with the database.");
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(ssrb.build());
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
异常跟踪:
org.hibernate.MappingException: Unknown entity: com.sessionpoint.session.sessiondr.db.DrAction
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1094)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1439)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:116)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:711)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:703)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:698)
at com.sessionpoint.session.sessiondr.core.EventDAO.addEventAction(EventDAO.java:35)
更新:
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">user</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/drcore</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
</session-factory>
</hibernate-configuration>
DrAction.java:
/**
* DrAction for the dr_action table
*/
public class DrAction implements java.io.Serializable {
/**
* Default serial version Id.
*/
private static final long serialVersionUID = 1L;
/**
* Auto increment drActionId.
*/
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long drActionId;
private String drVName;
private String drVRequestId;
private Long vKey;
public long getDrActionId() {
return drActionId;
}
public void setDrActionId(long drActionId) {
this.drActionId = drActionId;
}
public Long getvKey() {
return vKey;
}
public void setvKey(Long vKey) {
this.vKey = vKey;
}
public String getDrVName() {
return drVName;
}
public void setDrVName(String drVName) {
this.drVName = drVName;
}
public String getDrVRequestId() {
return drVRequestId;
}
public void setDrVRequestId(String drVRequestId) {
this.drVRequestId = drVRequestId;
}
}
更新:
异常跟踪 2:
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:89)
at org.hibernate.id.SequenceGenerator.generateHolder(SequenceGenerator.java:122)
at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:115)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:711)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:703)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:698)
at com.sessionpoint.session.sessiondr.core.EventDAO.addEventAction(EventDAO.java:35)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.postgresql.util.PSQLException: ERROR: relation "hibernate_sequence" does not exist
Position: 17
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:302)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:80)
更新:
异常跟踪 3:
Caused by: org.hibernate.MappingNotFoundException: resource: com/sessionpoint/session/sessiondr/db/DrAction.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:767)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2255)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2227)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2207)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
at com.sessionpoint.session.sessiondr.core.HibernateUtil.<clinit>(HibernateUtil.java:24)
将@Entity
添加到DrAction
class,并将<mapping class="your.package.DrAction"/>
添加到hibernate.cfg.xml
。
@Entity
@Table(name = "dr_action")
public class DrAction implements java.io.Serializable {
hibernate.cfg.xml
...
<session-factory>
....
<mapping class="your.package.DrAction"/>
</session-factory>
....
更新
您可能正在使用 Oracle 作为数据库,而 Oracle 不支持自动生成 ID。您必须指定将用于生成 ID 的序列。
在数据库中执行这个
create sequence DR_ACTION_SEQ;
并将 id 映射更改为此
@Id
@GeneratedValue(generator="drActionIdSeq")
@SequenceGenerator(name="drActionIdSeq",sequenceName="DR_ACTION_SEQ", allocationSize=5)
private long drActionId;
请注意,这需要您的 id 列被命名为 dractionid
,如果不是,那么您必须向该字段添加一个 @Column
注释,类似这样
@Column(name = "dr_action_id")
您必须对名称与数据库中的列不同的每个列执行此操作 table。
将以下行添加到您的 hibernate.cfg.xml
<mapping class="xxx.yyy.DrAction"/>//provide full package name
和@Entity
和@Table
(name="dr_action") annotation on
DrAction` class
Postgre 中的 @GeneratedValue
注释使用序列 tables。由于您没有明确提供名称 hibernate 默认情况下会查找 hibernate_sequence
序列 table.
在 postgre 架构中按名称 hibernate_sequence
或任何其他名称添加序列 table,然后在注释中添加该名称。