一对一映射与休眠/ JBoss / Seam [英] OneToOne Mapping with hibernate/JBoss/Seam

查看:139
本文介绍了一对一映射与休眠/ JBoss / Seam的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建Entity Customer和OptIn之间的一对一映射。 OptIn实体是可选的。
这就是为什么外键必须在OptIn中。在部署时,我得到以下错误,因为找不到
映射:



OneToOneSecondPass.java:135



值:
otherSide = optIn,mappedBy = customer

otherSideProperty = BinderHelper.findPropertyByName(otherSide,mappedBy);

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.buildMappings(Configuration.java:1115)
... $ / $>

我能做些什么才能得到正确的映射?

  @Table(name =KPS_OPT_IN,schema =EB)
public class OptIn extends KmsEntity implements java.io.Serializable {

private static最终长串serialVersionUID = - 8818445355079384264L;


private int id; / * kps_kunden_nr * /

私人客户客户;
$ b $ public OptIn(){
}

@Id
@Column(name =KPS_KUNDEN_NR,unique = true,nullable = false)
public int getId(){
return this.id;
}

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

$ b @OneToOne
@PrimaryKeyJoinColumn(name =KPS_KUNDEN_NR,referencedColumnName =KPS_KUNDEN_NR)
public Customer getCustomer(){
return customer ;
}

public void setCustomer(Customer customer){
this.customer = customer;
this.setId(customer.getId());







 @Entity 
@Table(name =KPS_KUNDEN,schema =EB)
public class Customer extends KmsEntity implements java.io.Serializable {

private static final long serialVersionUID = 1L;

private int id;

private OptIn optIn;
$ b $ public Customer(){
}

public Customer(int id){
this.id = id;
}

@Id
@GeneratedValue(generator =seqkpskunde)
@SequenceGenerator(name =seqkpskunde,sequenceName =SEQ_KPS_KUNDE)
@Column(name =KPS_KUNDEN_NR,unique = true,nullable = false)
public int getId(){
return this.id;
}

public void setId(int id){
this.id = id;
if(optIn!= null){
optIn.setId(id);

$
$ b @OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY,mappedBy =customer)
public OptIn getOptIn(){
返回optIn;
}

public void setOptIn(OptIn optIn){
this.optIn = optIn;
}
}


解决方案

I我不确定你的意思是外键必须在OptIn中。您已通过 @PrimaryKeyJoinColumn 映射您的 @OneToOne 关联,这意味着您的实体将通过其ID值进行关联。这也意味着:


  1. 不能在 @PrimaryKeyJoinColumn 注解;它们将取自两个实体上适当的 @Id 列。

  2. 将提取标记为 LAZY
  3. code>是毫无意义的,并且将被忽略;可选的@OneToOne关联始终总是热切地被获取。
  4. 在这个关联中, OptIn 是否在数据库中没有给定ID的条目。

您使用的是何种Hibernate / Annotations版本?如果它们很老,那可能是Hibernate代码中的一个错误。但是我相信如果你修正了上面的(1)和(2),它应该可以工作。


I want to create a one to one mapping between the Entity Customer and OptIn. The OptIn Entity is optional. That is why the foreign key must be in OptIn. At deployment I get the following error because the mapping can not be found:

OneToOneSecondPass.java:135

Values: otherSide= optIn, mappedBy=customer

otherSideProperty = BinderHelper.findPropertyByName( otherSide, mappedBy );

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.buildMappings(Configuration.java:1115) ...

What can I do to get a correct mapping?

@Entity
@Table(name = "KPS_OPT_IN", schema = "EB")
public class OptIn extends KmsEntity implements java.io.Serializable {

    private static final long serialVersionUID = -8818445355079384264L;


    private int id; /* kps_kunden_nr */

    private Customer customer;      

    public OptIn() {
    }

    @Id
    @Column(name = "KPS_KUNDEN_NR", unique = true, nullable = false)    
    public int getId() {
        return this.id;
    }

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

    @OneToOne
    @PrimaryKeyJoinColumn(name="KPS_KUNDEN_NR", referencedColumnName="KPS_KUNDEN_NR")   
    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
        this.setId(customer.getId());
    }    
}


@Entity
@Table(name = "KPS_KUNDEN", schema = "EB")
public class Customer extends KmsEntity implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    private int id; 

    private OptIn optIn;

    public Customer() {
    }

    public Customer(int id) {
        this.id = id;
    }

    @Id
    @GeneratedValue(generator="seqkpskunde")
    @SequenceGenerator(name="seqkpskunde",sequenceName="SEQ_KPS_KUNDE") 
    @Column(name = "KPS_KUNDEN_NR", unique = true, nullable = false)
    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
        if(optIn!=null){
            optIn.setId(id);
        }
    }

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "customer")
    public OptIn getOptIn() {
        return optIn;
    }

    public void setOptIn(OptIn optIn) {
        this.optIn = optIn;
    }
}

解决方案

I'm not sure what you mean by "foreign key must be in OptIn". You've mapped your @OneToOne association via @PrimaryKeyJoinColumn which means your entities will be linked via their ID values. It also means that:

  1. You can't specify column names within @PrimaryKeyJoinColumn annotation; they will be taken from appropriate @Id columns on both entities instead.
  2. Marking fetch as LAZY is pointless and going to be ignored; optional @OneToOne associations are always eagerly fetched.
  3. The only way OptIn would be optional on this association is if there was no entry with given ID in the database.

What Hibernate / Annotations versions are you using? If they're rather old it could be a bug in Hibernate code. But I believe it should work if you fix (1) and (2) above.

这篇关于一对一映射与休眠/ JBoss / Seam的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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