Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...


What is Restrictions in hibernate?



What is Restrictions in hibernate?..

Answer / pradip mishra(b-tech it)

This example demonstrate the use of Hibernate's
Restriction.in criterion. This restriction will query for
some record based on a collection of parameter defined for
a specific property of the bean.

view sourceprint?01.package
org.kodejava.example.hibernate.criteria;
02.

03.import org.hibernate.SessionFactory;
04.import org.hibernate.Session;
05.import org.hibernate.HibernateException;
06.import org.hibernate.Criteria;
07.import org.hibernate.criterion.Restrictions;
08.import org.hibernate.cfg.AnnotationConfiguration;
09.import org.kodejava.example.hibernate.model.Genre;
10.

11.import java.util.List;
12.

13.public class RestrictionInDemo {
14.
private static final SessionFactory sessionFactory;
15.

16.
static {
17.
try {
18.
sessionFactory = new AnnotationConfiguration().
19.
configure("hibernate.cfg.xml").
20.
buildSessionFactory();
21.
}
22.
catch (Throwable ex) {
23.
throw new ExceptionInInitializerError(ex);
24.
}
25.
}
26.

27.
public static Session getSession() throws
HibernateException {
28.
return sessionFactory.openSession();
29.
}
30.

31.
@SuppressWarnings("unchecked")
32.
public static void main(String[] args) {
33.
final Session session = getSession();
34.
try {
35.
//
36.
// Create a Criteria an add an in constraint for the
property id.
37.
// This in restrictions will return genre with id 1, 2, 3
and 4.
38.
//
39.
Criteria criteria = session.createCriteria(Genre.class)
40.
.add(Restrictions.in("id", new Long[] {1l, 2l, 3l, 4l}));
41.

42.
List<Genre> result = criteria.list();
43.

44.
for (Genre genre : result) {
45.
System.out.println(genre.getId() + "; " + genre.getName());
46.
}
47.
} finally {
48.
session.close();
49.
}
50.
}
51.}

Is This Answer Correct ?    2 Yes 0 No

Post New Answer

More Core Java Interview Questions

Explain about data types?

0 Answers  


What is an exception? difference between Checked and Unchecked exception in Java

0 Answers   SkillGun Technologies,


How to print a statement without using semicolan in java

6 Answers   HCL, IBM,


java program that takes a 3 digit number n and finds out whether the number 2^n + 1 is prime, or if it is not prime find out its factors.

7 Answers   HCL, Wipro,


What is a variable in java?

0 Answers  






What is a consumer in java?

0 Answers  


Where local and global variables are stored?

0 Answers  


Define a java class.

0 Answers  


What are white spaces in java?

0 Answers  


what is bytecode ?explain in detail and watz the difference between bytecode and machine code?

6 Answers   Lucent,


What is the difference between java and .Net?

39 Answers   Bosch,


What is JFC?

0 Answers  






Categories