在具有一对多关系的DB中持久保存JPA对象时出现异常

时间:2015-11-21 10:04:39

标签: jpa

你在图片表a和表b中有两个表如下:

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

    @Id
    @SequenceGenerator(name = "JOURNAL_CATEGORY_ID_GENERATOR", allocationSize = 1, sequenceName = "clm_jounal_category_config_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "JOURNAL_CATEGORY_ID_GENERATOR")
    @Column(name = "CLAIM_ID")
    private String claimId;

    @Column(name = "name")
    private String name;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "country")
    private List<ClaimDTLS> claimDetails;
}

B Primary Key:

@Embeddable
public class BPK implements Serializable {

    @Column(name = "code")
    private String code;

    @Column(name = "CLAIM_ID")
    private String claimId;
}

B Entity:

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

    @EmbeddedId
    protected BPK bpk;


    @Column(name = "name")
    private String name;

    @MapsId("country_code")
    @JoinColumn(name = "claimId", referencedColumnName = "claimId", insertable = false, updatable = false)
    @ManyToOne
    private A a;
}

当我尝试在Db中保留A类型的对象时,表b的声明id的值未设置并且初始化为零。     表A的主键也是用oracle序列生成的。

any help will be welcomed.
thanks in advance

1 个答案:

答案 0 :(得分:0)

序列值是数字,当JPA将它们用作生成器时,它调用实体PK的setter方法。现在,您在使用序列时将PK定义为字符串,因此无法找到匹配的setter。将PK的类型更改为Long并且事情可以正常工作