Results 1 to 4 of 4
Thread: JPA many to many Need help
- 02-26-2014, 03:44 AM #1
Member
- Join Date
- Feb 2014
- Posts
- 2
- Rep Power
- 0
JPA many to many Need help
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.
Java Code:@Entity @Table(name = "MATERIAL_RECORD") @XmlRootElement @NamedQueries({ @NamedQuery(name = "MaterialRecord.findAll", query = "SELECT m FROM MaterialRecord m")}) public class MaterialRecord implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "ID") private Integer id; @Column(name = "SDS_NUMBER") private Integer sdsNumber; @Column(name = "PRODUCT_NAME") private String productName; @ManyToMany(mappedBy = "materialRecordList") private List<Keywords> keywordsList;
Java Code:@Entity @Table(name = "KEYWORDS") @XmlRootElement @NamedQueries({ @NamedQuery(name = "Keywords.findAll", query = "SELECT k FROM Keywords k")}) public class Keywords implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "KEY_ID") private Integer keyId; @Column(name = "KEYWORD_NAME") private String keywordName; @JoinTable(name = "KEYWORD_LOOKUP", joinColumns = { @JoinColumn(name = "KEY_ID", referencedColumnName = "KEY_ID")}, inverseJoinColumns = { @JoinColumn(name = "SDS_NUMBER", referencedColumnName = "SDS_NUMBER")}) @ManyToMany private List<MaterialRecord> materialRecordList;
Java Code:public class JpaTest { public static void main (String [] args){ MaterialRecord record = new MaterialRecord(); record.setProductName("oofbar"); Keywords keywd = new Keywords(); keywd.setKeywordName("testing"); Keywords keywd2 = new Keywords(); keywd2.setKeywordName("testing2"); record.getKeywordsList().add(keywd); record.getKeywordsList().add(keywd2); keywd.getMaterialRecordList().add(record); keywd2.getMaterialRecordList().add(record); EntityManagerFactory emf = Persistence.createEntityManagerFactory("JavaApplication26PU"); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); em.persist(record); em.persist(keywd); em.persist(keywd2); tx.commit();
- 02-26-2014, 09:33 AM #2
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: JPA many to many Need help
But that has nothing to do with JPA, you're simply not initializing the list. Of course you get an NPE then when you try to call add() on a null-reference.
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 02-26-2014, 01:55 PM #3
Member
- Join Date
- Feb 2014
- Posts
- 2
- Rep Power
- 0
Re: JPA many to many Need help
gimbal,
Thanks for your reply. I have been struggling trying to implement this many-to-many and thats why I posted here. I thought that when I call "Keywords keywd = new Keywords();
keywd.setKeywordName("testing");" this initialized the variable. Would you be so kind as to give me an example of how I would initialize the list in this situation? It would be greatly appreciated! Thanks in advance
- 02-26-2014, 02:09 PM #4
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: JPA many to many Need help
Okay, let me make it a little more "I guess this person has very little experience with Java and is biting off more than he can chew":
Java Code:private List<MaterialRecord> materialRecordList; // <--- this variable is null
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
Bookmarks