-->
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.  [ 8 posts ] 
Author Message
 Post subject: Cascade implemented in Hibernate or in database ?
PostPosted: Thu Sep 29, 2005 3:12 am 
Beginner
Beginner

Joined: Tue Sep 20, 2005 4:32 am
Posts: 29
Location: Cluj-Napoca
What is it the best solution to manage the "cascade" ?

Implement it only in Hibernate mappings (ex. cascade="all-delete-orphan") or implement it only in database (ex. "foreign key cacade delete").

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 11:49 am 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
I think that if you have the choice, you should let Hibernate control that.

I personally even prefer to let Hibernate create my database schema. It is a lot easier to maintain and it allows for easier testing using java memory database etc.

You do this by putting the parameter
<property name="hibernate.hbm2ddl.auto">update</property>
in your hibernate.cfg.xml

see doc for other values.

Hope that helps.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 12:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
cascades in the database are generally much more efficient


Top
 Profile  
 
 Post subject: as usual it depends:
PostPosted: Thu Sep 29, 2005 12:23 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
It depends: if your application is the only application that accesses the database then you can do whatever you want, but it is seldom the case.
If many applications work with the same database ("shared DB pattern") then it is better to push as much functionality and checks as possible down to DB layer.

In this case it does not matter if .NET program or SQL script are trying to manipulate the data too– DB level checks will safeguard the data.

PS: I recommend using SQL script to generate and populate database with test data and do not use schema auto-generation.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 1:19 pm 
Newbie

Joined: Thu Jul 14, 2005 8:21 pm
Posts: 9
If you let the db cascade, doesn't that leave orphaned records in hibernate's cache?


Top
 Profile  
 
 Post subject: orphans
PostPosted: Thu Sep 29, 2005 1:27 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Theoretically it might. But when several different programs modify data in the same database then validity of cache is always questionable. Cache (especially 2nd level) requires careful planning

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 3:24 am 
Beginner
Beginner

Joined: Tue Sep 20, 2005 4:32 am
Posts: 29
Location: Cluj-Napoca
Firstly, thanks for your answers.

Personally I prefer to implement the cascade operations into db (efficiency reason and portability) and create db scripts manually or generated by some tools where I create also the data model.

But the other question is:
Implemeting cascade into db can be a source of conflicts with Hibernate persistence manager (error messages like "Inconsistent state of data..." or others) ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 3:36 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
farmer wrote:
If you let the db cascade, doesn't that leave orphaned records in hibernate's cache?


No, as long as you tell Hibernate what is going on will cascade="delete" and on-delete="cascade". Read the documentation.


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