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.  [ 3 posts ] 
Author Message
 Post subject: OneToMany mapping with compositeId
PostPosted: Thu Oct 26, 2006 9:21 pm 
Newbie

Joined: Tue Oct 24, 2006 9:21 pm
Posts: 12
Hi,

Can you map an entity as OneToMany with a composite Id

e.g.

Class A has a compositeId cmp_PK

Class A has a collection of class B (OneToMany)


can you have a OneToMany mapping to class B such that the joining table result to:


A_id_componentOne, A_id_componentTwo, B_id

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 10:07 pm 
Newbie

Joined: Tue Oct 24, 2006 9:21 pm
Posts: 12
It appears that in the annotation manual there is:

"Mapping composite primary and foreign keys"


But I seem to be getting some cascade saving problems:


I have the folowing:

@Embeddable
class CompositeId {

private Long id;
private String type;
}

class B {
@Id
private Long id;

}


class A {

@Id
CompositeId id;

@OneToMany(cascade =
{CascadeType.PERSIST,CascadeType.MERGE })
@JoinTable(
name="JoinTable",
joinColumns = {
@JoinColumn(name="aId", referencedColumnName = "id"),
@JoinColumn(name="aType", referencedColumnName = "type"),
},
inverseJoinColumns = @JoinColumn( name="bId"))
@IndexColumn(name="index")
Collection<B> children;

}


But I'm getting a foreign key constratint violation on table B on column id. When I enabled show_sql=true I noticed that it is doing the following:

1) Inserting row for table A
2) It then tries to insert row on JoinTable -- fails because it has a foreign key constraint on Table B



how do I set this so that row in table A gets inserted first, then row in table B then row in JoinTable.

any help would be much appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 12:16 pm 
Newbie

Joined: Tue Oct 24, 2006 9:21 pm
Posts: 12
solution:

It seems that the reason the cascade was not happening is because i have

cascade=CascadeType.MERGE, CascadeTpe.Persist as my annotation but i am calling sesion.save()


Once I've changed it to Cascade.ALL all seems fine.


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