Results 1 to 4 of 4
  1. #1
    notionDesign is offline Member
    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Default 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();

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,097
    Rep Power
    6

    Default 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

  3. #3
    notionDesign is offline Member
    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Default 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

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,097
    Rep Power
    6

    Default 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
    Now if you don't know how you can make sure that it is not null, my suggestion is to return to a study of the basics of Java and forget about JPA for now.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •