(hibernate4)org.hibernate.MappingException:未知实体

时间:2012-12-17 11:13:19

标签: hibernate java-ee struts2 oracle11g

我是Java EE的新手,请帮忙!

以下代码使用struts2 + hibernate4 + oracle11g。运行此代码后,它给出了以下错误消息:

  

org.hibernate.MappingException:未知实体

UserDetails.java

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

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

@Entity
@Table(name="users")
public class UserDetails {
    @Id
    private int id;
    private String passwd;
    public String getPasswd() {
        return passwd;
    }
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public void Update(){
        SessionFactory sessionFactory;
        ServiceRegistry serviceRegistry;

        Configuration configuration = new Configuration();
        configuration.configure();
        serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(this);
        session.getTransaction().commit();
    }
}

的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="dialect">org.hibernate.dialect.Oracle10gDialect</property>
    <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
    <property name="connection.url">jdbc:oracle:thin:@192.168.1.2:1521:orcl</property>
    <property name="connection.username">steve</property>
    <property name="connection.password">steve</property>

    <property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>

    <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    <property name="current_session_context_class">thread</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hbm2ddl.auto">create</property>
  </session-factory>

</hibernate-configuration>

表格结构:

Create table Users (ID int primary key, passwd varchar(45));

3 个答案:

答案 0 :(得分:2)

您需要添加课程:

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

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

@Entity
@Table(name="users")
public class UserDetails {
    @Id
    private int id;
    private String passwd;
    public String getPasswd() {
        return passwd;
    }
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public void Update(){
        SessionFactory sessionFactory;
        ServiceRegistry serviceRegistry;

        Configuration configuration = new Configuration();
        configuration.addAnnotatedClass(UserDetails.class); //<-- Here, adds the class to the Hibernate configuration
        configuration.configure();
        serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(this);
        session.getTransaction().commit();
    }
}

每次想要将某些东西保存到表中时,你绝对不想构造一个新的Hibernate Configuration和SessionFactory,所以将东西从Update移到某个地方的init方法。

答案 1 :(得分:1)

问题是你没有将你的类定义为休眠。

session-factory内添加以下行:

<property name="annotatedClasses">
    <list>
        <value>myPackage.UserDetails</value>
    </list>
</property>

答案 2 :(得分:1)

缺少实体的Hibernate映射。或者,您可以在您的hibernate配置文件中使用。 并定义UserDetails.hbm.xml。我希望这会有所帮助。