读书人

Hibernate annotation many to one多对

发布时间: 2013-07-16 22:38:05 作者: rapoo

Hibernate annotation many to one多对一的不明错误?
各位大哥,求救阿~
我有两个表,其中一个是含复合主键,在查询时候会一直出现下面的错误讯息...
我想应该是annotation错误,可是自己就是怎麽都看不出来哪边错?
不好意思请路过大侠解救....感谢感谢...


Column HA0501 not in table KZ01 in TESTDB.


KZ01表(ManytoOne)

/**
* Kz01 entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "KZ01", schema = "TESTDB", catalog = "TEST")
public class Kz01 implements java.io.Serializable {

// Fields

private Kz01Id id;
private String kmemo;


/** default constructor */
public Kz01() {
}

/** full constructor */
public Kz01(Kz01Id id, String kmemo) {
this.id = id;
this.kmemo = kmemo;
}
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH},fetch = FetchType.LAZY)
@JoinColumn(name = "id.kzpem")
private Ha0501 ha0501;
public Ha0501 getHa0501() {
return ha0501;
}

public void setHa0501(Ha0501 ha0501) {
this.ha0501 = ha0501;
}

// Property accessors
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "kzpem", column = @Column(name = "KZPEM", nullable = false, precision = 7, scale = 0)),
@AttributeOverride(name = "kzyer", column = @Column(name = "KZYER", nullable = false, precision = 4, scale = 0)),
@AttributeOverride(name = "kztyp", column = @Column(name = "KZTYP", nullable = false, length = 2)),
@AttributeOverride(name = "kzrno", column = @Column(name = "KZRNO", nullable = false, length = 14)) })
public Kz01Id getId() {
return this.id;
}

public void setId(Kz01Id id) {
this.id = id;
}

@Column(name = "KMEMO", nullable = false, length = 42)
public String getKmemo() {
return this.kmemo;
}

public void setKmemo(String kmemo) {
this.kmemo = kmemo;
}
}


Kz01Id是myeclipse生成的

/**
* Kz01Id entity. @author MyEclipse Persistence Tools


*/
@Embeddable
public class Kz01Id implements java.io.Serializable {

// Fields

private Integer kzpem;
private Short kzyer;
private String kztyp;
private String kzrno;

// Constructors

/** default constructor */
public Kz01Id() {
}

/** full constructor */
public Kz01Id(Integer kzpem, Short kzyer, String kztyp, String kzrno) {
this.kzpem = kzpem;
this.kzyer = kzyer;
this.kztyp = kztyp;
this.kzrno = kzrno;
}

// Property accessors

@Column(name = "KZPEM", nullable = false, precision = 7, scale = 0)
public Integer getKzpem() {
return this.kzpem;
}

public void setKzpem(Integer kzpem) {
this.kzpem = kzpem;
}

@Column(name = "KZYER", nullable = false, precision = 4, scale = 0)
public Short getKzyer() {
return this.kzyer;
}

public void setKzyer(Short kzyer) {
this.kzyer = kzyer;
}

@Column(name = "KZTYP", nullable = false, length = 2)
public String getKztyp() {
return this.kztyp;
}

public void setKztyp(String kztyp) {
this.kztyp = kztyp;
}

@Column(name = "KZRNO", nullable = false, length = 14)
public String getKzrno() {
return this.kzrno;
}

public void setKzrno(String kzrno) {
this.kzrno = kzrno;
}

public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof Kz01Id))
return false;
Kz01Id castOther = (Kz01Id) other;

return ((this.getKzpem() == castOther.getKzpem()) || (this.getKzpem() != null
&& castOther.getKzpem() != null && this.getKzpem().equals(
castOther.getKzpem())))
&& ((this.getKzyer() == castOther.getKzyer()) || (this
.getKzyer() != null && castOther.getKzyer() != null && this
.getKzyer().equals(castOther.getKzyer())))
&& ((this.getKztyp() == castOther.getKztyp()) || (this


.getKztyp() != null && castOther.getKztyp() != null && this
.getKztyp().equals(castOther.getKztyp())))
&& ((this.getKzrno() == castOther.getKzrno()) || (this
.getKzrno() != null && castOther.getKzrno() != null && this
.getKzrno().equals(castOther.getKzrno())));
}

public int hashCode() {
int result = 17;

result = 37 * result
+ (getKzpem() == null ? 0 : this.getKzpem().hashCode());
result = 37 * result
+ (getKzyer() == null ? 0 : this.getKzyer().hashCode());
result = 37 * result
+ (getKztyp() == null ? 0 : this.getKztyp().hashCode());
result = 37 * result
+ (getKzrno() == null ? 0 : this.getKzrno().hashCode());
return result;
}

}



Ha0501表(OnetoMany)

/**
* Ha0501 entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "HA0501", schema = "TESTDB", catalog = "TEST")
public class Ha0501 implements java.io.Serializable {

// Fields

private Integer hpemn;
private String hname;
private Set <Kz01> kz01;

// Constructors

/** default constructor */
public Ha0501() {
}

/** full constructor */
public Ha0501(Integer hpemn, String hname) {
this.hpemn = hpemn;
this.hname = hname;
}

// Property accessors
@Id
@Column(name = "HPEMN", nullable = false, precision = 7, scale = 0)
public Integer getHpemn() {
return this.hpemn;
}

public void setHpemn(Integer hpemn) {
this.hpemn = hpemn;
}

@Column(name = "HNAME", nullable = false, length = 10)
public String getHname() {
return this.hname;
}
@OneToMany(cascade={CascadeType.REMOVE},fetch=FetchType.EAGER,mappedBy = "id.kzpem")

public Set<Kz01> getKz01() {
return kz01;
}

public void setKz01(Set<Kz01> kz01) {
this.kz01 = kz01;
}




public void setHname(String hname) {
this.hname = hname;
}

}



透过spring mvc执行
下面的code,写了好多就是跑不出SQL,郁闷阿

@SuppressWarnings("unchecked")
public List<Kz01> getAllKz01()
{

Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Kz01.class);
////上面测试code都没用会跑出Column HA0501 not in table KZ01 in TESTDB的错误./////////
return criteria.list();//原始用法

}

我的目的想要产生以KZ01为主LEFT JOIN Ha0501
麻烦诸位帮忙看看...

Thanks a lot!




[解决办法]
mappedBy = "id.kzpem" 应该是ha0501

为什么要用这个联合主键呢?好好看看设计,这种场景很少的
[解决办法]
没用过annotation。
“Column HA0501 not in table KZ01 in TESTDB”, 表KZ01 中没有字段HA0501 错误! 这是你报的错误。
HA0501 这应该是个表, 不是个字段用于作为关联的, 应该是你再关联的地方出错了。

读书人网 >J2EE开发

热点推荐