hibernate 注解 复合主键

@SuppressWarnings("serial")@Entity@Table(name="TB_INDEX_LEFTTREE",schema="CENDOC")pub... @SuppressWarnings("serial")
@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;
}

复合主键 这样写 也行??? 我从来没这么写过 今天误打误撞 这么写了 尽然 也成功了!! 请问大神 这么写 有没有问题???
展开
 我来答
jiangyuhanxht
推荐于2018-04-13 · TA获得超过218个赞
知道答主
回答量:93
采纳率:100%
帮助的人:71.5万
展开全部
这样对整个程序的可读性不好。
建议你改成只有1个Id的模式。
@SuppressWarnings("serial")

@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;
}


}


这样整个的阅读性会更好。

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式