-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Unable to insert data into database
PostPosted: Wed Oct 12, 2005 4:05 pm 
Newbie

Joined: Thu Oct 06, 2005 9:30 am
Posts: 18
Location: Canada
I am using Hibernate 3.1

Following files are in my lib folder

antlr-2.7.5H3.jar
antlr.license.txt
cglib-nodep-2.1_2.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
hibernate3.jar
hsqldb.jar
jdbc2_0-stdext.jar
jdbc2_0-stdext.licence.txt
jta.jar
lib.txt
log4j-1.2.11.jar
mysql-connector-java-3.0.17-ga-bin.jar

following files are in my src folder
hibernate.cfg.xml
hibernate.properties log4j.properties src.txt
TestUser .java user.hbm.xml User.java

I get the following message when I run ant

Buildfile: build.xml

clean:
[delete] Deleting directory C:\TEMP\test3\bin
[mkdir] Created dir: C:\TEMP\test3\bin

copy-resources:
[copy] Copying 5 files to C:\TEMP\test3\bin

compile:
[javac] Compiling 2 source files to C:\TEMP\test3\bin

run:
[java] INFO - Hibernate 3.1 rc1
[java] INFO - loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=com.mysql.jdbc.Driver, hibernate.cglib.use_reflection_optimizer=true, hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider, hibernate.max_fetch_depth=1, hibernate.dialect=org.hibernate.dialect.MySQLMyISAMDialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.format_sql=true, hibernate.query.substitutions=yes 'Y', no 'N', hibernate.proxool.pool_alias=pool1, hibernate.connection.username=root, hibernate.cache.region_prefix=hibernate.test, hibernate.connection.url=jdbc:mysql://localhost/test, hibernate.show_sql=true, hibernate.connection.password=****, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=1}
[java] INFO - using java.io streams to persist binary types
[java] INFO - using CGLIB reflection optimizer
[java] INFO - using JDK 1.4 java.sql.Timestamp handling
[java] INFO - configuring from resource: /hibernate.cfg.xml
[java] INFO - Configuration resource: /hibernate.cfg.xml
[java] INFO - Reading mappings from resource: user.hbm.xml
[java] INFO - Mapping class: User -> users
[java] INFO - Configured SessionFactory: null
[java] INFO - processing extends queue
[java] INFO - processing collection mappings
[java] INFO - processing association property references
[java] INFO - processing foreign key constraints
[java] INFO - Using Hibernate built-in connection pool (not for production use!)
[java] INFO - Hibernate connection pool size: 10
[java] INFO - autocommit mode: false
[java] INFO - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/test
[java] INFO - connection properties: {user=root, password=****}
[java] INFO - RDBMS: MySQL, version: 4.1.14-nt
[java] INFO - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.6 ( $Date: 2004/12/09 15:57:25 $, $Revision: 1.27.4.52 $ )
[java] INFO - Using dialect: org.hibernate.dialect.MySQLDialect
[java] INFO - Using default transaction strategy (direct JDBC transactions)
[java] INFO - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
[java] INFO - Automatic flush during beforeCompletion(): disabled
[java] INFO - Automatic session close at end of transaction: disabled
[java] INFO - JDBC batch size: 15
[java] INFO - JDBC batch updates for versioned data: enabled
[java] INFO - Scrollable result sets: enabled
[java] INFO - JDBC3 getGeneratedKeys(): enabled
[java] INFO - Connection release mode: null
[java] INFO - Maximum outer join fetch depth: 1
[java] INFO - Default batch fetch size: 1
[java] INFO - Generate SQL with comments: disabled
[java] INFO - Order SQL updates by primary key: disabled
[java] INFO - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
[java] INFO - Using ASTQueryTranslatorFactory
[java] INFO - Query language substitutions: {no='N', yes='Y'}
[java] INFO - Second-level cache: enabled
[java] INFO - Query cache: disabled
[java] INFO - Cache provider: org.hibernate.cache.HashtableCacheProvider
[java] INFO - Optimize cache for minimal puts: disabled
[java] INFO - Cache region prefix: hibernate.test
[java] INFO - Structured second-level cache entries: disabled
[java] INFO - Echoing all SQL to stdout
[java] INFO - Statistics: disabled
[java] INFO - Deleted entity synthetic identifier rollback: disabled
[java] INFO - Default entity-mode: POJO
[java] INFO - building session factory
[java] INFO - Not binding factory to JNDI, no JNDI name configured
[java] INFO - Running hbm2ddl schema update
[java] INFO - fetching database metadata
[java] INFO - updating schema
[java] INFO - processing extends queue
[java] INFO - processing collection mappings
[java] INFO - processing association property references
[java] INFO - processing foreign key constraints
[java] INFO - table found: .users
[java] INFO - columns: [lastname, firstname, id]
[java] INFO - foreign keys: []
[java] INFO - indexes: [primary]
[java] INFO - schema update complete
[java] INFO - cleaning up connection pool: jdbc:mysql://localhost/test
[java] INFO - Checking 0 named queries
[java] Hibernate:
[java] insert
[java] into
[java] users
[java] (FIRSTNAME, LASTNAME, ID)
[java] values
[java] (?, ?, ?)

BUILD SUCCESSFUL
Total time: 4 seconds

mysql> select * from users;
Empty set (0.00 sec)

My user class is
public class User
{
private int id;
private String firstName;
private String lastName;

public User(){
super();
this.firstName=null;
this.lastName=null;
this.id = 0;
}

/*
public User(int id, String firstName, String lastName){
this.id =id;
this.firstName = firstName;
this.lastName = lastName;
}
*/

public int getId(){
return this.id;
}

public String getFirstName( ){
return this.firstName;
}

public String getlastName(){
return this.lastName;
}


public void setId(int id){
this.id = id;
}

public void setFirstName(String firstName){
this.firstName = firstName;
}

public void setLastName(String lastName){
this.lastName = lastName;
}

};

my test user class is

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;



class TestUser
{
public TestUser(){
super();
}

public static void main(String[] args)
{
Session session = null;
User user = new User();
user.setFirstName("Farid");
user.setLastName("Ghafour");
user.setId(3);

try{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
session.save(user);
}// end try
catch(Exception e){
System.out.println(e.getMessage());
}//end catch
finally{
session.flush();
session.close();
}

} //end main

} //end class

hibernate.cfg.xml file contains

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//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/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">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="user.hbm.xml"/>
</session-factory>
</hibernate-configuration>

user.hbm.xml contains
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="User" table="users" >
<id name="id" type="int" column="ID" >
<generator class="assigned"/>
</id>

<property name="firstName">
<column name="FIRSTNAME" />
</property>
<property name="lastName">
<column name="LASTNAME"/>
</property>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 9:43 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
make sure hibernate.hbm2ddl.auto isn't responsible - and you may want to make sure it is the right database ;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 10:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Maybe I missed something, but where are you ever managing a transaction?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 10:22 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
he's got an insert statement is in the log though.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 7:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
dennisbyrne wrote:
he's got an insert statement is in the log though.

Well, that does not mean the transaction commits.


Top
 Profile  
 
 Post subject: Works on one machine
PostPosted: Thu Oct 13, 2005 9:57 am 
Newbie

Joined: Thu Oct 06, 2005 9:30 am
Posts: 18
Location: Canada
I have tried the same project on two different computers with same parameters. On of the computers has older Mysql 3.xx and the the other one has 4.1, it works on the older database, but it does not work on the newer one.


Top
 Profile  
 
 Post subject: Re: Works on one machine
PostPosted: Thu Oct 13, 2005 10:03 am 
Newbie

Joined: Thu Oct 06, 2005 9:30 am
Posts: 18
Location: Canada
farid wrote:
I have tried the same project on two different computers with same parameters. On one of the computers has older Mysql 3.xx and the the other one has 4.1, it works on the older database, but it does not work on the newer one.


Top
 Profile  
 
 Post subject: Re: Works on one machine
PostPosted: Thu Oct 13, 2005 10:04 am 
Newbie

Joined: Thu Oct 06, 2005 9:30 am
Posts: 18
Location: Canada
I have tried the same project on two different computers with same parameters. On one of the computers has older Mysql 3.xx and the the other one has 4.1, it works on the older database, but it does not work on the newer one.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 10:25 am 
Newbie

Joined: Wed Oct 12, 2005 7:06 pm
Posts: 10
Did u try with <generator class="increment"/> in my sql 4.1 (I had this problem when it was class="assigned")

Lachu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 10:39 am 
Beginner
Beginner

Joined: Mon Aug 16, 2004 6:15 am
Posts: 24
Did your MySQL v3 install use InnoDB as the database engine? If not then you probably wouldn't have had any transactional support, hence the insert actually appearing to commit.

If your MySQL v4 is using InnoDB, then you do have transactional support and would need to commit any database changes before they are reflected correctly.


Top
 Profile  
 
 Post subject: Re: Generator
PostPosted: Thu Oct 13, 2005 10:39 am 
Newbie

Joined: Thu Oct 06, 2005 9:30 am
Posts: 18
Location: Canada
I did try <generator class="assigned"/>, took it out and made a database with no primary key. Still it worked on the older Mysql but not on Mysql 4.1. I also disabled the firewalls to make sure it is not blocking, but still was unable to insert into database.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 10:44 am 
Newbie

Joined: Thu Oct 06, 2005 9:30 am
Posts: 18
Location: Canada
when I do

finally{
session.flush();
session.close();
}

I think it should commit any kind of insertion


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 11:47 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
flush is an implicit commit. but it appears you muct call beginTransaction as well.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 2:18 pm 
Newbie

Joined: Thu Oct 06, 2005 9:30 am
Posts: 18
Location: Canada
Thanks dennis. Finally got it working


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 3:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
dennisbyrne wrote:
flush is an implicit commit.

Not correct. You must be referring to auto-commit at the jdbc level. That is *not* the recommended approach.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.