Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CRUD operations in Hibernate on 2 tables

 
Osgar ali
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to ORM world, I configured my Hibernate and MySQL. I wrote a small program and was able to get some data from one table. Now, I am trying to learn little bit more. I created 2 tables student and contact and an association table student_contact see DDL below.
Can some one help me to do following CRUD. I want to do 4 operations for students contact (insert, select,delete and update). Some example will really help
Thanks

List studentList = null;
Session session = HibernateUtil.getSessionFactory()
.getCurrentSession();
session.beginTransaction();
try
{
studentList = session.createQuery(
"from Students ORDER BY name").list();
session.getTransaction().commit();
}
catch (HibernateException e)
{
session.getTransaction().rollback();
throw e;
}






CREATE TABLE students (
student_id int(10) NOT NULL,
first_name varchar(50) NOT NULL,
last_name varchar(50) NOT NULL,
sex varchar(6) NOT NULL,
PRIMARY KEY (student_id)
);

CREATE TABLE contacts (
contact_id int(10) NOT NULL,
address1 varchar(50) NOT NULL,
address2 varchar(50),
state varchar(2) NOT NULL,
city varchar(50) NOT NULL,
country varchar(50) NOT NULL,
zipcode varchar(10) NOT NULL,
PRIMARY KEY (contact_id)
);


CREATE TABLE student_contacts (
student_id int(10) NOT NULL,
contact_id int(10) NOT NULL,
PRIMARY KEY (student_id,contact_id)
);

ALTER TABLE student_contacts
ADD FOREIGN KEY (student_id)
REFERENCES students (student_id);

ALTER TABLE student_contacts
ADD FOREIGN KEY (contact_id)
REFERENCES contacts (contact_id);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic