Code

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

Eager,Lazy App:

Laptop l1 = new Laptop();


l1.setLid(1);
l1.setBrand("dell");
Laptop l2 = new Laptop();
l2.setLid(2);
l2.setBrand("hp");
Laptop l3 = new Laptop();
l3.setLid(3);
l3.setBrand("lenovo");

Student s = new Student();


s.setSid(101);
s.setSname("Smith");
s.setMarks(90);
s.getLaptop().add(l1);
s.getLaptop().add(l3);

Student s1 = new Student();


s1.setSid(102);
s1.setSname("Mike");
s1.setMarks(89);
s1.getLaptop().add(l2);

l1.setStudent(s);
l2.setStudent(s1);
l3.setStudent(s);

Configuration con = new


Configuration().configure().addAnnotatedClass(Student.class)
.addAnnotatedClass(Laptop.class);
SessionFactory sf = con.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
session.persist(l1);
session.persist(l2);
session.persist(l3);

session.persist(s);
session.persist(s1);
tx.commit();

--------------------------------------------------------------
Configuration con = new
Configuration().configure().addAnnotatedClass(Student.class)
.addAnnotatedClass(Laptop.class);
SessionFactory sf = con.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Student student = session.get(Student.class, 101);
System.out.println(student.getSname());
Collection<Laptop> laps = student.getLaptop();
for(Laptop l:laps)
System.out.println(l);
tx.commit();

You might also like