1

I am using play framework(java) 1.2.4, jpa and hibernate.How can i check relation(oneToMany) model filed value on find error occurred

Contry model:

@Entity
public class Countries extends Model {

    @Required
    public String name;

    public String iso2;

    public String iso3; 

    @OneToMany(mappedBy="country", fetch=FetchType.EAGER,  cascade=CascadeType.All)
    public List<States> states;

    public Countries() { }

}

State model:

@Entity
public class States extends Model {

   @Required
   public long country_id;

   @Required
   public String name;  

   @Required
   public long product_id; 

   @ManyToOne(fetch=FetchType.EAGER)
   @NotFound(action = NotFoundAction.IGNORE)
   @JoinColumn(name="country_id", nullable=false,insertable=false, updatable=false)
   public Countries country;

   public States() { }
}

In Contry controller:

   List<Countries> countries = Countries.find("states.product_id =5").fetch(); 

When i check states table value (oneToMany) following error occurred:

    IllegalArgumentException occured : org.hibernate.QueryException: illegal attempt to dereference collection 
4

2 回答 2

0

我认为这样的事情会起作用,尽管它未经测试。

List<Countries> countries = Countries.find(
    "select c from Countries c, States s where s.country = c and s.product_id = ?", 5
);

你可能需要 5 左右的报价,我忘了。您也可以使用 join 子句来做到这一点,但我认为这里没有必要。

此外,您的型号名称应该是单数,而不是复数。那应该让你感到困惑。这让我很困惑:-)

于 2013-02-18T12:17:45.090 回答
0

方法find也支持正常的 JPQL 查询。以下是获取具有 product_id 5 状态的唯一国家/地区列表的直接替换:

SELECT DISTINCT(s.country) FROM States s WHERE s.product_id = 5
于 2013-02-18T21:22:34.833 回答