我无法将外键保留在桌面上,我认为这可能是一个映射问题。我正在使用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中执行此操作是否可行?