hibernate 注解 复合主键
@Entity
@Table(name = "TB_INDEX_LEFTTREE", schema = "CENDOC")
public class IndexCatalog implements java.io.Serializable {
private String sjlsh;
private String yljgdm;
private String ywybzf;
@Id
@Column(name = "SJLSH", nullable = false, length = 64)
public String getSjlsh() {
return this.sjlsh;
}
public void setSjlsh(String sjlsh) {
this.sjlsh = sjlsh;
}
@Id
@Column(name = "YLJGDM", nullable = false, length = 22)
public String getYljgdm() {
return this.yljgdm;
}
public void setYljgdm(String yljgdm) {
this.yljgdm = yljgdm;
}
@Id
@Column(name = "YWYBZF", nullable = false, length = 16)
public String getYwybzf() {
return this.ywybzf;
}
public void setYwybzf(String ywybzf) {
this.ywybzf = ywybzf;
}
复合主键 这样写 也行??? 我从来没这么写过 今天误打误撞 这么写了 尽然 也成功了!! 请问大神 这么写 有没有问题??? 展开
这样对整个程序的可读性不好。@SuppressWarnings("serial")
建议你改成只有1个Id的模式。
即
@Entity
@Table(name = "TB_INDEX_LEFTTREE", schema = "CENDOC")
public class IndexCatalog implements java.io.Serializable {
private CatalogID id;
public IndexCatalog() {
}
/** full constructor */
public IndexCatalog(CatalogID id) {
this.id = id;
}
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name="SJLSH", column=@Column(name="SJLSH", nullable=false, length=64) ),
@AttributeOverride(name="YLJGDM", column=@Column(name="YLJGDM", nullable=false, length=22) ),
@AttributeOverride(name="YWYBZF", column=@Column(name="YWYBZF", nullable=false,length=16) )
public CatalogID getId() {
return this.id;
}
public void setId(CatalogID id) {
this.id = id;
}
}
然后再写一个ID类
@Embeddable
public class CatalogID implements java.io.Serializable {
private String sjlsh;
private String yljgdm;
private String ywybzf;
@Column(name = "SJLSH", nullable = false, length = 64)
public String getSjlsh() {
return this.sjlsh;
}
public void setSjlsh(String sjlsh) {
this.sjlsh = sjlsh;
}
@Column(name = "YLJGDM", nullable = false, length = 22)
public String getYljgdm() {
return this.yljgdm;
}
public void setYljgdm(String yljgdm) {
this.yljgdm = yljgdm;
}
@Column(name = "YWYBZF", nullable = false, length = 16)
public String getYwybzf() {
return this.ywybzf;
}
public void setYwybzf(String ywybzf) {
this.ywybzf = ywybzf;
}
}
这样整个的阅读性会更好。