users@glassfish.java.net

Self referential ManyToMany relationship

From: Dibyendu Majumdar <dibyendu_at_mazumdar.demon.co.uk>
Date: Sat, 13 May 2006 01:03:17 +0100

Hi,

I am trying to implement a self referential many to many relationship.
Is following code correct?

@Entity
public class Book {

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    Long bookId;
   
    @Version
    Long version;
   
    @ManyToMany(mappedBy="linkedBooks")
    Collection<Book> linkedBooks;

    public Collection<Book> getLinkedBooks() {
        return linkedBooks;
    }

    public void setLinkedBooks(Collection<Book> linkedBooks) {
        this.linkedBooks = linkedBooks;
    }

    public Long getBookId() {
        return BookId;
    }

    public void setBookId(Long BookId) {
        this.BookId = BookId;
    }

    public Long getVersion() {
        return version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }
}