• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

CRUD operations in Hibernate on 2 tables

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic