-->
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.  [ 5 posts ] 
Author Message
 Post subject: New to Hibernate (SQL queries using Hibernate)
PostPosted: Wed Mar 11, 2009 11:13 pm 
Newbie

Joined: Tue Mar 10, 2009 10:48 pm
Posts: 18
Hi, I am very very new to Hibernate. I needed to execute the following sql queries using Hibernate Criteria.

SELECT s.id, s1.name
FROM table s,
table s1,
table s2
WHERE s1.id = '1'
AND s2.name = 'VEST'


Could someone please give me some tips on this?
Thank you so much in advance!!


Top
 Profile  
 
 Post subject: Re: New to Hibernate (SQL queries using Hibernate)
PostPosted: Thu Mar 12, 2009 4:12 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
janiceileen wrote:
Hi, I am very very new to Hibernate. I needed to execute the following sql queries using Hibernate Criteria.

SELECT s.id, s1.name
FROM table s,
table s1,
table s2
WHERE s1.id = '1'
AND s2.name = 'VEST'


Could someone please give me some tips on this?
Thank you so much in advance!!


do you have any relationship between the tables?


Top
 Profile  
 
 Post subject: Re: New to Hibernate (SQL queries using Hibernate)
PostPosted: Thu Mar 12, 2009 4:13 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
janiceileen wrote:
Hi, I am very very new to Hibernate. I needed to execute the following sql queries using Hibernate Criteria.

SELECT s.id, s1.name
FROM table s,
table s1,
table s2
WHERE s1.id = '1'
AND s2.name = 'VEST'


Could someone please give me some tips on this?
Thank you so much in advance!!


do you have any relationship between the tables?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 5:30 am 
Newbie

Joined: Tue Mar 10, 2009 10:48 pm
Posts: 18
Oh Yes, let me post the relationship.


SELECT s.id, s1.name
FROM table s,
table s1,
table s2
WHERE s.str_id = '1'
AND s.id = s1.id <--------relationship
AND s.version = '2'
AND s2.id = '1'
AND s2.version = '2'
AND s2.name = 'VEST'

I have created the following:
Criteria criteria = session.createCriteria(s.class, "s") <-- for table s

DetachedCriteria nQuery = DetachedCriteria.forClass(s1.class, "s1") <--for table s1

DetachedCriteria sQuery = DetachedCriteria.forClass(s2.class, "s2") <---for table s2

I do not know how to combine these 3 together and not sure whether am I in the right direction. I have been trying for 2 weeks. I would appreciate any kinda help. Thank you so much.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 10:00 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
janiceileen wrote:
Oh Yes, let me post the relationship.


SELECT s.id, s1.name
FROM table s,
table s1,
table s2
WHERE s.str_id = '1'
AND s.id = s1.id <--------relationship
AND s.version = '2'
AND s2.id = '1'
AND s2.version = '2'
AND s2.name = 'VEST'

I have created the following:
Criteria criteria = session.createCriteria(s.class, "s") <-- for table s

DetachedCriteria nQuery = DetachedCriteria.forClass(s1.class, "s1") <--for table s1

DetachedCriteria sQuery = DetachedCriteria.forClass(s2.class, "s2") <---for table s2

I do not know how to combine these 3 together and not sure whether am I in the right direction. I have been trying for 2 weeks. I would appreciate any kinda help. Thank you so much.


It's better if you post your java class instead of the sql.

This is the sample:
Code:
@Entity
@Table(name = "book")
public class Book implements Serializable {
   private static final long serialVersionUID = -5468472990471954259L;
   private Long id;
   private String title;
   private String isbn;
   private Author author;
        //setter and getter
}

@Entity
@Table(name = "author")
public class Author implements Serializable {
   private static final long serialVersionUID = 1130619164691755745L;
   private Long id;
   private String name;
   private Status activeStatus;
        //setter and getter
}

Query:
Criteria crit = session.createCriteria(Book.class);
crit.createCriteria("author", "author").add(Restrictions.eq("name", "Agustino"));
List<Book> list = book.list();

SQL :
select
        this_.id as id1_1_,
        this_.authorID as authorID1_1_,
        this_.delFlag as delFlag1_1_,
        this_.isbn as isbn1_1_,
        this_.title as title1_1_,
        author1_.id as id0_0_,
        author1_.activeStatus as activeSt2_0_0_,
        author1_.name as name0_0_
    from
        book this_
    inner join
        author author1_
            on this_.authorID=author1_.id
    where
        author1_.name=?


Note: You cannot use Criteria if you wanto to retrieve data from multiple tables and the tables don't have relationship each other. Using Query instead.


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