This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

problems with Session.save()

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

I'm pretty new to Hibernate and am facing some issues out here. When i try to insert a new record into the table, the app returns a success message with the generated primary key id. However, the database doesnt reflect the new inserted record.

1) When I check the details on a view screen, I can find the new inserted record. However, if i restart the server, the record is missing from the app too. So, I guess some caching happens which enables me to see my inserted records in that particular server instance.

2) I am using Hibernate 3 with JSF.

here is my hibernate.cfg.xml



This is my code snippet.




Hope to get some solution.
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sridhar,

To be JPA compliant you may perhaps want to use session.persist instead of session.save. You should call the session.flush before the tx.commit.
 
sridhar skrishnan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might sound weird but is it mandatory to have JPA? Besides, I used session.persist and got the same results as my previous post.
 
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by sridhar skrishnan:
This might sound weird but is it mandatory to have JPA? Besides, I used session.persist and got the same results as my previous post.



No, you don't have to use JPA. And by using the hibernate.xfg.xml you are not using JPA.

Anyway, what server are you using? The reason why I ask is because lets say you are using JBoss AS, and you are using the default Hypersonic database DefaultDS datasource, and haven't swapped it out for a real database. In that case, the Hypersonic database is only an in-memory database, so when you restart the server, the database is gone.

Mark
 
sridhar skrishnan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Mark Spritzler:


Anyway, what server are you using? The reason why I ask is because lets say you are using JBoss AS, and you are using the default Hypersonic database DefaultDS datasource, and haven't swapped it out for a real database. In that case, the Hypersonic database is only an in-memory database, so when you restart the server, the database is gone.



But anyways I'm using weblogic 8.1. I havent explicitly named a datasource in my hibrnate.cfg.xml. Is the same behaviour as JBoss, i.e an in-memory DB, to be expected with weblogic?
 
Mark Spritzler
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what Weblogic uses as their default Database.

Anyway? Now looking in more detail at your hibernate.cfg.xml, it looks like you are hooking up to Oracle, which of course is a real database.

And you code looks fine to me.

Mark
 
Mark Spritzler
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see anything wrong in your code or config xml file. Can you post your Mapping file for the Type class?

The thing to remember in terms of what you are seeing is that the object still holds values that is being shown in your UI, so you can't really use the UI as your gage for determining if it was truely saved or not. I also can't think of anything that we might see in your class mapping, except maybe it is going to a different table, but I am doubting that will be the case, but lets see it anyway.

Mark
 
Shailesh Kini
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sridhar,

I still feel you should call the hibernateSession.flush() before you call tx.commit(). Here's my thought... calling flush() synchronizes the entity with the database. However you are calling commit before the synchronization happens and there is nothing to commit as the entity state has not yet been flushed.

Just change the order of these two statements
tx.commit();
hibSession.flush();

to
hibSession.flush();
tx.commit();
 
Mark Spritzler
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Shailesh Kini:
Sridhar,

I still feel you should call the hibernateSession.flush() before you call tx.commit(). Here's my thought... calling flush() synchronizes the entity with the database. However you are calling commit before the synchronization happens and there is nothing to commit as the entity state has not yet been flushed.

Just change the order of these two statements
tx.commit();
hibSession.flush();

to
hibSession.flush();
tx.commit();




Calling commit() on the transaction also executes a flush() so calling flush is redundant in that case.

Mark
 
Shailesh Kini
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,

You are correct, my bad! Thanks for correcting me. We do not need to call flush before commit. Calling commit should flush automatically.
[ December 29, 2007: Message edited by: Shailesh Kini ]
 
Mark Spritzler
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Shailesh Kini:
Mark,

You are correct, my bad! Thanks for correcting me. We do not need to call flush before commit. Calling commit should flush automatically.

[ December 29, 2007: Message edited by: Shailesh Kini ]



I will say that there is a way to make it not flush on commit, but I wouldn't recommend putting in that setting.

Mark
 
sridhar skrishnan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm, i need some examples of hibernate 3 with CMT. In particular, examples which show some insight into creating a SessionFactory and all. Can someone post some links???
 
sridhar skrishnan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add more, I tried out a small stand-alone application with the same table and class files. I used the config files and Util class given in the "Java Persistence with Hibernate" example. This works fine. However, it has not beans, no transaction handling etc,etc. So i guess something must be wrong with my SessionFactory settings and JNDI binding.
 
sridhar skrishnan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I have some pointers on how to solve my problem?

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 28-Nov-2007 17:08:19 by Hibernate Tools 3.2.0.b9 -->
<hibernate-mapping>
<class name="com.beans.Type" table="TYPE">
<id name="Type" type="string">
<column name="TYPE" length="3" />
<generator class="assigned" />
</id>
<property name="TypeDesc" type="string">
<column name="TYPE_DESC" length="25" />
</property>

</class>
</hibernate-mapping>
 
sridhar skrishnan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello people,

I finally solved my problem. I just created a datasource and voila its working fine. Can any one tell me why?
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!