【问题标题】:EnityManager query returns a strange unaccessible objectEntityManager 查询返回一个奇怪的不可访问对象
【发布时间】:2013-05-06 10:57:26
【问题描述】:

我有 2 个通过连接注释连接的实体,除了查询结果奇怪之外,一切正常。

所以我让这个班级说Cat,另一个班级说Home。因此,如果我执行类 Cat 的命名查询,我希望它的属性 Cat.home 将填充来自表 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

所以我的问题是如何访问这两个对象 CatHome,以及我为什么从实体管理器而不是单个对象获得这个结果Cat,其中 Home 位于 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;
    }
}

提前致谢!

【问题讨论】:

  • 嗯,你会非常期待Cats 的列表。您的命名查询看起来如何?请用它+实体对象更新您的问题。
  • 更新了实体和类 Cat 中的命名查询

标签: java hibernate entity entitymanager hibernate-entitymanager


【解决方案1】:

在命名查询中添加select 部分

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

【讨论】:

  • 对不起我之前删除的问题。这似乎确实是正确的答案。
  • @MagnusTengdahl 我更新了查询。现在在from 中没有join
  • 几乎可以工作。这意味着它正确返回对象列表,但 Cat.house 属性似乎不包含它应该包含的内容。我正在调试这个,然后我会回复更多信息。
  • 就是这样。我不敢相信我花了大约 3 个小时试图弄清楚这一点,这是一件如此简单的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-09
  • 2012-04-17
  • 1970-01-01
  • 2020-04-06
  • 2016-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多