-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to use "Criteria" to query "many-to-one" with the "or"
PostPosted: Mon Mar 28, 2011 5:25 am 
Newbie

Joined: Thu Mar 24, 2011 9:42 pm
Posts: 4
hi
I have three class

Code:
class Link{
int id;
Point pointA;
Point pointZ;
...
}
class Point{
int id;
Circuit circuit;
...
}
class Circuit{
int id;
...
}

now I want to query the Link which the pointA.circuit.id=1 or pointZ.circuit.id=1
the sql is

Code:
select * from Link where pointA.id in (select id from Point where circuit.id=1) or pointZ.id in (select id from Point where circuit.id=1)

how should i do?
thank you!


Top
 Profile  
 
 Post subject: Re: How to use "Criteria" to query "many-to-one" with the "or"
PostPosted: Mon Mar 28, 2011 7:46 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
This is really a question for the Hibernate Users forum, not for the Hiberate Search/Validator forum.
That said, have you looked at the Criteria API documentation?

--Hardy


Top
 Profile  
 
 Post subject: Re: How to use "Criteria" to query "many-to-one" with the "or"
PostPosted: Tue Mar 29, 2011 3:14 am 
Newbie

Joined: Thu Mar 24, 2011 9:42 pm
Posts: 4
yes.I've read the documents,I know I can navigate associations using createAlias() or createCriteria().
such as
Code:
criteria.createCriteria("pointA","a").add(Restrictions.eq("a.id",1));

to search all link which pointA.id=1
but I don't know how to create "or" criteria.
the method "createCriteria()" and "createAlias()" returns Criteria
but the method "Restrictions.or()" needs Criterion


Top
 Profile  
 
 Post subject: Re: How to use "Criteria" to query "many-to-one" with the "or"
PostPosted: Tue Mar 29, 2011 10:00 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Have you tried using DetachedCriteras?
Code:
DetachedCriteria pointAids= DetachedCriteria.forClass(Point.class).setProjection( Property.forName("id").add(...)
DetachedCriteria pointZids= DetachedCriteria.forClass(Point.class).setProjection( Property.forName("id").add(...)

List links = sess.createCriteria(Link.class)
    .add( Restrictions.or(
        Restrictions.in( "pointA.id", pointAids ),
        Restrictions.isNull("pointZ.id", pointAids )
    ) )
    .list();

Mind you this is only pseudo code.

--Hardy


Top
 Profile  
 
 Post subject: Re: How to use "Criteria" to query "many-to-one" with the "or"
PostPosted: Thu Mar 31, 2011 9:48 pm 
Newbie

Joined: Thu Mar 24, 2011 9:42 pm
Posts: 4
thank you,
the problem was solved,
I use the method,
Code:
session = HibernateSessionFactory.getSession();
DetachedCriteria pointAids = DetachedCriteria.forClass(Point.class)
         .setProjection(Property.forName("id")).add(Restrictions.eq("circuit.id", 10055));
DetachedCriteria pointZids = DetachedCriteria.forClass(Point.class)
         .setProjection(Property.forName("id")).add(Restrictions.eq("circuit.id", 10055));
List<Link> list = session.createCriteria(Link.class)
       .add( Restrictions.or(
      Restrictions.in("pointA.id", pointAids.getExecutableCriteria(session).list() ),
      Restrictions.in("PointZ.id", pointZids.getExecutableCriteria(session).list() )
      ))
      .list();

and it print 3 sql
Code:
Hibernate:
    select
        this_.ResPointID as y0_
    from
        CircuitResPoint this_
    where
        this_.CircuitID=?
Hibernate:
    select
        this_.ResPointID as y0_
    from
        CircuitResPoint this_
    where
        this_.CircuitID=?
Hibernate:
    select
        this_.ResLinkID as ResLinkID1_0_,
        this_.ResLinkName as ResLinkN2_1_0_,
        this_.ResLinkAlias as ResLinkA3_1_0_,
        this_.ResLinkDescr as ResLinkD4_1_0_,
        this_.ResLinkType as ResLinkT5_1_0_,
        this_.ResPointID_A as ResPointID6_1_0_,
        this_.ResPointID_Z as ResPointID7_1_0_
    from
        CircuitResLink this_
    where
        (
            this_.ResPointID_A in (
                ?, ?, ?, ?, ?, ?, ?, ?, ?
            )
            or this_.ResPointID_Z in (
                ?, ?, ?, ?, ?, ?, ?, ?, ?
            )
        )

but if it has too many items in pointAids or pointZids maybe there will be a problem.


Top
 Profile  
 
 Post subject: Re: How to use "Criteria" to query "many-to-one" with the "or"
PostPosted: Fri Apr 01, 2011 2:35 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

There might be limits of your database you have to consider. Have a look at http://opensource.atlassian.com/project ... e/HHH-1123 as well

--Hardy


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