1

我整天都在尝试和搜索以解决 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>

我正在实用程序中创建我的会话工厂

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>

但仍然没有运气。有什么线索吗?

我正在使用:休眠 3.5 JPA 2.0 Eclipse 开普勒

更新:我的完整错误堆栈跟踪是

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)
4

4 回答 4

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 的库并且它可以工作:) 祝你好运

于 2013-09-10T05:20:42.653 回答
0

我想知道,如果你使用 XML 来配置你的 bean,为什么你使用 annotationConfiguration 来构建你的会话,你可以用 annotation config 试试。可能有效。

于 2013-09-09T12:59:17.650 回答
0

我相信您在休眠映射中缺少该package属性。尝试更新文件中的映射部分hbm,如下所示:

<hibernate-mapping package="com.mywebservice.domain">
于 2013-09-09T11:46:20.567 回答
0

你的Account班级在包里com.mywebservice.domain,所以你Account.hbm.xmlclass名字应该是com.mywebservice.domain.Account,而不仅仅是Account

于 2013-09-09T11:47:50.900 回答