• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How this is happening in Hibernate?

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I am using Hibernate to develop web application.I have a point that after saving values from my web application to database in localhost.When I undeploy application from my localhost.All values in saved database are also being deleted.However table structure is there but values are empty?

Why this is happening?

Thanks
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And are you sure the data is actually in the database (table) after saving the values (and before undeploying the application)?

And what kind of database are you using? MySQL? Oracle? Or an in-memory database perhaps?
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok this post might be lengthy

This is my model class


This is my bean.xml file



This is my dao Impl class


and this is MyServlet code from where I am calling this Dao method


I hope know its more clear
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:I hope know its more clear

No, it's not! I asked 2 pretty straightforward questions. You replied with a very lengthy post but it doesn't answer either of my questions...

I also wonder why you have a Dao class for updating and another one for creating. I have never seen this approach. You have a Dao interface (and implementing class) with CRUD methods.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I also wonder why you have a Dao class for updating and another one for creating. I have never seen this approach. You have a Dao interface (and implementing class) with CRUD methods.


because if i will use only create command then it will always create a new table previous record will be delete.
If I use only update when first time I run I will get exception that table not exists.

So I have made a custom logic if request is first time or already a table is created.
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would never rely on hbm2ddl in a production environment. It's ok for development purposes, but a risk for a production envorinment. I always create my database scripts manually and execute them manually.

And it seems it's not only me who doesn't want to rely on hbm2ddl in a production environment. Here and here you'll find more user experiences.

Hope it helps!
Kind regards,
Roel
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what you suggest in my case to change this ?
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
write the ddl statements (to create/alter tables and other database objects) yourself and execute them manually
don't use hbm2ddl in your application
write 1 Dao for each entity with create, read, update, delete, find and other required methods

consider Spring to handle transaction management using annotation instead of creating transactions in the code yourself. And if you do, consider Spring Data JPA as well.
 
Paul Clapham
Sheriff
Posts: 21586
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for why Hibernate recreates empty database tables before each run (if that's what's actually happening): there's a configuration property which tells Hibernate to do that (or not). It's this one, if I'm not mistaken:

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will update my post soon on this topic
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic