我正在尝试使用Hibernate保存使用查找表(lookupvalue)的父实体(兴趣)。当我持久感兴趣对象(em.persists(interest)
)时,所有列都被保存,但categoryLookupValue.Any线索除外?
以下是表格的详细信息
CREATE TABLE interest.interest
(
id bigint NOT NULL,
interestname character varying NOT NULL,
summary character varying NOT NULL,
legislation character varying,
description character varying,
categorylookupvalue integer,<= This column is not saved
dictionarystartdate date,
pirstartdate date,
pirenddate date,
affectpropertybeforedetail character varying,
affectpropertyafterdetail character varying,
CONSTRAINT pk_interest PRIMARY KEY (id),
CONSTRAINT fk_interest_category FOREIGN KEY (categorylookupvalue)
REFERENCES reference.lookupvalue (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
CREATE TABLE reference.lookupdomain
(
id integer NOT NULL,
name character varying(100) NOT NULL,
CONSTRAINT pk_lookupdomain PRIMARY KEY (id)
)
CREATE TABLE reference.lookupvalue
(
id integer NOT NULL,
lookupvalue_lookupdomain_id integer NOT NULL,
domaincode character varying NOT NULL,
domainvalue character varying NOT NULL,
CONSTRAINT pk_lookupvalue PRIMARY KEY (id),
CONSTRAINT fk_lokupvalue_lookupdomain_id FOREIGN KEY (lookupvalue_lookupdomain_id)
REFERENCES reference.lookupdomain (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
这是实体
public class Interest implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq")
@SequenceGenerator(name = "id_seq", sequenceName = "id_seq")
private Long id;
@NotNull
private String interestName;
@Basic
private String description;
// Do not cascade as LookupValue is just for lookup
@OneToOne(fetch = FetchType.EAGER, optional = false)
// set insertable = false, updatable = false to ensure that JPA doesnt try to update/delete lookup table
@JoinColumn(name = "categorylookupvalue",referencedColumnName = "id",insertable = false, updatable = false,nullable = false)
private LookupValue categoryLookupValue;
..other fileds
}
public class LookupValue implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq")
@SequenceGenerator(name = "id_seq", sequenceName = "id_seq")
private Long id;
@Column(name = "lookupvalue_lookupdomain_id")
private Long lookupvalue_lookupdomain_id;
@Column(name = "domaincode")
private String domainCode;
@Column(name = "domainvalue")
private String domainValue;
}
public class LookupDomain implements Serializable {
@Id
@GeneratedValue(generator = "id_seq")
@SequenceGenerator(name = "id_seq", sequenceName = "id_seq")
private Long id;
@Column(name = "name")
private String name;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "lookupvalue_lookupdomain_id", referencedColumnName = "id", insertable = false, updatable = false)
})
private List<LookupValue> lookupValue = new ArrayList<>();
}