-->
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.  [ 1 post ] 
Author Message
 Post subject: does cascade-merge work for entire object graph
PostPosted: Sat Mar 07, 2009 5:02 am 
Newbie

Joined: Sat Jan 31, 2009 2:00 am
Posts: 3
Guys ,

I posted one question but no body answered my question and i guess i know the reason ( I think it was length of my post and all the code i wrote in that post :)) ). So this time i m going to frame my question differently.

Ok..here i go (Please have patience i am a hibernate newbie)

I am trying to add a grandchild to an object and while doing that i also have to create child of that object. Cascade.All is mentioned in all three classes . So my question is hibernate should be able to generate id for child and then grand child and persist the whole graph..right ? as per transitive persistance..

Here is what is happening... hibernate does not generate id for child untill the end of transaction and grandchild does not get that id so whole transaction fails with

Caused by: org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property: userImage


However if i split this operation into two parts first i save (UserImage) commit the tx and then in another tx save image it works like charm.

Here is the code snippet

Code:
User UserImage and Image

User {
collection<UserImage> userimageCollection ;

@OneToMany(cascade = {CascadeType.ALL},mappedBy = "user",fetch=FetchType.EAGER)
    private List<UserImage> userImagCollection;

public void  addUserImage(UserImage) {
userimageCollection.add(userImage);
userImage.add(user);

}

}

UserImage {
@Id
    @GeneratedValue(strategy=GenerationType.TABLE, generator="tg")
    @Column(name = "id", nullable = false)   
    private long id;

   @OneToOne(cascade = CascadeType.ALL, mappedBy = "userImage")
    private Image image;
}


Image {
@Id
    @GeneratedValue(generator = "myForeignGenerator")
    @org.hibernate.annotations.GenericGenerator(
          name = "myForeignGenerator",
          strategy = "foreign",
          parameters = @Parameter(name = "property", value ="userImage")
          )

@JoinColumn(name = "id", referencedColumnName = "id", insertable = false, updatable = false)
    @OneToOne
    private UserImage userImage;

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

}
To add one image in db I do following
@Transactional
updateUser{

UserImage userImage = new UserImage();
Image image = {get from front end completely populated "New" image object}
userImage.setImage(image);
image.setUserImage(userImage)

User user= getFromDb();
user.addUserImage(userImage);
}



[/code]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.