0

I am trying to insert into my table in MySQL database from a web application using entity manager.

This is the class I created for the table (URLLINK.java):

package com.htasia.panton9.Entities.MCRE;


    import javax.persistence.*;
    import javax.validation.constraints.NotNull;
    import java.io.Serializable;

    @Entity
    @Table(name = "urllink")
    @NamedQueries({
            @NamedQuery(name = "urllink.findAll", query = "SELECT n FROM UrlLink n")})
    public class UrlLink implements Serializable {

        private static final long serialVersionUID = 1L;
        @Id
        @NotNull
        @Column(name = "ULNODEID")
        private int ulnodeid;



        @Basic(optional = false)
        @NotNull
        @Column(name = "NODEID")
        private Long nodeid;



        @Basic(optional = false)
        @NotNull
        @Column(name = "URL")
        private String URL;

        public UrlLink() {
        }

        public UrlLink(Long nodeid) {
            this.nodeid = nodeid;
        }


        public Long getNodeid() {
            return nodeid;
        }

        public void setNodeid(Long nodeid) {
            this.nodeid = nodeid;
        }


        public String getURL() {
            return URL;
        }

        public void setURL(String URL) {
            this.URL = URL;
        }





    }

I created another function in NodeFacada.java:

@PersistenceContext(unitName ="PantonPU")
    private EntityManager em;  

    public  void insert_url(long nodeid,String url){



     try {
          UrlLink n=new UrlLink();
          n.setNodeid(nodeid);
          n.setURL(url);
          em.persist(n);


        }catch (Exception e) {
           e.printStackTrace();
        }

    }

And finally,I call this function in a JSP page so it will insert the values into the table.Problem is nothing gets inserted so I tried debugging it and it claims em is null.

halfer
  • 19,824
  • 17
  • 99
  • 186
Daredevil
  • 1,672
  • 3
  • 18
  • 47
  • Did you configure the Entity Bean in XML or using annotation. – Lova Chittumuri Mar 18 '19 at 06:54
  • Possible duplicate of [PersistenceContext EntityManager injection NullPointerException](https://stackoverflow.com/questions/4708035/persistencecontext-entitymanager-injection-nullpointerexception) – Rcordoval Mar 18 '19 at 06:54
  • I did not configure such in entity bean,code wise, is there any wrong? – Daredevil Mar 18 '19 at 06:55
  • are you sure you are initializing the Entity Manager? or doing the dependency injection / creating the bean? – Mufaddal Tahir Mar 18 '19 at 06:48
  • I am re-using the same Entity Manager and others table have no problem inserting but not this new table I created. – Daredevil Mar 18 '19 at 06:49
  • Possible duplicate of [Null EntityManager using @PersistenceContext](https://stackoverflow.com/questions/40461833/null-entitymanager-using-persistencecontext) – halfer May 02 '19 at 17:58

0 Answers0