-->
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: Hibernate join two coloumn
PostPosted: Mon Oct 19, 2015 4:51 am 
Newbie

Joined: Fri Oct 02, 2015 6:11 am
Posts: 6
Hi World. i have a problem.

I have two table : Reunion, metting
The two tables are connected by id.

I must do two control:
1- The id must be equal betwenn two tables.
2-The state of reunion must be "active"

In sql language is :
select * from Reunion p, meeting a where p.number=a.number and p.state='active'

anybody knows write in hibernate language ( criatealias ecc.)?

I enetered in the model onetomany ecc. The problem is the part that is running ( the dao).

sorry for my english ahah thanks.


Top
 Profile  
 
 Post subject: Re: Hibernate join two coloumn
PostPosted: Mon Oct 19, 2015 11:59 am 
Regular
Regular

Joined: Mon Oct 19, 2015 7:49 am
Posts: 61
Location: ChengDu China
(1) HQL/JPQL:
select p, a from Reunion p, meeting a where p.number=a.number and p.state='active'

(2) QBC
Unfortunately, QBC does not support cross join(or multiple "from" keywords), it only supports join by asscoaition
Criteria criteria = session.createCriteria(Eunion.class);
criteria.createAlias("meeting", m); //Class "Reunion" must has a one-to-one property "meeting", this statment contains "this.number == m.number" implicitly, so need not to do this in where
criteria.addRestriction(Restrictions.eq("this.state", 'active'));
criteria.setProjection(Projections.list("this", "m")); //Not very sure, because haven't use QBC for near ten years

(3) JPACriteria(recommand, JPA API, not Hibernate API, typed safe, but need maven plugin to generate source code at compilation-time)

// If any error, report error at compliation-time, not runtime. So cool!!!
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Object[]> cq = cb.createCriteria(Object[].class);
Root<Reunion> p= cq.from(Reunion.class);
Root<Metting> a = cq.from(Meeting.class);
cq
.where(
cb.equal(p.get(Reunion_.number), a.get(Meeting_.number)) //Reunion_ and Meeting_ are two new classes generated by maven plugin
cb.equal(p.get(Reunion_.state), "active")
)
.multiselect(p, a);
return entityManager.createQuery(cq).getResultList();

_____________________________________________________________

By the way, help me to let more Hibernate friends know: https://github.com/babyfish-ct/babyfish


Top
 Profile  
 
 Post subject: Re: Hibernate join two coloumn
PostPosted: Wed Oct 21, 2015 2:25 am 
Newbie

Joined: Thu Oct 15, 2015 11:05 pm
Posts: 4
thank you for this great lesson, that's sweet of you.
Many Thanks and all the best


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.