ManyToOne注释因Hibernate 4.1:MappingException而失败

时间:2012-05-31 10:01:18

标签: java hibernate

使用Hibernate 4.1.1.Final。

当我尝试添加@ManyToOne时,架构创建失败:org.hibernate.MappingException:无法实例化persister org.hibernate.persister.entity.SingleTableEntityPersister

User.java:

@Entity
public class User { 
 @Id
 private int id;
 public int getId() {return id;}
 public void setId(int id) {this.id = id;}
 @ManyToOne
 Department department;
 public Department getDepartment() {return department;}
 public void setDepartment(Department department) {this.department = department;}
}

Department.java

@Entity
public class Department {
 @Id
 private int departmentNumber;
 public int getDepartmentNumber() {return departmentNumber;}
 public void setDepartmentNumber(int departmentNumber) {this.departmentNumber = departmentNumber;}
}

hibernate.properties:

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/dbname
hibernate.connection.username=user
hibernate.connection.password=pass
hibernate.connection.pool_size=5
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.hbm2ddl.auto=create

init(抛出异常):

ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().buildServiceRegistry();
sessionFactory = new MetadataSources(
serviceRegistrY.addAnnotatedClass(Department.class).addAnnotatedClass(User.class).buildMetadata().buildSessionFactory();

在init处抛出异常:

org.hibernate.MappingException: Could not instantiate persister org.hibernate.persister.entity.SingleTableEntityPersister
    at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:174)
    at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:148)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:820)
    at org.hibernate.metamodel.source.internal.SessionFactoryBuilderImpl.buildSessionFactory(SessionFactoryBuilderImpl.java:65)
    at org.hibernate.metamodel.source.internal.MetadataImpl.buildSessionFactory(MetadataImpl.java:340)

我已经尝试添加其他一些注释,但不应该默认工作并创建表和外键吗?如果我从用户中删除部门,则表格生成正常。

提前致谢!

3 个答案:

答案 0 :(得分:0)

    @Entity
    public class User { 
     @Id
     private int id;
     public int getId() {return id;}
     public void setId(int id) {this.id = id;}

     @ManyToOne
     Department department;
     public Department getDepartment() {return department;}
     public void setDepartment(Department department) {this.department = department;}
    }

@Entity
public class Department {
 @Id
 private int departmentNumber;

 @OneToMany(mappedBy="department")
 private Set<User> user;
public Set<User> getUser() {
        return user;
    }
    public void setUser(Set<User> user) {
        this.user = user;
    }


 public int getDepartmentNumber() {return departmentNumber;}
 public void setDepartmentNumber(int departmentNumber) {this.departmentNumber = departmentNumber;}
}

您必须将一组添加到部门实体并将OneToMany关系映射到用户

答案 1 :(得分:0)

我的例子:

User.java

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User {
    private int id;
    private String userName;
    private String password;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Column
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

TraceLog.java

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class TraceLog {
    private int id;
    private User user;
    private String tokenId;
    private String variable;
    private String value;
    private Date traceTime;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @ManyToOne(cascade = CascadeType.ALL)
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Column
    public String getTokenId() {
        return tokenId;
    }

    public void setTokenId(String tokenId) {
        this.tokenId = tokenId;
    }

    @Column
    public String getVariable() {
        return variable;
    }

    public void setVariable(String variable) {
        this.variable = variable;
    }

    @Column
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Column
    public Date getTraceTime() {
        return traceTime;
    }

    public void setTraceTime(Date traceTime) {
        this.traceTime = traceTime;
    }
}

的hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sessiontest</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">mysql</property>
  <property name="hibernate.connection.pool_size">1</property>
  <property name="show_sql">true</property>
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.hbm2ddl.auto">update</property>

  <mapping class="com.cpviet.example.session.model.User" />
  <mapping class="com.cpviet.example.session.model.TraceLog" />

</session-factory>

</hibernate-configuration>

HibernateUtil.java

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

public class HibernateUtil {

    private SessionFactory sessionFactory = null;

    private static HibernateUtil instance = null; 

    private HibernateUtil() {
    }

    public static HibernateUtil getInstance() {
        if (instance == null) {
            instance = new HibernateUtil();
        }
        return instance;
    }

    public SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            Configuration configuration = new Configuration();
            configuration.configure();
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        }
        return sessionFactory;
    }

}

使用方法:

Session session = HibernateUtil.getInstance().getSessionFactory().openSession();
user = (User) session.get(User.class, (Integer)1);
session.close();

Session session = HibernateUtil.getInstance().getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();

TraceLog traceLog = new TraceLog();
traceLog.setTokenId(tokenId);
traceLog.setVariable("var1");
traceLog.setValue("val1");
traceLog.setUser(user);
traceLog.setTraceTime(new Date());

session.save(traceLog);

transaction.commit();
session.close();

答案 2 :(得分:0)

您正在使用尚未完成的功能。 org.hibernate.metamodel中的所有内容都定位到5.0。

http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/metamodel/package-summary.html