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: Noob Query
PostPosted: Fri Dec 24, 2010 1:52 pm 
Newbie

Joined: Tue Jun 29, 2010 11:50 am
Posts: 10
I am trying to setup a Many-To-Many relationship using annotations.
here are my entities
Code:
@Entity
public class Publisher{
        @Id
        public int id;
   public String name;
}
@Entity
public class PublisherBook{
        @Id
        public int id;
        @ManyToOne(cascade=CascadeType.ALL)
   @JoinColumn(name="bookId",nullable=false)
        public Book book;
        @ManyToOne(cascade=CascadeType.ALL)
   @JoinColumn(name="publisherId",nullable=false)
        public Publisher Publisher;
}

@Entity
public class Book{
        @Id
        public int id;
   public String name;
        @OneToMany(fetch=FetchType.EAGER, mappedBy="book")
   public Set<BookAuthor> authors;
}
@Entity
public class BookAuthor{
        @Id
        public int id;
        @ManyToOne(cascade=CascadeType.ALL)
   @JoinColumn(name="bookId",nullable=false)
        public Book book;
        @ManyToOne(cascade=CascadeType.ALL)
   @JoinColumn(name="authorId",nullable=false)
        public Author author;
}

@Entity
public class Authors{
        @Id
        public int id;
   public String name;
}


Here is the same data I have
Code:
Author:( id,name)
1, Author 1
2, Author 2
3, Author 3

Book: (id, name )
1, Book 1
2, Book 2
3, Book 3

Publisher(id,name)
1, Publisher 1
2, Publisher 2
3, Publisher 3

BookAuthor (id, bookId, authorId)
1, 1, 1
2, 1, 2
3, 1, 3
4, 2, 1
5, 3, 2

PublisherBook(id, publisherId, bookId)
1, 1, 1
2, 1, 2
3, 1, 3
4, 2, 1
5, 3, 2


If I try to get list of all books for Publisher 1 (id=1) from PublisherBook table, it should return total of 3 books.
But I am getting a total of 5 books. Book 1(id = 1) is being repeated 3 times with different author values (1, 2, 3)
So the list of PublisherBook returns
Code:
1, 1, 1
1, 1, 1
1, 1, 1
2, 1, 2
3, 1, 3


If I remove the Set<BookAuthor> from Book entity, the list of PublisherBook for publisher 1 returns the correct values
Code:
1, 1, 1
2, 1, 2
3, 1, 3


Can someone tell me what am I doing wrong?


Top
 Profile  
 
 Post subject: Re: Noob Query
PostPosted: Mon Dec 27, 2010 11:27 am 
Newbie

Joined: Tue Jun 29, 2010 11:50 am
Posts: 10
any ideas guys?


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.