-->
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.  [ 2 posts ] 
Author Message
 Post subject: How write custom join clause in one-to-many mapping
PostPosted: Wed Jun 29, 2016 7:00 am 
Newbie

Joined: Wed Jun 15, 2016 2:38 am
Posts: 4
I have 2 tables repository and repository_credential_assn (relationship is one to many)


Repository

id
name


repository_credential_assn
repository_id
credential_id
is_default

I want to get all repository list info and those credential_id which is_default = true

Code:
@Entity
@Table(name="repository")
@SecondaryTable(name = "repository_credential_assn", pkJoinColumns=@PrimaryKeyJoinColumn(name="repository_id", referencedColumnName="id"))
public class RepositoryDom implements Serializable {
   
   @Id
   @Column(name="id")   
   private int id;
   
   @Column(name = "name")
   private String name;      

        @Column(name="credential_id", table="repository_credential_assn")
   @WhereJoinTable(clause = "is_default = true")
   private Integer defCredId;


Select should be as follows:

Code:
select r.id, r.name, rca.credential_id
from repository r left outer join repository_credential_assn rca on r.id = rca.repository_id and is_default =true


But @WhereJoinTable does not work 'and is_default =true' condition does not add. How can I solve the problem ?


Top
 Profile  
 
 Post subject: Re: How write custon join clause
PostPosted: Wed Jun 29, 2016 8:24 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The SecondaryTable works only for tables whose rows are in a one-to-one relationship. In your case, there is a one-to-many association, so the mapping is wrong.

1. You need to map Repository and RepositoryCredentialAssn as individual entities
2. In RepositoryCredentialAssn, you need to have:

Code:
@ManyToOne
@JoinColumnsOrFormulas(
{
    @JoinColumnOrFormula(column=@JoinColumn(name="repository_id", referencedColumnName="id")),
    @JoinColumnOrFormula(formula=@JoinFormula(value="true", referencedColumnName="is_default"))
})
private Repository repository;


3. Then, in Repository you have the bidirectional one-to-many side:

Code:
@OneToMany(mappedBy = "repository", cascade = CascadeType.ALL, orphanRemoval = true)
private List<RepositoryCredentialAssn> credentials = new ArrayList<>();


So you can access the RepositoryCredentialAssn like this:

Code:
Repository repository - entotuManager.find(Repository.class, repositoryId);
List<RepositoryCredentialAssn> credentials = repository.getCredentials();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.