联合主键用Hibernate注解映射方式主要有三种:
第一、将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode,再将
该类注解为@Embeddable,最后在主类中(该类不包含联合主键类中的字段)保存该联合主键类的一个引用,并生成set和get方法,并将该引用注
解为@Id
第二、将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode,最后
在主类中(该类不包含联合主键类中的字段)保存该联合主键类的一个引用,并生成set和get方法,并将该引用注解为@EmbeddedId
第三、将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并要重写equals和hashcode.
最后在主类中(该类包含联合主键类中的字段)将联合主键字段都注解为@Id,并在该类上方将上这样的注解:@IdClass(联合主键类.class)
public class BmRoRolerightmatrixPK implements Serializable{ private static final long serialVersionUID = 1L; private Long actId;//功能操作ID private Long roleId;//角色ID @Column(name = "ACT_ID",unique = false,nullable = true,length = 10) public Long getActId() { return actId; } public void setActId(Long actId) { this.actId = actId; } @Column(name = "ROLE_ID",unique = false,nullable = true,length = 10) public Long getRoleId() { return roleId; } public void setRoleId(Long roleId) { this.roleId = roleId; } }
@Entity @IdClass(BmRoRolerightmatrixPK.class) @Table(name = "BM_RO_ROLERIGHTMATRIX") public class BmRoRolerightmatrix implements Serializable{ private static final long serialVersionUID = 1L; public BmRoRolerightmatrix(){} //属性 private Long actId;//功能操作ID private Long roleId;//角色ID @Id public Long getActId() { return this.actId; } @Id public Long getRoleId() { return this.roleId; } ...
public class WalletBalanceId implements Serializable{ /** * */ private static final long serialVersionUID = -6967634249077311858L; /*** * CID 客户号 */ @ManyToOne @JoinColumn(name = "CID") private Customer customer; /*** * 理财账号 */ @Column(name = "ACCT_NO") private String acctNo; public WalletBalanceId() { super(); } public WalletBalanceId(Customer customer, String acctNo) { super(); this.customer = customer; this.acctNo = acctNo; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } public String getAcctNo() { return acctNo; } public void setAcctNo(String acctNo) { this.acctNo = acctNo; } @Override public String toString() { return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE); } }
@Entity @Table(name = "T_WALLET_BALANCE") @IdClass(WalletBalanceId.class) public class WalletBalance implements Serializable { /** * */ private static final long serialVersionUID = -4041192003429561466L; /*** * CID(主键) 客户号 */ @Id @ManyToOne @JoinColumn(name = "CID") private Customer customer; /*** * 理财账号 */ @Id @Column(name = "ACCT_NO") private String acctNo; /*** * 总金额 */ @Column(name = "TOTAL_AMT") private BigDecimal totalAmt; /*** * 资金状态 */ @Column(name = "FUND_STATE") private String fundState; /*** * 更新日期 */ @Column(name = "UPD_DATE") private Date updDate; public WalletBalance() { super(); } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } public String getAcctNo() { return acctNo; } public void setAcctNo(String acctNo) { this.acctNo = acctNo; } public BigDecimal getTotalAmt() { return totalAmt; } public void setTotalAmt(BigDecimal totalAmt) { this.totalAmt = totalAmt; } public String getFundState() { return fundState; } public void setFundState(String fundState) { this.fundState = fundState; } public Date getUpdDate() { return updDate; } public void setUpdDate(Date updDate) { this.updDate = updDate; } @Override public String toString() { return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE); } }
时间: 2024-10-06 00:01:29