0

Hibernate4.1.1.Finalの使用。

@ManyToOneを追加しようとすると、スキーマの作成が次のように失敗します:org.hibernate.MappingException:永続化機能をインスタンス化できませんでした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)

他の注釈を追加しようとしましたが、デフォルトが機能してテーブルと外部キーを作成するべきではありませんか?ユーザーから部門を削除すると、テーブルは正常に生成されます。

前もって感謝します!

4

3 に答える 3

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関係をマップする必要があります

于 2012-05-31T10:12:18.483 に答える
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();
于 2012-05-31T10:37:41.367 に答える
0

まだ完成していない機能を使用しています。org.hibernate.metamodelのすべてが5.0をターゲットにしています。

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

于 2012-05-31T11:49:32.627 に答える