• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Unable to persist the data in hibernate

 
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am just started using hibernate. I am able to retrieve the hibernate data from the database but failing to insert the same to the table. Please let me know the mistake I have made!

CD.java

CD cd1 = new CD("Music1","artist1",new Date(),9.99);
CD cd2 = new CD("Music2","artist2",new Date(),9.99);

Configuration cfg = new Configuration().addClass(CD.class);
/*
Properties properties = cfg.getProperties();
Enumeration enum = properties.keys();
String key = null;
while(enum.hasMoreElements())
{
key =(String)enum.nextElement();
System.out.println( key +"---->"+ properties.getProperty(key) );
}
*/
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(cd1);
session.save(cd2);
session.flush();


<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<mapping resource="CD.hbm.xml"/>
</session-factory>
</hibernate-configuration>
---------------------------------------------

import java.io.*;
import java.util.*;

public class CD implements Serializable{
int id;
String title;
String artist;
Date purchasedate;
double cost;


public CD()
{
}

public CD(String title, String artist, Date purchaseDate, double cost)
{
this.title = title;
this.artist= artist;
this.purchasedate=purchaseDate;
this.cost = cost;
}

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

public int getId()
{
return id;
}

public void setTitle(String title)
{
this.title = title;
}

public String getTitle()
{
return title;
}

public void setArtist(String artist)
{
this.artist = artist;
}

public String getArtist()
{
return artist;
}

public void setPurchasedate(Date purchasedate)
{
this.purchasedate = purchasedate;
}

public Date getPurchasedate()
{
return purchasedate;
}

public void setCost(double cost)
{
this.cost = cost;
}

public double getCost()
{
return cost;
}

public void print()
{
//System.out.println("CD Id -->"+ id);
System.out.println("CD Title -->"+ title);
System.out.println("CD Artist -->"+ artist);
System.out.println("CD Date -->"+ purchasedate);
System.out.println("CD Cost -->"+ cost);
}

}
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the exception message and stacktrace?
 
Malli Raman
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Scott Johnson:
What is the exception message and stacktrace?


No exception or error. The program simply completes the execution.

Is it do I need to check with the configuration file

Thanks,
M.S.Raman
 
Malli Raman
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Malli Raman:

No exception or error. The program simply completes the execution.

Is it do I need to check with the configuration file

Thanks,
M.S.Raman



I got the answer. I need to commit the transaction. earlier I thought that session.flush() by default will commit the transaction. Once I added session.connection().commit() the transaction gets commited.

Thanks,
M.S.Raman
 
I'm so happy! And I wish to make this tiny ad happy too:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic