• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

spring data jpa bidirectional mapping is not working

 
iurii sherstoi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.

Let's assume I have two entities Student and Book. One student could have many books.

Student.java


Book.java


I also have next repositories:


My services are looking like this:



And controller:


I insert few test records to DB, so when I put next url in browser, I see a lot of the same nested data:

What is wrong with my mapping?

Thanks in advance.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi iurii sherstoi,

First of all, a warm welcome to CodeRanch!

iurii sherstoi wrote:What is wrong with my mapping?

Let's start with the beginning: is the data in the database tables Student and Book correct? Meaning: do you have different records (with a different primary key) and are relationships between records valid (using correct foreign keys)?

Kind regards,
Roel
 
iurii sherstoi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

Thanks for replay and sorry for delay. Yes, table structure looks correct for me.
Student table:

STUDENT_ID STUDENT_NAME
1 Misha
2 Egor

I have next key for Student table:
 
iurii sherstoi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry have some issue with html table tag for my post (can't insert two html tables in one post).
So here is continue.

Book table:

BOOK_ID BOOK_NAME STUDENT_ID
1 Thinking in Java 1
2 Effective Java 1
3 Effective SQL 2


So I have next keys for Book table.

So as for me mapping looks fine but still don't understand why do I see nested data...
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's odd! Everything seems ok to me and your mapping looks almost similar to the example in the Java Persistence wikibook.

So you'll have some debugging to do If you are using at least JPA 2.0, I would try the unidirectional OneToMany without the inverse ManyToOne and verify if this gets you the expected/desired list of students.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic