-->
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: basics: saving an object graph
PostPosted: Sun Feb 04, 2007 7:07 am 
Newbie

Joined: Sun Apr 16, 2006 1:02 pm
Posts: 12
Hibernate version:
3.2

Hi!

Urls holds urls and gzhtml holds the gzipped HTML code. Pretty simple.

Code:
public class Urls
{
    @Id
    @GeneratedValue( generator = "hibernate-uuid" )
    private String id;

    @Column( name = "url" )
    private String url;

    @OneToOne
    @PrimaryKeyJoinColumn
    private GzHtml gzHtml;

Code:
public class GzHtml
{
    @Id
    @Column( name = "url_id" )
    private String urlId;

    @Column( name = "html" )
    private byte[] html;


Now I simply want to save a new entity but the following code only saves the Urls entity, not GzHtml. Why? What am I doing wrong?

Code:
       Urls u = new Urls();
       u.setUrl( url.toString() );
       
       GzHtml h = new GzHtml();
       h.setHtml( Utility.compress( html ) );

       u.setGzHtml( h );
       session.save( u );


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 04, 2007 11:32 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
You must set the Cascade attribute on the "parent" object. Here it is Cascade.SAVE or something like this (I don't use annotations but it's the same principle).

If you don't put the cascade property, you will have to call save on each objects.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 04, 2007 11:37 am 
Newbie

Joined: Sun Apr 16, 2006 1:02 pm
Posts: 12
Code:
@OneToOne(cascade = CascadeType.ALL)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 04, 2007 12:58 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
someone12345 wrote:
Code:
@OneToOne(cascade = CascadeType.ALL)

Yes, if this is a parent/child relationship you have. Putting this, you ask hibernate to propagate saves, updates, deletes and so on.

But pay attention, I don't know if it's the same for hibernate annotations CascadeType class, but it would be quite coherent : settting ALL with classical/old XML configuration style does not include delete-orphan rule...

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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.