开发者

jpa Using WHERE clause

I have 2 entities:

@Entity
public class Elements implements Serializable {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long id;

   private Owner owner;

}

@Entity
public class Owner implements Serializable {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long id;

   @OneToMany(fetch=FetchType.LAZY)
   List<Elements> elements;
}

Suppose I want to fetch all elemets bellonging to the owner from Elements Table and therfore I use:

TypedQuery query=em.createQuery("SELECT elem FROM Elements elem WHERE 
                       elem.owner:=elemOwner", Elements.class);

query.setParameter("elemOwner", ownerObjectFetchFromDataBase);

List<TrendUsers> u开发者_JS百科serList=query.getResultList();

But I get the following error: Comparisons between 'BLOB' and 'BLOB' are not supported. Types must be comparable. String types must also have matching collation. If collation does not match, a possible solution is to cast operands to force them to the default collation...

Is there any way I can Select from Elements Table and in the WHERE clause use object (and not just String,int...)?

(p.s I also tried the query below and it didn't work: TypedQuery query=em.createQuery("SELECT elem FROM Elements elem WHERE elem.owner.id:=elemOwner", Elements.class); query.setParameter("elemOwner", ownerObjectFetchFromDataBase.id); List userList=query.getResultList(); )

Thanks


You need to mark the Owner als a ManyToOne.

@Entity public class Elements implements Serializable {
   ...snip ...
   @ManyToOne(fetch=FetchType.LAZY)
   @JoinColumn(name="OWNER_ID")
   private Owner owner;
}

@Entity public class Owner implements Serializable {
   .. snip ...
   @OneToMany(fetch=FetchType.LAZY, mappedBy="owner")
   List<Elements> elements;
}

Right now you try to store the serialized owner in Blob. Thats not what you want ;-)

enjoy

Edit: included fix by xatavt

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜