-->
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.  [ 2 posts ] 
Author Message
 Post subject: cannot insert record in MS SQLServer 2000
PostPosted: Sat Oct 11, 2008 2:46 pm 
Newbie

Joined: Mon Jun 20, 2005 11:14 pm
Posts: 13
hibernate version: 3.x
database: MS SQL Server 2000

cfg.xml
<?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.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:microsoft:sqlserver://localhost:1433;databaseName=hibernatetutorialSQLServer</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">sa</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>

hbm.xml
<?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="roseindia.tutorial.hibernate.Contact" table="CONTACT">
<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>
<property name="email">
<column name="EMAIL"/>
</property>
</class>
</hibernate-mapping>

.java file
package roseindia.tutorial.hibernate;

/**
* @author Deepak Kumar
*
* http://www.roseindia.net
* Java Class to map to the datbase Contact Table
*/
public class Contact {
private String firstName;
private String lastName;
private String email;
private int id;

/**
* @return Email
*/
public String getEmail() {
return email;
}

/**
* @return First Name
*/
public String getFirstName() {
return firstName;
}

/**
* @return Last name
*/
public String getLastName() {
return lastName;
}

/**
* @param string Sets the Email
*/
public void setEmail(String string) {
email = string;
}

/**
* @param string Sets the First Name
*/
public void setFirstName(String string) {
firstName = string;
}

/**
* @param string sets the Last Name
*/
public void setLastName(String string) {
lastName = string;
}

/**
* @return ID Returns ID
*/
public int getId() {
return id;
}

/**
* @param l Sets the ID
*/
public void setId(int l) {
id = l;
}

}

table structure:
id int 4
FIRSTNAME nvarchar(50)
LASTNAME nvarchar(50)
EMAIL nvarchar(50)

in eclipse, everything go right and the sql is shown correctly
Hibernate: insert into CONTACT (FIRSTNAME, LASTNAME, EMAIL, ID) values (?, ?, ?, ?)

in sql profiler, the statement is
exec sp_executesql N'insert into CONTACT (FIRSTNAME, LASTNAME, EMAIL, ID) values (@P1, @P2, @P3, @P4)', N'@P1 nvarchar(4000) ,@P2 nvarchar(4000) ,@P3 nvarchar(4000) ,@P4 int ', N'Deepak', N'Kumar', N'deepak_38@yahoo.com', 6

I try to run this statement in sql analyzer and it really can create a record.
but if just run in eclipse, no exception, the sql is shown, but no record is added in the table!!
what is the problem??


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 12, 2008 2:10 am 
Newbie

Joined: Mon Jun 20, 2005 11:14 pm
Posts: 13
I find the solution by myself finally.
need to use transaction, seesion.flush is not enough!!

Transaction tx;
tx = session.beginTransaction();
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(7);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_38@yahoo.com");
session.persist(contact);
//session.flush();
tx.commit();


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