ElementCollection createAlias in hibernate API

后端 未结 3 1690
北荒
北荒 2021-02-07 12:53

does anyone know if and how the solution for following question (which is written in the JPA API) can be written using the hibernate criteria API?

To be more specific I

相关标签:
3条回答
  • 2021-02-07 13:08

    Can you give us a more complete schema and query?

    Also, Criteria API sucks. Try using QueryDSL, your query might look like:

    HibernateQuery q=new HibernateQuery(getSession())
    .from(QDiscussion.discussion).from(QDocument.document)
    .where(QDocument.document.userName.in(QDiscussion.discussion.userNames))
    .list(QDocument.document);
    
    0 讨论(0)
  • 2021-02-07 13:24

    Late answer.

    The correct propertyName for collection (annotated by @ElementCollection) is "elements" or constant CollectionPropertyNames#COLLECTION_ELEMENTS.

    Try with the following answers

    Criteria crit = getSession().createCriteria(Discussion.class);
    crit.createAlias("participants", "p");
    
    crit.add(Restrictions.eq("p.elements", portalUsername));
    

    or use the constant COLLECTION_ELEMENTS instead

    crit.add(Restrictions.eq("p." + CollectionPropertyNames.COLLECTION_ELEMENTS, portalUsername));
    
    0 讨论(0)
  • 2021-02-07 13:30

    As explained here what I wanted is not possible:

    The limitations of using an ElementCollection instead of a OneToMany is that the target objects cannot be queried, persisted, merged independently of their parent object. They are strictly privately-owned (dependent) objects, the same as an Embedded mapping. There is no cascade option on an ElementCollection, the target objects are always persisted, merged, removed with their parent.

    So, I used a OneToMany instead.

    0 讨论(0)
提交回复
热议问题