wood burning stoves*
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
Author

cannot insert record in MS SQLServer 2000

peter tong
Ranch Hand

Joined: Mar 15, 2008
Posts: 240
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??
peter tong
Ranch Hand

Joined: Mar 15, 2008
Posts: 240
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();
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: cannot insert record in MS SQLServer 2000