-->
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.  [ 13 posts ] 
Author Message
 Post subject: How to create an identified 1xN relation ?
PostPosted: Thu Jan 14, 2010 5:48 pm 
Newbie

Joined: Thu Jan 14, 2010 5:43 pm
Posts: 8
Hello!

I want to translate this to my classes using Annotations:

Image

How do I do it?



Some code that may help:
Code:
@Entity
public class Book {
    @Id
    private int idBook;
    private String title;
    //getters & setters...
}


@Entity
public class Edition {
    @Id
    private int idEdition;
    private int yearPublished;
    //getters & setters...
}


Top
 Profile  
 
 Post subject: Re: How to create an identified 1xN relation ?
PostPosted: Thu Jan 14, 2010 6:03 pm 
Newbie

Joined: Mon Jan 11, 2010 11:05 am
Posts: 8
in your class Book, you can add :

@OneToMany(cascade = { CascadeType.ALL })
@JoinColumn(name="Book_idBook")
private Set<Edition> editions;

getter/setter...


Top
 Profile  
 
 Post subject: Re: How to create an identified 1xN relation ?
PostPosted: Thu Jan 14, 2010 6:17 pm 
Newbie

Joined: Thu Jan 14, 2010 5:43 pm
Posts: 8
kaza7 wrote:
in your class Book, you can add :

@OneToMany(cascade = { CascadeType.ALL })
@JoinColumn(name="Book_idBook")
private Set<Edition> editions;

getter/setter...


But that just generates me a non-identified relation (that is, the FK isn't PK also).


Top
 Profile  
 
 Post subject: Re: How to create an identified 1xN relation ?
PostPosted: Wed Jan 20, 2010 5:18 pm 
Newbie

Joined: Thu Jan 14, 2010 5:43 pm
Posts: 8
Wow, no one knows?
I thought it was simple.


Top
 Profile  
 
 Post subject: Re: How to create an identified 1xN relation ?
PostPosted: Thu Jan 21, 2010 4:20 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Please explain more in detail what you understand with a identified 1xN relation.
For instance you have a determinate book which has 3 editions.
How many book instances do you want to have in the db?
Just one or 3 (a instance for each edition) ?


Top
 Profile  
 
 Post subject: Re: How to create an identified 1xN relation ?
PostPosted: Thu Jan 21, 2010 4:43 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
anyway I would suggest you following:
Code:
in your class Book:
@javax.persistence.OneToMany(mappedBy = "bookId")
protected Set<Edition> editions = new java.util.HashSet();


in your class Edition:
Code:
@Id @GeneratedValue
private long id;

@Column
private long edtion;     

@javax.persistence.ManyToOne
protected Book bookId = null;


Otherwise, if you don't want to have the generated id value in class Edition,
you need a composite Id composed by edition + bookid.
In such case you must use the IdClass annotation


Top
 Profile  
 
 Post subject: Re: How to create an identified 1xN relation ?
PostPosted: Thu Jan 21, 2010 8:32 am 
Newbie

Joined: Thu Jan 14, 2010 5:43 pm
Posts: 8
pb00067 wrote:
anyway I would suggest you following:
Code:
in your class Book:
@javax.persistence.OneToMany(mappedBy = "bookId")
protected Set<Edition> editions = new java.util.HashSet();


in your class Edition:
Code:
@Id @GeneratedValue
private long id;

@Column
private long edtion;     

@javax.persistence.ManyToOne
protected Book bookId = null;


Otherwise, if you don't want to have the generated id value in class Edition,
you need a composite Id composed by edition + bookid.
In such case you must use the IdClass annotation


Yes, what I want is to have a composite key (edition + bookId) both being primary keys, exactly as shown in the picture.
Could you show the use of this IdClass annotation in this example, please?

Thanks!


Top
 Profile  
 
 Post subject: Re: How to create an identified 1xN relation ?
PostPosted: Thu Jan 21, 2010 10:37 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Which picture do you mean?


Top
 Profile  
 
 Post subject: Re: How to create an identified 1xN relation ?
PostPosted: Thu Jan 21, 2010 12:08 pm 
Newbie

Joined: Thu Jan 14, 2010 5:43 pm
Posts: 8
pb00067 wrote:
Which picture do you mean?


The one in the first post:

Image

(Hosted in another server)
Image


Top
 Profile  
 
 Post subject: Re: How to create an identified 1xN relation ?
PostPosted: Sun Jan 24, 2010 10:23 am 
Newbie

Joined: Thu Jan 14, 2010 5:43 pm
Posts: 8
CarlSagan wrote:
pb00067 wrote:
Which picture do you mean?


The one in the first post:

Image

(Hosted in another server)
Image

No idea for this one?


Top
 Profile  
 
 Post subject: Re: How to create an identified 1xN relation ?
PostPosted: Mon Jan 25, 2010 9:09 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I cannot see any attached image (tif, jpeg, gif or else)!
I checked my profile, but I think not id depends on my personal settings ...


Top
 Profile  
 
 Post subject: Re: How to create an identified 1xN relation ?
PostPosted: Mon Jan 25, 2010 10:15 am 
Newbie

Joined: Thu Jan 14, 2010 5:43 pm
Posts: 8
pb00067 wrote:
I cannot see any attached image (tif, jpeg, gif or else)!
I checked my profile, but I think not id depends on my personal settings ...

Wow.

Try flickr then: http://www.flickr.com/photos/41696532@N06/4303156891/


Top
 Profile  
 
 Post subject: Re: How to create an identified 1xN relation ?
PostPosted: Mon Jan 25, 2010 10:58 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hm, apparently overlapping @Id + @javax.persistence.ManyToOne on the same property is not supported.

Code:
@Id
@javax.persistence.ManyToOne
protected Book bookId = null;


I get javax.persistence.PersistenceException: [PersistenceUnit: helloworld] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:761)


Sorry, the solution with the IdClass does not work.

Anyway I personally prefer always the use of a surrogate generated key as primary key
and use NaturalId for any business keys (as bookid or edition) like I proposed.
Then i never face the problem you have.
And furthermore: if once I have to change the bookid value for a determinate book,
then I need not to update also other foreign keys.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.