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.