-->
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 map a RIGHT JOIN with Hibernate
PostPosted: Wed Jun 15, 2016 2:58 am 
Newbie

Joined: Wed Jun 15, 2016 2:38 am
Posts: 4
I have 2 tables datasource and ds_restriction and relationship is many to one. I want to select all datasources regardless they are restricted or not. Here is mapped classes:

Code:
@Entity
@Table(name="datasource")
public class DatasourceDom implements Serializable {

@Id
@Column(name = "id")   
private int id;

@Column(name = "alias")
private String alias;   

@Column(name = "url")
private String url;


@Entity
@Table(name = "ds_restriction")
public class DsRestrictionDom implements Serializable {

@Id
@Column(name = "datasource_id")
private int datasourceId;

@Id
@Column(name = "principal")
private String principal;   

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "datasource_id")
private DatasourceDom datasource;


And here is my code to get datasources:

Code:
public List<DsRestrictionDom> getDatasorcesRestictions() {
    Session session = getCurrentSession();
    Criteria criteria = session.createCriteria(DsRestrictionDom.class);
    criteria.createCriteria("datasource","ds",JoinType.RIGHT_OUTER_JOIN);
    List<DsRestrictionDom> dsList = criteria.list();       
    return dsList;
}


The result is:

Code:
[
  null,
  null,
  null,
  null,
  null,
  null,
  null,
  {
    "datasourceId": 1,
    "principal": "PUBLIC",   
    "datasource": {
      "id": 1,
      "alias": "alias1",     
      "url": "some url"
    }
  },
  null,
  null
]


What is incorrect ?


Top
 Profile  
 
 Post subject: Re: How should be mapping in case of RIGHT JOIN
PostPosted: Wed Jun 15, 2016 4:09 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
Because you are selecting DatasourceDom, when there is no matching (RIGHT_JOIN) you'll get a NULL.

If you want to select all DatasourceDom and their associated DsRestrictionDom, you should add a bidirectional association:

Code:
@Entity
@Table(name="datasource")
public class DatasourceDom implements Serializable {

    @Id
    @Column(name = "id")
    private int id;

    @Column(name = "alias")
    private String alias;

    @Column(name = "url")
    private String url;
   
    @OneToMany(mappedBy = "datasource", cascade = CascadeType.ALL)
    private List<DsRestrictionDom> dsRestrictionDoms = new ArrayList<>();

    ...
}


You should remove the CascadeType.ALL from the @ManyToOne association because it makes no sense to cascade from a child to a parent.


Then the query becomes:

Code:
select dd
from DatasourceDom dd
left join fetch dd.dsRestrictionDoms


Top
 Profile  
 
 Post subject: Re: How should be mapping in case of RIGHT JOIN
PostPosted: Wed Jun 15, 2016 8:57 am 
Newbie

Joined: Wed Jun 15, 2016 2:38 am
Posts: 4
Thank you it is worked.


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.