日期:2014-05-16  浏览次数:20427 次

hibernate 级联配置
Hibernate 复合主键级联操作配置:


@Audited
@Entity
@Table(name = "REFERENCE_CODE")
@NamedQueries({
@NamedQuery(name = "ReferenceCode.findAll", query = "SELECT p FROM ReferenceCode p"),
@NamedQuery(name = "ReferenceCode.findbyRefcode", query = "SELECT code FROM ReferenceCode code WHERE  "
+ "code.refCode = :refCode AND code.referenceCodeDomain.codeType = :codeType AND code.publisherId = :publisherId"),
@NamedQuery(name = "ReferenceCode.findbyCodeTypeAndPublisherID", query = "SELECT code FROM ReferenceCode code WHERE  "
+ "code.referenceCodeDomain.codeType = :codeType AND code.publisherId = :publisherId"),
@NamedQuery(name = "ReferenceCode.findbyCodeType", query = "SELECT rc FROM ReferenceCode rc WHERE rc.referenceCodeDomain.codeType = :codeType") })
@Cache(region = "ReferenceCode", usage = CacheConcurrencyStrategy.READ_WRITE)
@Cacheable(value = true)
public class ReferenceCode extends BaseEntity {

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = false)
@JoinColumns( {
@JoinColumn(name = "CODE_ID", referencedColumnName = "CODE_ID", insertable = false, updatable = false) })
@Cache(region = "com.gs.nisystem.dao.infra.entity.ReferenceCodeLang", usage = CacheConcurrencyStrategy.READ_WRITE)
private List<ReferenceCodeLang> langs;
}

//关联列
@Entity
@Table(name = "REFERENCE_CODE_LANG")
@NamedQueries({ @NamedQuery(name = "ReferenceCodeLang.findAll", query = "SELECT p FROM ReferenceCodeLang p") })
@Cache(region = "ReferenceCodeLang", usage = CacheConcurrencyStrategy.READ_WRITE)
@Cacheable(value = true)
public class ReferenceCodeLang extends BaseEntity {

/** The Constant serialVersionUID. */
private static final long serialVersionUID = 8083320578186003078L;

/** id property, it maps the composite primary key in REFERENCE_CODE_LANG table. */
@EmbeddedId
@AttributeOverrides({ @AttributeOverride(name = "referenceCode", column = @Column(name = "CODE_ID", nullable = false, precision = 13, scale = 0)),
@AttributeOverride(name = "lang", column = @Column(name = "LANG", nullable = false, precision = 13, scale = 0)) })
private ReferenceCodeLangPK id;
}

//复合主键:

@Embeddable
public class ReferenceCodeLangPK implements Serializable {

/** The Constant serialVersionUID. */
private static final long serialVersionUID = -1564044758129864184L;

/** The reference code. */
@Column(name = "CODE_ID", nullable = false, precision = 13, scale = 0)
private Long referenceCode;

/** The lang. */
@Column(name = "LANG", length = 6)
private String lang;
}