1

我有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; 
    } 
} 

提前感謝!

+0

好吧,你會obiously期待'Cat's列表。你的命名查詢看起來如何?請用它+實體對象更新你的問題。 – Magnilex 2013-05-06 11:17:36

+0

更新了實體和類中的命名查詢** Cat ** – 2013-05-06 12:51:42

回答

1

添加select部分命名查詢

query="select a from Cat a where a.name = :catName and a.home.houseKey like :houseKey") 
+0

對不起,我之前刪除的問題。這確實似乎是正確的答案。 – Magnilex 2013-05-06 12:55:11

+0

@MagnusTengdahl我更新了查詢。現在沒有''加入''從' – Ilya 2013-05-06 12:56:00

+0

幾乎工作。這意味着它會正確返回對象列表,但** Cat.house **屬性似乎不包含它應該包含的內容。我正在調試這個,然後我會回覆更多的信息。 – 2013-05-06 13:00:56