File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes cannot insert record in MS SQLServer 2000 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "cannot insert record in MS SQLServer 2000" Watch "cannot insert record in MS SQLServer 2000" New topic

cannot insert record in MS SQLServer 2000

peter tong
Ranch Hand

Joined: Mar 15, 2008
Posts: 245
hibernate version: 3.x
database: MS SQL Server 2000

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

<property name="hibernate.connection.driver_class"></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="">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<class name="roseindia.tutorial.hibernate.Contact" table="CONTACT">
<id name="id" type="int" column="ID" >
<generator class="assigned"/>

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

.java file
package roseindia.tutorial.hibernate;

* @author Deepak Kumar
* 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'', 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??
peter tong
Ranch Hand

Joined: Mar 15, 2008
Posts: 245
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();
I agree. Here's the link:
subject: cannot insert record in MS SQLServer 2000
It's not a secret anymore!