我有2个实体通过连接注释连接,一切正常,除了奇怪的查询结果。EnityManager查询返回一个奇怪的无法访问的对象
所以我有这个班级说猫和其他班级说首页。因此,如果我执行类Cat的命名查询,我期望它的属性Cat.home将填充来自表主页的查询结果。
我执行的查询是这样的:
List<Cat> a = (List<Cat>) em.createNamedQuery("Cat.findHome")
.setParameter("catName", catName)
.setParameter("houseKey", houseKey).getResultList();
,结果我得到的是:
a = ArrayList<E>
elementData= Object[10] (id=22688)
[0] = Object[2] (id=22692)
[0] = Cat (id=22692)
[1] = Home (id=22692)
[1] = null
[2] = null
[3] = null
[4] = null
[5] = null
...
[9] = null
所以我的问题是我如何可以访问这些两个对象猫和主页,以及我如何从实体管理器获取此结果而不是单个对象Cat其中首页是Cat.home
实体:
Cat.java - 猫实体
//imports here
@Entity
@Table(name="CAT")
@NamedQuery(name="cat.findHome",
query="from Cat a join a.home p where a.name = :catName and p.housekey like :houseKey")
public class Cat implements Serializable{
private static final long serialVersionUID = 1L;
private String catkey;
private String name;
public List<Home> key; // a cat can have many homes
/**
* @param catkey the catkey to set
*/
public void setCatkey(String catkey) {
this.catkey = catkey;
}
/**
* @return the catkey
*/
@Id
@Column(name="K_CAT")
public String getCatkey() {
return catkey;
}
/**
* @return the name
*/
@Column(name="NAME")
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param home the home to set
*/
public void setHome(List<Home> home) {
this.home = home;
}
/**
* @return the home
*/
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="cat")
@Filter(name="contrFilter",condition = "K_CAT=:kcathome")
public List<Home> getHome() {
return home;
}
}
Home.java - 首页实体
@Entity
@Table(name="HOME")
public class Home implements Serializable{
private static final long serialVersionUID = 1L;
private Sting housekey;
private Cat cat;
/**
* @param housekey the housekey to set
*/
public void setHousekey(String housekey) {
this.housekey = housekey;
}
/**
* @return the housekey
*/
@Id
@Column(name="K_HOME")
public String getHousekey(){
return housekey;
}
/**
* @param cat the cat to set
*/
public void setCat(Cat cat) {
this.cat = cat;
}
/**
* @return the cat
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "K_CAT_HOME", updatable=false, insertable=false)
public Cat getCat() {
return cat;
}
}
提前感谢!
好吧,你会obiously期待'Cat's列表。你的命名查询看起来如何?请用它+实体对象更新你的问题。 – Magnilex 2013-05-06 11:17:36
更新了实体和类中的命名查询** Cat ** – 2013-05-06 12:51:42