Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Hibernate configuration  RSS feed

Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my hibernate code for one for one to many mapping

Customer has Requests

public class Customer {
private int id;  
private String qname;  
private Set<Request> answers;  
public Customer(){}
public int getId() {
return id;
public void setId(int id) { = id;
public String getQname() {
return qname;
public void setQname(String qname) {
this.qname = qname;
public Set<Request>getAnswers() {
return answers;
public void setAnswers(Set<Request>answers) {
this.answers = answers;


Request Pojo class

public class Request {
private int id;  
private String answername;  
private String postedBy;  
public Request(){}
public int getId() {
return id;
public void setId(int id) { = id;
public String getAnswername() {
return answername;
public void setAnswername(String answername) {
this.answername = answername;
public String getPostedBy() {
return postedBy;
public void setPostedBy(String postedBy) {
this.postedBy = postedBy;


Mapping file of Customer

<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   <hibernate-mapping package="com.srikant.hibernate3">
   <class name="Customer" table="cust">
   <id name="id" column="id">
   <generator class="increment"/>
   <property name="qname" column="qname"/>
   <set name="answers" cascade="all">
   <key column="qid"/>
   <one-to-many class="Request"/>

Request mapping file

<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   <hibernate-mapping package="com.srikant.hibernate3">
   <class name="Request" table="requests">
   <id name="id">  
         <generator class="increment"></generator>  
         <property name="answername"></property>  
         <property name="postedBy"></property>  
   Main class

public class Hibernate {
public static void main(String[] args){
Configuration cfg=new Configuration();
SessionFactory sf=cfg.buildSessionFactory();
Session se=sf.openSession();
Transaction tx=se.beginTransaction();

System.out.println("configuration pass");
Request ans1=new Request();    
   ans1.setAnswername("Java is a programming language");    
   ans1.setPostedBy("Ravi Malik");    
   Request ans2=new Request();    
   ans2.setAnswername("Java is a platform");    
   ans2.setPostedBy("Sudhir Kumar");    
   Request ans3=new Request();    
   ans3.setAnswername("Servlet is an Interface");    
   ans3.setPostedBy("Jai Kumar");    
   Request ans4=new Request();    
   ans4.setAnswername("Servlet is an API");    
Set<Request> list1=new HashSet<Request>();    
   Set<Request> list2=new HashSet<Request>();    
   Customer question1=new Customer();    
   question1.setQname("What is Java?");    
   Customer question2=new Customer();    
   question2.setQname("What is Servlet?");    
}  }  

As per my Request.hbm.xml file there are only three columns
Answer NAME
Posted by

When i run my program
I get one more row in my ORacle

 QID which is the foreign key
Which i have not mentioned in in my request mapping file
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!