如何解决jpa和休眠一对一的doSecondPass异常 [英] how to solve jpa and hibernate one to one annotion with doSecondPass Exception

查看:167
本文介绍了如何解决jpa和休眠一对一的doSecondPass异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在编写简单的代码,使用jpa和Hibernate 3.2版本将数据插入到两个表中。我的不足之处是将员工详细信息保存在两个表中,即包含employeeId的Employee,这是手动分配赋值的主键和Employee Address使用@onetoOne annotions共享Employee表中的主键employeeId。我的类lool像

  @Entity 
@Table (name =employee,catalog =test,uniqueConstraints = {})
public class Employee实现java.io.Serializable {

// Fields

私人Integer employeeid;

私人字符串名称;

私人Employeeaddress employeeaddress;
//构造函数

/ **默认构造函数* /
public Employee(){
}

/ **最小构造函数* /
public Employee(Integer employeeid){
this.employeeid = employeeid;

$ b $ **完全构造函数* /
public Employee(Integer employeeid,String name){
this.employeeid = employeeid;
this.name = name;
}

//属性访问器
@Id
@Column(name =EMPLOYEEID,unique = true,nullable = false,insertable = true,updatable = true)
public Integer getEmployeeid(){
return this.employeeid;
}

public void setEmployeeid(Integer employeeid){
this.employeeid = employeeid;


@Column(name =NAME,unique = false,nullable = true,insertable = true,updatable = true,length = 45)
public String getName ){
return this.name;
}

public void setName(String name){
this.name = name;

$ b $ @ @OneToOne(mappedBy =employee)
@PrimaryKeyJoinColumn
public Employeeaddress getEmployeeaddress(){
return employeeaddress;
}

public void setEmployeeaddress(Employeeaddress employeeaddress){
this.employeeaddress = employeeaddress;



$ b @Entity
@Table(name =employeeaddress,catalog =test,uniqueConstraints = {})
public class Employeeaddress实现java.io.Serializable {

// Fields

private Integer employeeid;

私有字符串地址;

私人雇员雇员;
//构造函数

/ **默认构造函数* /
public Employeeaddress(){
}

/ **最小构造函数* /
public Employeeaddress(Integer employeeid){
this.employeeid = employeeid;
}

/ **完整构造函数* /
public Employeeaddress(Integer employeeid,String address){
this.employeeid = employeeid;
this.address = address;
}

//属性访问器
@Id
@Column(name =EMPLOYEEID,unique = true,nullable = false,insertable = true,updatable = true)
public Integer getEmployeeid(){
return this.employeeid;
}

public void setEmployeeid(Integer employeeid){
this.employeeid = employeeid;
}

@Column(name =ADDRESS,unique = false,nullable = true,insertable = true,updatable = true,length = 45)
public String getAddress ){
返回this.address;
}

public void setAddress(String address){
this.address = address;

$ b @OneToOne(mappedBy =employeeaddress)
@PrimaryKeyJoinColumn
public Employee getEmployee(){
return employee;
}

public void setEmployee(Employee employee){
this.employee = employee;
}

}

我的主类是 p>

  public class EmployeeDao {
public static void main(String [] args){
try {
AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.addAnnotatedClass(employeeforms.Employee.class);
cfg.addAnnotatedClass(employeeforms.Employeeaddress.class);
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Employee e = new Employee();
e.setEmployeeid(100872);
e.setName(sandeep);
Employeeaddress ed = new Employeeaddress();
ed.setAddress(Hyderabad);
e.setEmployeeaddress(ed);
session.save(e);
} catch(Exception e){
e.printStackTrace();

}

}

当我运行我的主类我得到以下异常

  java.lang.NullPointerException $ b $在org.hibernate.cfg.OneToOneSecondPass。在org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)上
(org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296))中的

(OneToOneSecondPass.java:135)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
at employeeDao.EmployeeDao.main(EmployeeDao.java:16)

我的表格是

  CREATE TABLE employee(
EMPLOYEEID int(10)unsigned NOT NULL,
NAME varchar(45)default'',
PRIMARY KEY(EMPLOYEEID`)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;

CREATE TABLE employeeaddress(
EMPLOYEEID int(10)unsigned NOT NULL默认值为'0',
ADDRESS varchar(45)default'',
PRIMARY KEY(EMPLOYEEID )
)ENGINE = InnoDB
DEFAULT CHARSET = latin1;


解决方案

您至少有三个问题。第一个问题:你指定

  @OneToOne(mappedBy =employee )
@PrimaryKeyJoinColumn

  @OneToOne(mappedBy =employeeaddress)
@PrimaryKeyJoinColumn

这两个注释是矛盾的;如果设置 mappedBy ,则意味着映射信息位于另一侧。只有一方应该定义映射( @PrimaryKeyJoinColumn),另一方应该设置 mappedBy 属性。



第二个问题:您指定

  Employeeaddress ed = new Employeeaddress( ); 
ed.setAddress(Hyderabad);
e.setEmployeeaddress(ed);

协会的双方都应该初始化。代码应该这样做:

  e.setEmployeeaddress(ed); 
ed.setEmployee(e);

Hibernate使用mappedBy not 设置的边来知道关联存在。



第三个问题:

您保存员工,但不保存地址。从员工到地址都没有级联。

Hi I am writing simple code that insert data into two tables using jpa and Hibernate 3.2 version.My inetention is to save Employee details in two tables i.e Employee which contains employeeId which is primary key that is manually assigned assigned value and Employee Address that share the primary key employeeId in Employee table using @onetoOne annotions.My classes lool like

@Entity
@Table(name = "employee", catalog = "test", uniqueConstraints = {})
public class Employee implements java.io.Serializable {

    // Fields

    private Integer employeeid;

    private String name;

    private Employeeaddress employeeaddress;
    // Constructors

    /** default constructor */
    public Employee() {
    }

    /** minimal constructor */
    public Employee(Integer employeeid) {
        this.employeeid = employeeid;
    }

    /** full constructor */
    public Employee(Integer employeeid, String name) {
        this.employeeid = employeeid;
        this.name = name;
    }

    // Property accessors
    @Id
    @Column(name = "EMPLOYEEID", unique = true, nullable = false, insertable = true, updatable = true)
    public Integer getEmployeeid() {
        return this.employeeid;
    }

    public void setEmployeeid(Integer employeeid) {
        this.employeeid = employeeid;
    }

    @Column(name = "NAME", unique = false, nullable = true, insertable = true, updatable = true, length = 45)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @OneToOne(mappedBy="employee")
    @PrimaryKeyJoinColumn
    public Employeeaddress getEmployeeaddress() {
        return employeeaddress;
    }

    public void setEmployeeaddress(Employeeaddress employeeaddress) {
        this.employeeaddress = employeeaddress;
    }
}


@Entity
@Table(name = "employeeaddress", catalog = "test", uniqueConstraints = {})
public class Employeeaddress implements java.io.Serializable {

    // Fields

    private Integer employeeid;

    private String address;

    private Employee employee;
    // Constructors

    /** default constructor */
    public Employeeaddress() {
    }

    /** minimal constructor */
    public Employeeaddress(Integer employeeid) {
        this.employeeid = employeeid;
    }

    /** full constructor */
    public Employeeaddress(Integer employeeid, String address) {
        this.employeeid = employeeid;
        this.address = address;
    }

    // Property accessors
    @Id
    @Column(name = "EMPLOYEEID", unique = true, nullable = false, insertable = true, updatable = true)
    public Integer getEmployeeid() {
        return this.employeeid;
    }

    public void setEmployeeid(Integer employeeid) {
        this.employeeid = employeeid;
    }

    @Column(name = "ADDRESS", unique = false, nullable = true, insertable = true, updatable = true, length = 45)
    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @OneToOne(mappedBy="employeeaddress")
    @PrimaryKeyJoinColumn
    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

}

and my main class is

public class EmployeeDao {
    public static void main(String[] args) {
        try{
        AnnotationConfiguration cfg = new AnnotationConfiguration();
        cfg.addAnnotatedClass(employeeforms.Employee.class);
        cfg.addAnnotatedClass(employeeforms.Employeeaddress.class);
        SessionFactory sessionFactory = cfg.buildSessionFactory();
        Session session =sessionFactory.openSession();
        Employee e = new Employee();
        e.setEmployeeid(100872);
        e.setName("sandeep");
        Employeeaddress ed = new Employeeaddress();     
        ed.setAddress("Hyderabad");
        e.setEmployeeaddress(ed);
        session.save(e);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

}

when i run my main class i get below exceptions

java.lang.NullPointerException
    at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:135)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
    at employeeDao.EmployeeDao.main(EmployeeDao.java:16)

my tables are

    CREATE TABLE  employee (
      EMPLOYEEID int(10) unsigned NOT NULL ,
      NAME varchar(45) default '',
      PRIMARY KEY  (EMPLOYEEID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    CREATE TABLE  employeeaddress (
      EMPLOYEEID int(10) unsigned NOT NULL default '0',
      ADDRESS varchar(45) default '',
      PRIMARY KEY  (EMPLOYEEID)
    ) ENGINE=InnoDB 
DEFAULT CHARSET=latin1;

解决方案

You have (at least) three problems.

First problem: you specify

@OneToOne(mappedBy="employee")
@PrimaryKeyJoinColumn

and

@OneToOne(mappedBy="employeeaddress")
@PrimaryKeyJoinColumn

Those two annotations are contradictory; if mappedBy is set, it means that the mapping information is on the other side. Only one side should define the mapping (the @PrimaryKeyJoinColumn), and the other side should set the mappedBy attribute.

Second problem: you specify

Employeeaddress ed = new Employeeaddress();     
ed.setAddress("Hyderabad");
e.setEmployeeaddress(ed);

Both sides of the associations should be initialized. The code should thus do:

e.setEmployeeaddress(ed);
ed.setEmployee(e);

Hibernate uses the side where mappedBy is not set to know if an association exists.

Third problem:

You save the employee, but don't save the address. And there is no cascade set from employee to address.

这篇关于如何解决jpa和休眠一对一的doSecondPass异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆