-->
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.  [ 9 posts ] 
Author Message
 Post subject: Inserting record in hibernate!
PostPosted: Mon Jun 20, 2011 7:53 am 
Newbie

Joined: Mon Jun 20, 2011 7:39 am
Posts: 6
Hi
I'm trying to insert a record to DataBase, the program run without any exception!
the table is created but, no rows exist!
I used hibernate and mysql.
please someone help me.
here is the code of my main method:

Code:
public static void main(String[] args) {
      
      Session session = null;

      try{
         
         SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
          session =sessionFactory.openSession();
            System.out.println("creating table done!");
                Person person= new Person();
                person.setNationalNumber(12345);
                person.setFamily("A");
                person.setName("M");
                person.setLocation("Tehran");
               
                session.save(person);
                      
      }catch(Exception e){
         System.out.println(e.getMessage());
      }
      finally{
         
         session.flush();
         session.close();

         }
      
   }



Top
 Profile  
 
 Post subject: Re: Inserting record in hibernate!
PostPosted: Mon Jun 20, 2011 12:50 pm 
Beginner
Beginner

Joined: Mon Nov 15, 2010 10:39 am
Posts: 27
That looks pretty correct to me, could you add your Hibernate config file and mapping files/annotated class?

Also, you could try to add this in your Hibernate config file:

<property name="hibernate.show_sql">true</property>

Then you should be able to see whether SQL statements are actually sent to your database.


Top
 Profile  
 
 Post subject: Re: Inserting record in hibernate!
PostPosted: Mon Jun 20, 2011 4:43 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You should try working with transactions. My guess is that the insert is sent to the database but when you close the session, it is rolled back. See http://docs.jboss.org/hibernate/core/3. ... emarcation for a good starting point with some code templates.


Top
 Profile  
 
 Post subject: Re: Inserting record in hibernate!
PostPosted: Wed Jun 22, 2011 12:56 am 
Newbie

Joined: Mon Jun 20, 2011 7:39 am
Posts: 6
Tobb and nordborg, thanks for your reply :) here is my cfg.xml and hbm.xml codes:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatetest</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password"></property>
      <property name="hibernate.connection.pool_size">10</property>
      <property name="show_sql">true</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <!-- Mapping files -->
      <mapping resource="Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 15, 2011 2:03:10 PM by Hibernate Tools 3.4.0.CR1 -->


<hibernate-mapping>

    <class name="domain.Person" table="PERSON">
        <id name="nationalNumber" type="int" column="NATIONALNUMBER">
           <generator class="increment"/>
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="family" type="java.lang.String">
            <column name="FAMILY" />
        </property>
        <property name="location" type="java.lang.String">
            <column name="LOCATION" />
        </property>
       
       
       
    </class>
</hibernate-mapping>



Top
 Profile  
 
 Post subject: Re: Inserting record in hibernate!
PostPosted: Wed Jun 22, 2011 2:23 am 
Newbie

Joined: Mon Jun 20, 2011 7:39 am
Posts: 6
nordborg wrote:
You should try working with transactions. My guess is that the insert is sent to the database but when you close the session, it is rolled back. See http://docs.jboss.org/hibernate/core/3. ... emarcation for a good starting point with some code templates.


dear nordborg,
thank you for your advice.
I used transaction and it worked,but now I have a new Problem :(
I used it in my sample project,when I did it on my real project,it gave me this exception:

Exception: Could not execute JDBC batch update
!!!

do you know what does it for?!!!


Top
 Profile  
 
 Post subject: Re: Inserting record in hibernate!
PostPosted: Wed Jun 22, 2011 2:44 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
That is a generic exception indicating that something went wrong when executing SQL on the database. You'll need to dig up the complete stacktrace which usually contains more information. Eg.

Code:
...
catch(Exception e) {
  e.printStackTrace(); // instead of System.out.println(e.getMessage());
}
...


Top
 Profile  
 
 Post subject: Re: Inserting record in hibernate!
PostPosted: Wed Jun 22, 2011 3:28 am 
Newbie

Joined: Mon Jun 20, 2011 7:39 am
Posts: 6
Thanks a lot :)

I find it!
in my real project, I have a Boolean type
when hibernate-mapping run,it map boolean type to BIT(1)!!
and when an object is inserting into table,it wanna insert "TRUE" !
when I changed it to BIT(8) or BOOLEAN int table,it worked!! :)

Do you know if it's a bug of hibernate or I have to add some extra configuration to my hibernate configuration code?


Top
 Profile  
 
 Post subject: Re: Inserting record in hibernate!
PostPosted: Wed Jun 22, 2011 3:41 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Yes a boolean is mapped to bit(1) but I have never had any problem with it, but I have seen other that have the following in their configuration files:

Code:
<property name="hibernate.query.substitutions">true 1, false 0</property>


Try it and see if it helps. Maybe it depends on the MySQL version.


Top
 Profile  
 
 Post subject: Re: Inserting record in hibernate!
PostPosted: Wed Jun 22, 2011 4:14 am 
Newbie

Joined: Mon Jun 20, 2011 7:39 am
Posts: 6
nordborg wrote:
Yes a boolean is mapped to bit(1) but I have never had any problem with it, but I have seen other that have the following in their configuration files:

Code:
<property name="hibernate.query.substitutions">true 1, false 0</property>


Try it and see if it helps. Maybe it depends on the MySQL version.


dear nordborg,
Thank you very much, you helped me a lot. :)


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