-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem in inserting Data in Table using Hibernate3.2
PostPosted: Fri Oct 20, 2006 3:50 am 
Newbie

Joined: Fri Oct 20, 2006 2:55 am
Posts: 8
Hi Experts!!

I am begnnier in using Hibernate.
I am using Hibernate3.2 with NetBeans5.5 IDE

all the things work out finely but data is not being inserted my Sybase database table.

Here is my problem in depth ready for your reference:: :-)

1) Table structure (Say table Contact)

columnName type constraints
-----------------------------------------------------------------------

empId int notnull,primary key
firstName nvarchar
lastName nvarchar
email nvarchar

==========================================
2) java file for my table say Contact.java

public class Contact {

int empId;
String firstName;
String lastName;
String email;

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public String getEmail() {
return email;
}

public int getEmpId() {
return empId;
}

public void setEmpId(int empId) {
this.empId = empId;
}

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

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

public void setEmail(String email) {
this.email = email;
}
}
============================================
3) .xml file for the same .... say contact.hbm.xml

<!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="Test.mdave.Contact">
<id name="empId" type="int" column="empId" >
<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> -->

<property name="firstName"/>
<property name="lastName"/>
<property name="email"/>

</class>
</hibernate-mapping>
=========================================
4) My Hibernate configuration file .....

<hibernate-configuration>
<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">com.sybase.jdbc2.jdbc.SybDriver</property>
<property name="connection.url">jdbc:sybase:Tds:10.100.112.48:5000/Test</property>

<property name="connection.username">mdave</property>
<property name="connection.password">mdave1</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SybaseDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">create</property> -->
<mapping resource="roseindia/tutorial/hibernate/contact.hbm.xml"/>

</session-factory>
</hibernate-configuration>
===============================================
5) This is my mail class file

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

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

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();

//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record .......");
Contact contact = new Contact();

contact.setEmpId(2);
contact.setFirstName("mike");
contact.setLastName("dave");
contact.setEmail("mike@hibernate.com");

session.save(contact);

System.out.println("Done");
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
try{
session.flush();
session.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}

==============================================
6) This is what appear in out put window of NetBeans5.5 IDE

run:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Inserting Record .......
Done
Hibernate: insert into Test.mdave.Contact (firstName, lastName, email, empId) values (?, ?, ?, ?)
BUILD SUCCESSFUL (total time: 16 seconds)
============
Please note that i have used Syabase database and when i run my program it gives me error.....



Thanks U All in Advance !!!!!

_________________
I am What I Am


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 20, 2006 9:57 am 
Senior
Senior

Joined: Wed Aug 17, 2005 12:56 pm
Posts: 136
Location: Erie, PA (USA)
This looks like a transaction issue -- the transaction is not getting committed and therefore never shows up in the db. You could configure Hibernate to "auto commit", although that is probably not the best solution, or, the better solution would be to, explicitly declare and handle the transaction.

Curtis ...

_________________
---- Don't forget to rate! ----


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 2:48 am 
Newbie

Joined: Fri Oct 20, 2006 2:55 am
Posts: 8
CWitherow wrote:
This looks like a transaction issue -- the transaction is not getting committed and therefore never shows up in the db. You could configure Hibernate to "auto commit", although that is probably not the best solution, or, the better solution would be to, explicitly declare and handle the transaction.

Curtis ...



Hi Curtis

Thanks alot for providing me your helping hand.
I have tried to run my application after making changes as per your suggestion.Though it is not inseting the records into my DB..

I have gone through some stuff in which it is mentioned that all the transactions in Hibernate are set to true value by default.
means
auto-commit = "true"

As being bennier i dont know how to solv my problem just by exlplicitly declaring and handling the transaction :-( ....

Will it solve my purpose?

I have checked out each and every line of my code so many times buy could not be able to come out from the pit!!!!


Please guide me ....


Thanks n Regards.
Mike

_________________
I am What I Am


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 6:11 am 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
Hi. Add transaction demarcation to your code:

(...)
session =sessionFactory.openSession();
session.beginTransaction();

//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record .......");
Contact contact = new Contact();

contact.setEmpId(2);
contact.setFirstName("mike");
contact.setLastName("dave");
contact.setEmail("mike@hibernate.com");

session.save(contact);
sesion.getTransaction().commit();

System.out.println("Done");
(...)

Hope this helps...

_________________
andresgr (--don't forget to rate)


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