• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA many to many relationship

 
Chris Merani
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been reading and watching tons of tutorials and just can't seem to get the many to many relationship to work. I have built a new project to simplify the project down to one many to many relationship. Please take a look at the code below and lend some suggestions as to why this fails. Currently I get a null pointer at the *keywd.getMaterialRecordList().add(record); line. If I comment this out then I get the same null pointer on the next .get....add();. If it matters I have abandoned Derby and moved to an H2 database.


--------------------------------------------------------

------------------------------------------------------------------------------
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

I've gone ahead and added the code tags for you. See how much easier the code is to read?
 
Chris Merani
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear,
Thank you for adding the tags and the welcome. I did add a couple more tags. I used a formatting that works on another forum, sorry for not reading before I posted. I have lurked around the ranch for a while and usually find the solution to my problem but this many to many I am stumped. People on this forum seem to be much nicer and understanding of the difficulties to programmers new to Java. Thanks All!
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to supply a default (parameter-less) constructor to initialize the fields. JPA will populate the fields only when actually loading the object from the database. When creating a new instance using the new keyword, JPA will not initialize your fields.
 
Amritendu De
author
Ranch Hand
Posts: 188
11
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Watch out for my new book which will clear all your doubts. The probable release date is April-May this year.

You can see the index here: http://www.coderanch.com/t/622592/books/book-Java-Spring-Hibernate-Data
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic