-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to insert data to the exist table by using Hibernate...
PostPosted: Thu Jun 24, 2004 2:45 pm 
Newbie

Joined: Mon Jun 21, 2004 3:43 pm
Posts: 19
I can create the table and insert the data to that blank table. But I
couldn't insert new data to the exist table. For example,

I already have one user table have several rows record like this in MySQL Database:

UID Uname
1 Apple
2 Banana
3 Cat

Then if I would like to insert the new user " Dog" in to this table
and automatic generate the UID as 4. How to do that?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 3:34 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
show mapping files, and java code

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 4:31 pm 
Newbie

Joined: Mon Jun 21, 2004 3:43 pm
Posts: 19
Here is my mapping file:

Code:
-
   <hibernate-mapping>
-
   <!--

    Created by the Middlegen Hibernate plugin

    http://boss.bekk.no/boss/middlegen/
    http://hibernate.sourceforge.net/
-->
-
   <class name="test.User" table="users">
-
   <id name="uno" type="java.lang.Integer" column="Uno">
<generator class="assigned"/>
</id>
<property name="uname" type="java.lang.String" column="UName" not-null="true" length="25"/>
<!-- associations -->
<!-- bi-directional one-to-many association to Model -->
-
   <set name="models" lazy="true" inverse="true">
-
   <key>
<column name="Uno"/>
</key>
<one-to-many class="test.Model"/>
</set>
</class>
</hibernate-mapping>


And this is my code

Code:
package test;

import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
import net.sf.hibernate.Query;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.type.LongType;




  public class trytest {
    public trytest() {
    }

    public static void main(String[] args) throws Exception {
      Configuration cfg = new Configuration().addClass(User.class);


   //   new SchemaExport(cfg).create(true, true);

      //sessionFactory
      SessionFactory sessions = cfg.buildSessionFactory();
      //sessionFactory
      Session session = sessions.openSession();
      //
     Transaction tx = session.beginTransaction();


        User u1= new User("Maple");

        session.save(u1);
        session.flush();
   
      tx.commit();

      session.close();


      session.close();
       



    }

  }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 25, 2004 2:41 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
your id is "assigned" so you have to set it before calling .save()

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 25, 2004 7:04 pm 
Newbie

Joined: Mon Jun 21, 2004 3:43 pm
Posts: 19
Thank you, I got it!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.