如何使用hibernate 4.1.10.Final插入? [英] How to insert using hibernate 4.1.10.Final?

查看:97
本文介绍了如何使用hibernate 4.1.10.Final插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用hibernate 4.1.10.Final将数据插入到数据库中,但它会抛出以下异常:

我有三个表,即开发地址。 / p>

开发本身具有地址对象。

  INFO:HCANN000001 :Hibernate Commons Annotations {4.0.1.Final} 
INFO:HHH000412:Hibernate Core {4.1.10.Final}
INFO:HHH000206:找不到hibernate.properties
INFO:HHH000021:Bytecode提供程序名称:javassist
INFO:HHH000043:从资源配置:/hibernate.cfg.xml
INFO:HHH000040:配置资源:/hibernate.cfg.xml
INFO:HHH000041:配置的SessionFactory: null
SEVERE:java.lang.NullPointerException $ b $ org.hibernate.mapping.PersistentClass.createPrimaryKey(PersistentClass.java:327)
at org.hibernate.cfg.CreateKeySecondPass.doSecondPass(CreateKeySecondPass。 java:48)
在org.hibernate.cfg.Configuration.processSecondPassesOfType(Configuration.java:1386)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1341)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1730)
at com.myproject .util.HibernateUtil.configureSessionFactory(HibernateUtil.java:23)
at com.myproject.util.HibernateUtil.getSessionFactory(HibernateUtil.java:34)
at com.myproject.model.ConstructionModel.addDevelopment(ConstructionModel .java:185)
at com.myproject.controller.Construction.addDevelopment(Construction.java:135)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect .NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601 )
......

我的密码



development.java

  @Entity 
@Table(name =development)
public class Development实现Serializable {

private int id;
私人地址;

public Development(){

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

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


@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
public Address getAddress(){
return this.address;
}

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


}

address.java

  @Entity 
@Table(name =address)
public class Address实现Serializable {

private int id;
私人字符串单位;

公共地址(){

}


@Id
@GeneratedValue
@OneToOne(mappedBy =address)
@JoinColumn(name =addid)
public int getId(){
return id;


$ b @Column(name =unit)
public String getUnit(){
return unit;
}

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


public void setUnit(String unit){
this.unit = unit;
}


}


解决方案

删除 @GeneratedValue @OneToOne(mappedBy =development)@JoinColumn(name =Developer) @GeneratedValue @OneToOne(mappedBy =address)@JoinColumn(name =addid)来自开发人员和地址的Id字段。


I am using hibernate 4.1.10.Final to insert data into database, but it throws the following exception,

I have three tables, development and address .

Development has an object of address in itself.

INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
INFO: HHH000412: Hibernate Core {4.1.10.Final}
INFO: HHH000206: hibernate.properties not found
INFO: HHH000021: Bytecode provider name : javassist
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
INFO: HHH000041: Configured SessionFactory: null
SEVERE: java.lang.NullPointerException
    at org.hibernate.mapping.PersistentClass.createPrimaryKey(PersistentClass.java:327)
    at org.hibernate.cfg.CreateKeySecondPass.doSecondPass(CreateKeySecondPass.java:48)
    at org.hibernate.cfg.Configuration.processSecondPassesOfType(Configuration.java:1386)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1341)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1730)
    at com.myproject.util.HibernateUtil.configureSessionFactory(HibernateUtil.java:23)
    at com.myproject.util.HibernateUtil.getSessionFactory(HibernateUtil.java:34)
    at com.myproject.model.ConstructionModel.addDevelopment(ConstructionModel.java:185)
    at com.myproject.controller.Construction.addDevelopment(Construction.java:135)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
      ......

My code

development.java

@Entity
@Table(name="development")
public class Development implements Serializable{

    private int id;
    private Address address;

    public Development(){

    }
    @Id
    @GeneratedValue
    @Column(name="id")
    public int getId() {
        return id;
    }

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

    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn 
    public Address getAddress() {
        return this.address;
    }

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


}

address.java

@Entity
@Table(name="address")
public class Address implements Serializable{

    private int id;
    private String unit;

    public Address(){

    }


    @Id
    @GeneratedValue
    @OneToOne(mappedBy="address")
    @JoinColumn(name="addid")
    public int getId() {
        return id;
    }


    @Column (name="unit")
    public String getUnit() {
        return unit;
    }

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


    public void setUnit(String unit) {
        this.unit = unit;
    }


}

解决方案

Remove @GeneratedValue @OneToOne(mappedBy="development") @JoinColumn(name="Developer") and @GeneratedValue @OneToOne(mappedBy="address") @JoinColumn(name="addid") from Id fields of Developer and Address.

这篇关于如何使用hibernate 4.1.10.Final插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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