-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: simple hibernate mapping
PostPosted: Sat Sep 12, 2009 8:09 am 
Newbie

Joined: Fri Aug 28, 2009 8:29 am
Posts: 3
Book.java

Code:
public class Book implements Serializable{

   private String id;
   private String name;
   public Book() {
      // TODO Auto-generated constructor stub
   }
   private List chapters;
   
   public List getChapters() {
      return chapters;
   }
   public void setChapters(List chapters) {
      this.chapters = chapters;
   }
   public String getId() {
      return id;
   }
   public void setId(String id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   
}


Chapters.java

Code:
public class Chapter implements Serializable{

   private String id;
   private String name;
   public Chapter() {
      // TODO Auto-generated constructor stub
   }
   public Chapter(String id, String name) {
      super();
      this.id = id;
      this.name = name;
   }
   public String getId() {
      return id;
   }
   public void setId(String id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   
}



Mapping file

Code:
<hibernate-mapping default-cascade="all">
   <class name="Book" table="BOOK" >
      <id name="id" type="string" column="ID"></id>
      <property name="name" type="string" column="NAME"></property>
      <list name="chapters" cascade="save-update,delete" lazy="false" inverse="true" fetch="join">
         <key column="BOOK_ID" update="true" ></key>
         <list-index column="INDEXX" base="0" ></list-index>
         <one-to-many class="Chapter" />
      </list>
   </class>
   <class name="Chapter" table="CHAPTER">
      <id name="id" type="string" column="ID"></id>
      <property name="name" type="string" column="NAME"></property>
   </class>
</hibernate-mapping>



Running code


Code:
   public static void main(String[] args) {
      try {
         Chapter c1 = new Chapter("c1", "c1 name");
         Chapter c2 = new Chapter("c2", "c2 name");
         Chapter c3 = new Chapter("c3", "c3 name");
         List cList = new ArrayList();
         cList.add(c1);
         cList.add(c2);
         cList.add(c3);
         Book b = new Book();
         b.setId("b1");
         b.setName("b name");
         b.setChapters(cList);
         Session s = getSessionFactory().openSession();
         s.saveOrUpdate(b);
         s.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
      
   }


When i run the above code i need to persist Book with chapters...

sql statements executed by the class


Code:
Hibernate: select book_.ID, book_.NAME as NAME0_ from BOOK book_ where book_.ID=?
Hibernate: select chapter_.ID, chapter_.NAME as NAME1_ from CHAPTER chapter_ where chapter_.ID=?
Hibernate: select chapter_.ID, chapter_.NAME as NAME1_ from CHAPTER chapter_ where chapter_.ID=?
Hibernate: select chapter_.ID, chapter_.NAME as NAME1_ from CHAPTER chapter_ where chapter_.ID=?



it doesn't persist anything to the dadabase...

can anybody help me in this......it might be simple... but i cant able to figure out what is the problem...


Top
 Profile  
 
 Post subject: Re: simple hibernate mapping
PostPosted: Sat Sep 12, 2009 9:50 am 
Beginner
Beginner

Joined: Wed Jun 17, 2009 9:03 pm
Posts: 31
Location: mumbai
Hi Need to put the save part in a transaction.



Code:
SessionFactory   sf   =   getSessionFactory();
         Session      s =   sf.openSession();
         Transaction   tr      =   s.beginTransaction();
      try {
            Chapter c1 = new Chapter("c1", "c1 name");
            Chapter c2 = new Chapter("c2", "c2 name");
            Chapter c3 = new Chapter("c3", "c3 name");
            List cList = new ArrayList();
            cList.add(c1);
            cList.add(c2);
            cList.add(c3);
            Book b = new Book();
            b.setId("b1");
            b.setName("b name");
            b.setChapters(cList);
          
            s.saveOrUpdate(b);
            tr.commit();
            s.close();
         } catch (Exception e) {
            e.printStackTrace();
         }


Top
 Profile  
 
 Post subject: Re: simple hibernate mapping
PostPosted: Sat Sep 12, 2009 10:21 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
The key is using a transaction. I'm actually suprised that you didn't get a transaction required exception when you ran.

You know, you might want to just start with a simpler example. Check out my signature links for some very easy to follow tutorials.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: simple hibernate mapping
PostPosted: Sat Sep 12, 2009 2:41 pm 
Newbie

Joined: Fri Aug 28, 2009 8:29 am
Posts: 3
thanx prateek.singh it works fine now.....

to Cameron McKenzie

thanx for your links..... ill spend this weekend with your tutorials... itll help beginners like me...cheers for your work........


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.