JPA2 / Hibernate部分复合主要作为一对多关系中的外键

时间:2015-10-29 08:46:52

标签: java hibernate jpa jpa-2.0 hibernate-mapping

我无法将外键保留在桌面上,我认为这可能是一个映射问题。我正在使用JPA2 / Hibernate4

数据模型(不可更改)如下:

OBJECTIVES
==========
INTCODE (PK)
INTOBJECTIVE (PK)
DESCRIPTION

REGISTER
========
INTCODE (PK)
INTREGISTER (PK)    
...
INTOBJECTIVE (FK from Objectives)

正如您在数据模型中看到的,我在OBJECTIVES表(INTCODE,INTOBJECTIVE)中有一个复合主键,并且只有一个字段(INTOBJECTIVE)是REGISTER表中的外键。

我的JPA映射条款如下:

目标类

@Entity
@Table(name = "OBJECTIVES", uniqueConstraints = @UniqueConstraint(columnNames = "INTOBJECTIVE"))
public class Objectives implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    @AttributeOverrides({ @AttributeOverride(name = "intcode", column = @Column(name = "INTCODE", nullable = false, length = 2)),
    @AttributeOverride(name = "intobjective", column = @Column(name = "INTOBJECTIVE", unique = true, nullable = false, precision = 22, scale = 0)) })
    private ObjectivesId id; 
    @Column(name = "DESCRIPTION")
    private String description;
    @OneToMany(mappedBy="objective", fetch=FetchType.LAZY, cascade=CascadeType.PERSIST)
    private List<Register> registers;

    // Constructors, getters and setters ...

目标键类

@Embeddable
public class ObjectivesId implements Serializable {

   private static final long serialVersionUID = 1L;

   @Column(name = "INTCODE", nullable = false, length = 2)
   private String intcode;
   @Column(name = "INTOBJECTIVE", unique = true, nullable = false, precision = 22, scale = 0)
   private BigDecimal intobjective;
   // Constructor, getters and setters, ...

注册课程

@Entity
@Table(name="REGISTER", uniqueConstraints = @UniqueConstraint(columnNames = "INTREGISTER"))
public class Register {

   @EmbeddedId
   @AttributeOverrides({ @AttributeOverride(name = "intcode", column = @Column(name = "INTCODE", nullable = false, length = 2)),
   @AttributeOverride(name = "intregister", column = @Column(name = "INTREGISTER", unique = true, nullable = false, precision = 22, scale = 0)) })
   private RegisterId id;

   @ManyToOne
   @JoinColumns({
      @JoinColumn(name="INTCODE", referencedColumnName="INTCODE", insertable=false, updatable=false),
      @JoinColumn(name="INTOBJECTIVE", referencedColumnName="INTOBJECTIVE", insertable=false, updatable=false)
   })
   private Objectives objective;

   // Constructor, getter and setters, ...

注册密钥类

@Embeddable
public class RegisterId implements Serializable {

   private static final long serialVersionUID = 1L;

   @Column(name = "INTCODE", nullable = false, length = 2)
   private String intcode;
   @Column(name = "INTREGISTER", unique = true, nullable = false, precision = 22, scale = 0)
   private BigDecimal intregister;
   // Constructor, getters and setters, ...

在Hibernate4中执行此操作是否可行?

0 个答案:

没有答案