-->
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.  [ 3 posts ] 
Author Message
 Post subject: Problem of Id
PostPosted: Wed Dec 13, 2006 10:26 am 
Newbie

Joined: Wed Dec 13, 2006 9:34 am
Posts: 6
hello

I've got a problem when i try to insert into my database with hibernate.
I'm a beginner so i try on this small project to understand hibernate.

i use a MYSQL database and this bean



Code:
public class personne
{
    private int id;
    private String nome;
   
    /** Creates a new instance of Personne */
    public personne()
    {
    }
   
    public personne(String s)
    {
        this.nom = s;
    }
   
    public int getId()
    {
        return id;
    }
    public void setId(int i)
    {
        this.id = i;
    }
    public void setNom(String n)
    {
        this.nom = n;
    }
    public String getNom()
    {
        return this.nom;
    }
   
}


So, I use the following personne.hbm.xml file :

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="personne" table="personne">
        <id name="id" type="int" column="id">
            <generator class="native" />
        </id>
        <property name="nom" type="string" not-null="true" />
    </class>
</hibernate-mapping>


And i use this code to insert
Code:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();       
       
        Transaction tx = null;
        try
        {
         tx = session.beginTransaction();
         personne p = new personne("maurice");
         session.save(p);
         session.flush();
         tx.commit();
        }
        catch(Exception e)
        {
            if(tx != null)
                tx.rollback();
           
            System.out.println(e.getMessage());
        }
        finally
        {
            sessionFactory.close();
        }


When i insert i've an error which tell me that field ID doesn't have a defalut value.

I believe that it was hibernate which generate the id ?
Isn't it ?
Where is the problem ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 10:43 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I don't have much experience with mysql, but you need to make sure that the id column is generating its own value. I think mysql has autoincrementing columns like sql server.


and tx.commit() automatically flushes the session.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 10:45 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
Try to use unsaved-value="0" attribute of id element in mapping. Did you define id column in database as identity column?

_________________
Best Regards


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