java - Hibernate Eclipse 未知实体(使用 javax.persistence.Entity)

标签 java eclipse hibernate jpa

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

我正在实用程序中创建 session 工厂

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 开普勒

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

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)

最佳答案

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

<hibernate-mapping package="com.mywebservice.domain">

关于java - Hibernate Eclipse 未知实体(使用 javax.persistence.Entity),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18697433/

相关文章:

java - cucumber 5 : Get step name from feature file excluding gherkin syntax (given, 当、然后、和)

java - 曼宁·斯普林 (Manning Spring),Hibernate 封面图片

eclipse - 如何从Gradle Tasks View 中删除项目或清理 View ?

java - 如何在生产模式下运行 GWT

eclipse - java.lang.NoClassDefFoundError : org/apache/commons/logging/LogFactory in Hadoop 1. 2.1 Kmean算法

java - 无法在 hibernate 中保存ManyToMany自引用关系

java - JPA 中的空版本字段?

Java用==比较两个字符串是错误的?

java - 无法在 Android MediaRecorder 中设置手动视频大小

java - 包 org.jetbrains.annotations 不存在