• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

spring data jpa bidirectional mapping is not working

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic