-->
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: A sense of style: @OneToMany or @ElementCollection?
PostPosted: Wed Oct 27, 2010 10:03 am 
Newbie

Joined: Wed Jun 06, 2007 9:14 am
Posts: 14
Hello, everybody!

In JPA 1.0 I could map the collection association to DocumentSubject (only accessible through the Document entity) like this:

Code:
@Entity
@Table(name = "SUBJECTS")
public class Subject
{
    @Id
    @Column(name = "SUBJECT_ID")
    private int id;
   
    private String description;

    // Getters and setters...
}

@Embeddable
public class DocumentSubjectId implements Serializable
{
    @Column(name = "DOCUMENT_ID")
    private int documentId;
   
    @Column(name = "ITEM_ID")
    private int item;

    // Getters and setters, equals and hashcode...
}

@Entity
@Table(name = "DOCUMENT_SUBJECTS")
public class DocumentSubject
{
    @EmbeddedId
    private DocumentSubjectId id;

    @ManyToOne
    @JoinColumn(name = "SUBJECT_ID")
    private Subject subject;

    @ManyToOne
    @JoinColumn(name = "DOCUMENT_ID", referencedColumnName = "DOCUMENT_ID", insertable = false, updatable = false)
    private Document document; // See below for the Document class...

    // Getters and setters...
}

@Entity
@Table(name = "DOCUMENTS")
public class Document
{
    @Id
    @Column(name = "DOCUMENT_ID")
    private int id;

    @OneToMany(mappedBy = "document", cascade = {CascadeType.ALL})
    private List<DocumentSubject> subjects = new ArrayList<DocumentSubject>();

    // Getters and setters...
}

In JPA 2.0 I can get rid of the DocumentSubjectId class, transform DocumentSubject in an @Embeddable and use the @ElementCollection annotation, like this:

Code:
@Embeddable
public class DocumentSubject
{
    @Column(name = "ITEM_ID")
    private short item;

    @ManyToOne
    @JoinColumn(name = "SUBJECT_ID")
    private Subject subject;

    // Getters and setters...
}

@Entity
@Table(name = "DOCUMENTS")
public class Document
{
    @Id
    @Column(name = "DOCUMENT_ID")
    private int id;

    @ElementCollection
    @CollectionTable(name = "DOCUMENT_SUBJECTS", joinColumns = @JoinColumn(name = "DOCUMENT_ID"))
    private List<DocumentSubject> subjects = new ArrayList<DocumentSubject>();

    // Getters and setters...
}


So, my question is: what is the recommended to use in this case: relationships to entity classes, like in the first case, or collection of embedded classes, like the second case? Please note that I will never access in my application the DocumentSubject separately from the Document class. It really belongs to the Document class. What would you use in JPA 2.0, and why?

Thank you in advance.

Marcos


Top
 Profile  
 
 Post subject: Re: A sense of style: @OneToMany or @ElementCollection?
PostPosted: Thu Oct 28, 2010 7:02 am 
Newbie

Joined: Wed Jun 06, 2007 9:14 am
Posts: 14
Please can someone give me some advice about this? This is important to me.

Thank you.

Marcos


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.