-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How to filter parents by child entity type?
PostPosted: Wed May 11, 2016 8:15 am 
Newbie

Joined: Wed May 11, 2016 8:13 am
Posts: 1
I have following object mapping in our project. One contact can have multiple quotes from typeA and typeB.

Code:
@Entity
@Table(name = "t_ind_contact")
public class Contact implements Serializable {

    @Id
    @Column(name = "id", unique = true)
    private Long id;

    @OneToMany
    private List<Quote> quotes;

}

Abstract Quote class.

    @Entity
    @Inheritance
    @DiscriminatorColumn(name = "app_code")
    @Table(name = "t_ind_quote")
    public abstract class Quote implements Serializable {
   
        private static final long serialVersionUID = 1L;
   
        @Id
        @Column(name = "id")
        private long id;
   
        @OneToMany
        @JoinColumn(name = "contact_id")
        private Contact contact;
   
        @Column(name = "app_code", insertable = false, updatable = false)
        private int appCode;
    }


TypeA Quote class

    @Entity
    @DiscriminatorValue("1")
    public class TypeAQuote implements Serializable {
   
        private static final long serialVersionUID = 1L;
   
    }

TypeB Quote class

    @Entity
    @DiscriminatorValue("2")
    public class TypeBQuote implements Serializable {
   
        private static final long serialVersionUID = 1L;
   
    }


This mapping is working fine for all operations. But for some operations we need the contact object with typeA quotes only. As of now, we will filter typeA quotes after system loads data from the database.

Is there a way that we can populate a contact object only with typeA quotes? I mean even the generated queries will load only typeA quotes.


Top
 Profile  
 
 Post subject: Re: How can I load only specific child object to a mapping place
PostPosted: Mon May 16, 2016 8:21 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You can write a query for that:

Code:
List<Contact > contacts = entityManager
.createQuery(
    "select distinct c " +
    "from Contact c " +
    "where TypeA = all (" +
    "   select type(q) from Quote q where q.contact = c" +
    ")", Contact .class)
.getResultList();


Check out this GitHub code example.


Top
 Profile  
 
 Post subject: Re: How can I load only specific child object to a mapping place
PostPosted: Mon May 23, 2016 2:54 am 
Newbie

Joined: Sat Apr 16, 2016 1:43 am
Posts: 2
mihalcea_vlad wrote:
You can write a query for that:

Code:
List<Contact > contacts = entityManager
.createQuery(
    "select distinct c " +
    "from Contact c " +
    "where TypeA = all (" +
    "   select type(q) from Quote q where q.contact = c" +
    ")", Contact .class)
.getResultList();


Check out this GitHub code example.


This answered a similar question that I had. Thanks for the link too. Great useful post. Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.