-->
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: handle null object in mapping
PostPosted: Sat Jan 17, 2015 11:19 am 
Newbie

Joined: Sat Jan 17, 2015 10:27 am
Posts: 1
I have my java classes as

Class Parent{

@ID
@GeneratedValue(strategy=GenerationType.Identity)
@Column(id)
Int id;

@oneToOne(mapped by=parent, Cascade=CascadeType.All)
Child1 c1;

@oneToOne(mapped by=parent,Cascade=CascadeType.All)
Child2 c2;

// getter setter

}


Class Child1{

@ID
@GeneratedValue(generator = "gen")
@GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property", value="parent"))
@Column(id)
Int id;

@oneToOne
@primaryKeyJoinColumn
Parent parent;

@oneToMany(mapped by=child1,Cascade=CascadeType.All)
List<Toy> toys;

// getter setter

}


Class Child2{

@ID
@GeneratedValue(generator = "gen")
@GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property", value="parent"))
@Column(id)
Int id;

@oneToOne
@primaryKeyJoinColumn
Parent parent;

@oneToMany(mapped by=child2,Cascade=CascadeType.All)
List<Toy> toys;

// getter setter

}

Class Toy{

@EmbeddedId
ToyPk toyid;

@manytoOne
@joinColumn(name="id1")
@MapsId("id1")
Child1 child1;

@manytoOne
@joinColumn(name="id1")
@MapsId("id1")
Child2 child2

// getter setter
}

@Embeddable
Class ToyPk{

@column("id1")
int id1;

@column("id2")
int id2;

//getter setter
}

At time of save or retrieve, i can have either Parent-Child1-Toy list or Parent-Child2-Toy list. When i save object with Parent-Child1-Toy list, i get exception= "attempted to assign id from null one to one(TOY.child2)".
What should i do?


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.