我一直在努力寻找解决eclipse中未知实体的问题。 我正在使用单独的.java和hbm.xml文件。
我的实体类是Account.java
// default package
package com.mywebservice.domain;
// Generated Sep 9, 2013 3:55:42 PM by Hibernate Tools 3.4.0.CR1
import java.util.Date;
import javax.persistence.Entity;
/**
* Account generated by hbm2java
*/
public class Account implements java.io.Serializable {
private int id;
private String uid;
private String empId;
private String password;
private Integer status;
private Integer roleId;
private String name;
private String description;
private String details;
private String email;
private Date dateCreated;
private Date dateModified;
private String modifiedBy;
private Integer efpRoleId;
private Integer isEfp;
private Integer deptId;
private Boolean isEpp;
private Boolean isPasswordNew;
private Integer statusEpp;
public Account() {
}
public Account(int id) {
this.id = id;
}
public Account(int id, String uid, String empId, String password,
Integer status, Integer roleId, String name, String description,
String details, String email, Date dateCreated, Date dateModified,
String modifiedBy, Integer efpRoleId, Integer isEfp,
Integer deptId, Boolean isEpp, Boolean isPasswordNew,
Integer statusEpp) {
this.id = id;
this.uid = uid;
this.empId = empId;
this.password = password;
this.status = status;
this.roleId = roleId;
this.name = name;
this.description = description;
this.details = details;
this.email = email;
this.dateCreated = dateCreated;
this.dateModified = dateModified;
this.modifiedBy = modifiedBy;
this.efpRoleId = efpRoleId;
this.isEfp = isEfp;
this.deptId = deptId;
this.isEpp = isEpp;
this.isPasswordNew = isPasswordNew;
this.statusEpp = statusEpp;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getUid() {
return this.uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getEmpId() {
return this.empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getStatus() {
return this.status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getRoleId() {
return this.roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDetails() {
return this.details;
}
public void setDetails(String details) {
this.details = details;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getDateCreated() {
return this.dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public Date getDateModified() {
return this.dateModified;
}
public void setDateModified(Date dateModified) {
this.dateModified = dateModified;
}
public String getModifiedBy() {
return this.modifiedBy;
}
public void setModifiedBy(String modifiedBy) {
this.modifiedBy = modifiedBy;
}
public Integer getEfpRoleId() {
return this.efpRoleId;
}
public void setEfpRoleId(Integer efpRoleId) {
this.efpRoleId = efpRoleId;
}
public Integer getIsEfp() {
return this.isEfp;
}
public void setIsEfp(Integer isEfp) {
this.isEfp = isEfp;
}
public Integer getDeptId() {
return this.deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public Boolean getIsEpp() {
return this.isEpp;
}
public void setIsEpp(Boolean isEpp) {
this.isEpp = isEpp;
}
public Boolean getIsPasswordNew() {
return this.isPasswordNew;
}
public void setIsPasswordNew(Boolean isPasswordNew) {
this.isPasswordNew = isPasswordNew;
}
public Integer getStatusEpp() {
return this.statusEpp;
}
public void setStatusEpp(Integer statusEpp) {
this.statusEpp = statusEpp;
}
}
Account.hbm.xml是
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 9, 2013 3:55:42 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="Account" table="ACCOUNT" schema="dbo" catalog="EPP">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="uid" type="string">
<column name="UID" length="50" />
</property>
<property name="empId" type="string">
<column name="EMP_ID" length="50" />
</property>
<property name="password" type="string">
<column name="PASSWORD" length="50" />
</property>
<property name="status" type="java.lang.Integer">
<column name="STATUS" />
</property>
<property name="roleId" type="java.lang.Integer">
<column name="ROLE_ID" />
</property>
<property name="name" type="string">
<column name="NAME" length="50" />
</property>
<property name="description" type="string">
<column name="DESCRIPTION" length="50" />
</property>
<property name="details" type="string">
<column name="DETAILS" length="50" />
</property>
<property name="email" type="string">
<column name="EMAIL" length="50" />
</property>
<property name="dateCreated" type="timestamp">
<column name="DATE_CREATED" length="23" />
</property>
<property name="dateModified" type="timestamp">
<column name="DATE_MODIFIED" length="23" />
</property>
<property name="modifiedBy" type="string">
<column name="MODIFIED_BY" length="50" />
</property>
<property name="efpRoleId" type="java.lang.Integer">
<column name="EFP_ROLE_ID" />
</property>
<property name="isEfp" type="java.lang.Integer">
<column name="IS_EFP" />
</property>
<property name="deptId" type="java.lang.Integer">
<column name="DEPT_ID" />
</property>
<property name="isEpp" type="java.lang.Boolean">
<column name="IS_EPP" />
</property>
<property name="isPasswordNew" type="java.lang.Boolean">
<column name="IS_PASSWORD_NEW" />
</property>
<property name="statusEpp" type="java.lang.Integer">
<column name="STATUS_EPP" />
</property>
</class>
</hibernate-mapping>
我在util
中创建会话工厂package com.mywebservice.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class SessionFactoryUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
//Configuration config = new Configuration();
Configuration config = new AnnotationConfiguration();
sessionFactory = config.configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
并从
调用它package com.mywebservice.utils;
import org.hibernate.Session;
import org.hibernate.Query;
import com.mywebservice.domain.*;
import org.hibernate.annotations.*;
public class HibernateTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
getAccount(session);
}
public static void getAccount(Session session) {
Account acc = new Account();
acc.setEmpId("123456789");
session.save(acc);
System.out.println("Saved!");
}
}
另外,我在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">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">hrportal@123</property>
<property name="hibernate.connection.url">jdbc:sqlserver://HQ-10063332D\SQLEXPRESS;DatabaseName=EPP</property>
<!-- <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property> -->
<property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="com.mywebservice.domain.Account" />
</session-factory>
</hibernate-configuration>
但仍然没有运气。 有线索吗?
我正在使用: Hibernate 3.5 JPA 2.0 Eclipse KEPLER
更新: 我的完整错误堆栈跟踪是
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.mywebservice.domain.Account
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:706)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1475)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:693)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:689)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344)
at com.sun.proxy.$Proxy0.save(Unknown Source)
at com.mywebservice.utils.HibernateTest.getAccount(HibernateTest.java:24)
at com.mywebservice.utils.HibernateTest.main(HibernateTest.java:15)
答案 0 :(得分:0)
我相信你错过了hibernate映射中的package
属性。尝试更新hbm
文件中的映射部分,如下所示:
<hibernate-mapping package="com.mywebservice.domain">
答案 1 :(得分:0)
您的Account
课程位于com.mywebservice.domain
个包中,因此在您的Account.hbm.xml
class
名称应为com.mywebservice.domain.Account
,而不仅仅是Account
< / p>
答案 2 :(得分:0)
我想知道,如果您使用XML来配置bean,为什么使用annotationConfiguration来构建会话,您可以使用注释配置来尝试它。可能有用。
答案 3 :(得分:0)
回答我自己的问题,以便将来可以帮助其他人。
在我的hibernate.cfg.xml中,映射应该像
<mapping resource="com/mywebservice/domain/Account.hbm.xml"></mapping>
而不是
<mapping class="com.mywebservice.domain.Account" />
在纠正我的hibernate.cfg.xml之后,我收到了一个错误 “无法启动默认tuplizer”,我必须添加一个名为javassist.jar的库,它工作了:) 祝你好运