-->
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.  [ 2 posts ] 
Author Message
 Post subject: need @OneToOne mapping help
PostPosted: Thu Mar 05, 2009 6:30 am 
Newbie

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

I am facing this annoying issue with @OneToOne mapping.


Set up is very simple.

class User {

@OneToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH},mappedBy = "user",fetch=FetchType.EAGER)

private List<UserImage> userImagCollection;

public void addImage(UserImage image) {
if(userImagCollection == null) {
userImagCollection = new ArrayList<UserImage>();
}
image.setUser(this);
userImagCollection.add(image);

}


}

class UserImage {

@TableGenerator(name="tg", table="dw_pk_table",
pkColumnName="name", valueColumnName="value",pkColumnValue="UserImage_id",
allocationSize=10
)
@GeneratedValue(strategy=GenerationType.TABLE, generator="tg")
@Column(name = "id", nullable = false)
private long id;

@OneToOne(cascade = CascadeType.ALL, mappedBy = "userImage")
private Image image;
@JoinColumn(name = "usr_id", referencedColumnName = "usr_id")
@ManyToOne
private User user;
}


class Image

{
public class Image implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "myForeignGenerator")
@org.hibernate.annotations.GenericGenerator(
name = "myForeignGenerator",
strategy = "foreign",
parameters = @Parameter(name = "property", value ="userImage")
)
@Column(name = "id", nullable = false)
private long id;
@Lob
@Column(name = "img")
private byte[] img;
@JoinColumn(name = "id", referencedColumnName = "id", insertable = false, updatable = false)
@OneToOne
private UserImage userImage;

}


Simplified veriosn of code.


UserImage userImage = new UserImage();
userImage

UserImage userImage = new UserImage();
userImage.setImage(image);
image.setUserImage(userImage);
user.addImage(userImage);

entitymanger.merge(user)


I think i have wired all the ends.

but merge throws

ed: Db operation failed: Could not update: user: org.hibernate.id.IdentifierGene
rationException: attempted to assign id from null one-to-one property: userImage
: attempted to assign id from null one-to-one property: userImage



I know the reason it is happening ..The reason is userImage object id is null but hibernate should able to handle it. Even if i am creating a new UserImage object , hibernate should get the id from db as per generation strategy and sit in child object (image ) save the data.

In my view that is what the transitive persistance is.

Thanks in advance..


Top
 Profile  
 
 Post subject: Re: need @OneToOne mapping help
PostPosted: Tue Feb 16, 2010 2:05 pm 
Newbie

Joined: Tue Feb 16, 2010 1:59 pm
Posts: 2
Hi,
Did you solve this problem?.

Thanks
Rohit


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