Win a copy of Head First Go this week in the Go 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 ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Hibernate configuration  RSS feed

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
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) {
this.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) {
this.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"
   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
   <hibernate-mapping package="com.srikant.hibernate3">
   <class name="Customer" table="cust">
   <id name="id" column="id">
   <generator class="increment"/>
   </id>
   <property name="qname" column="qname"/>
   <set name="answers" cascade="all">
   <key column="qid"/>
   <one-to-many class="Request"/>
   </set>
   </class>
   </hibernate-mapping>


Request mapping file


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

   
public class Hibernate {
public static void main(String[] args){
Configuration cfg=new Configuration();
   cfg.configure("hibernate.cfg.xml");
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");    
   ans4.setPostedBy("Arun");    
       
Set<Request> list1=new HashSet<Request>();    
   list1.add(ans1);    
   list1.add(ans2);    
       
   Set<Request> list2=new HashSet<Request>();    
   list2.add(ans3);    
   list2.add(ans4);    
       
   Customer question1=new Customer();    
   question1.setQname("What is Java?");    
   question1.setAnswers(list1);    
       
   Customer question2=new Customer();    
   question2.setQname("What is Servlet?");    
   question2.setAnswers(list2);    
       
   se.persist(question1);    
   se.persist(question2);    
       
   tx.commit();    
   se.close();    
   System.out.println("success");    
}  }  





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

But
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!