I have 2 entities connected through join annotations and everything works fine except for the query result which is strange.
So I have this class say Cat and the other class say Home. So if I execute a named query of the class Cat I expect it's property Cat.home to be populated with the query result from the table Home.
I'm executing the query this way:
List<Cat> a = (List<Cat>) em.createNamedQuery("Cat.findHome")
.setParameter("catName", catName)
.setParameter("houseKey", houseKey).getResultList();
and the result I get is:
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
So my question is how can I access those two objects Cat and Home, and how come I'm getting this result from the entity manager and not a single Object Cat where Home is in Cat.home
Entities:
Cat.java - Cat entity
//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 - Home entity
@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;
}
}
Thanks in advance!
add select
part to named query
query="select a from Cat a where a.name = :catName and a.home.houseKey like :houseKey")