5 Replies Latest reply on May 17, 2014 8:51 PM by Stacy Williams

    I'm getting this error and I don't know why: Could not determine type for: javax.persistence.EntityManagerFactory, at table:Student, for columns: [org.hibernate.mapping.Column(em)]

    Stacy Williams Newbie

      Can someone help me with this? Here is the code it's coming from.

      import java.io.Serializable;

      import javax.persistence.Entity;

      import javax.persistence.EntityManagerFactory;

      import javax.persistence.GeneratedValue;

      import javax.persistence.GenerationType;

      import javax.persistence.Id;

      import javax.persistence.NamedQueries;

      import javax.persistence.OneToMany;

      import javax.persistence.PersistenceUnit;

       

      @Entity

       

      public class Student implements Serializable{

        private static final long serialVersionUID = 1L;

        @PersistenceUnit

        EntityManagerFactory em;

      Student user =(Student)em.createEntityManager().createQuery("select e FROM Student e").getResultList();

       

        @GeneratedValue(strategy=GenerationType.AUTO)

        @Id

        private int studentId;

       

        private String firstname;

       

        private String lastname;

       

        private int yearLevel;

      public int getStudentId() {

        return studentId;

        }

        public void setStudentId(int studentId) {

        this.studentId = studentId;

        }

        public String getFirstname() {

        return firstname;

        }

        public void setFirstname(String firstname) {

        this.firstname = firstname;

        }

        public String getLastname() {

        return lastname;

        }

        public void setLastname(String lastname) {

        this.lastname = lastname;

        }

        public int getYearLevel() {

        return yearLevel;

        }

        public void setYearLevel(int yearLevel) {

        this.yearLevel = yearLevel;

        }

        public Student(int studentId, String firstname, String lastname,

        int yearLevel) {

        super();

        this.studentId = studentId;

        this.firstname = firstname;

        this.lastname = lastname;

        this.yearLevel = yearLevel;

        }

        public Student(){}


      }

      Here is my session bean

       

      java.util.List;

      com.joseph.dao.view.StudentDaoLocal;

      com.joseph.model.Student;

      javax.ejb.Local;

      javax.ejb.LocalBean;

      javax.ejb.Stateless;

      javax.persistence.EntityManager;

      javax.persistence.PersistenceContext;

      (StudentDaoLocal.class)

      class StudentDao implements StudentDaoLocal {


      @PersistenceContext


      private EntityManager em;


      @Override

      public Student getStudent(int studentId){


      return em.find(Student.class,studentId);


      @Override 


      public void addStudent(Student student){

      em.persist(student);

      @Override

      public void editStudent(Student student){

      em.merge(student);


      @Override

      public void deleteStudent(int studentId){

      em.remove(getStudent(studentId));

      @SuppressWarnings("unchecked")

      @Override

      public List<Student>getAllStudents(){

      //return em.createNamedQuery("Student.getAll").getResultList();

       

      return  em.createNamedQuery("SELECT e FROM Student e").getResultList();


      public StudentDao() {





        • 1. Re: I'm getting this error and I don't know why: Could not determine type for: javax.persistence.EntityManagerFactory, at table:Student, for columns: [org.hibernate.mapping.Column(em)]
          Christian Sadilek Master

          Hi Stacy,

           

          Your JPA entities should not have a field of type EntityManagerFactory, since there's no matching column in your DB. What's the purpose of this field? You shouldn't need access to an entity manager from within an entity.

           

          Cheers,

          Christian

          • 2. Re: I'm getting this error and I don't know why: Could not determine type for: javax.persistence.EntityManagerFactory, at table:Student, for columns: [org.hibernate.mapping.Column(em)]
            Stacy Williams Newbie

            Thank you, now I'm getting another error in my session bean"Named query not found: SELECT s FROM Student.class s" here is the code: I know the method it's complaining about but I'm unsure what to do.

             

            @Stateless

            @Local(StudentDaoLocal.class)

            @LocalBean

            public class StudentDao implements StudentDaoLocal {

              @PersistenceContext

              private EntityManager em;

             

              @Override

                public Student getStudent(int studentId){

                return em.find(Student.class,studentId);

                }

             

                @Override 

              public void addStudent(Student student){

                em.persist(student);

             

              }

                @Override

                public void editStudent(Student student){

                em.merge(student);

              

                }

                @Override

                public void deleteStudent(int studentId){

                em.remove(getStudent(studentId));

              

                }

               

                @SuppressWarnings("unchecked")

              @Override

                public List<Student>getAllStudents(){

                //return em.createNamedQuery("Student.getAll").getResultList();    // This is the method it's complaining about

                //return em.createQuery("Student.getAllStudents").getResultList();

               return em.createNamedQuery("SELECT s FROM Student s").getResultList();

                }

               

                public StudentDao() {

                   

                }

             

             

            }

            • 4. Re: I'm getting this error and I don't know why: Could not determine type for: javax.persistence.EntityManagerFactory, at table:Student, for columns: [org.hibernate.mapping.Column(em)]
              Stacy Williams Newbie

              Okay, I'm having another problem now. It seem like the application is working but when I try to test it, like adding data or deleting data it doesn't do anything. I'm assumming this is either the servlet or JSP page, here is the code.

              package com.joseph.dao;

              import java.util.List;

              import com.joseph.dao.view.StudentDaoLocal;

              import com.joseph.model.Student;

              import javax.ejb.Local;

              import javax.ejb.LocalBean;

              import javax.ejb.Stateless;

              import javax.persistence.EntityManager;

              import javax.persistence.PersistenceContext;

              import javax.persistence.Query;

              /**

              * Session Bean implementation class StudentDao

              */

              @Stateless

              @Local(StudentDaoLocal.class)

              @LocalBean

              public class StudentDao implements StudentDaoLocal {

                @PersistenceContext

                private EntityManager em;

               

                @Override

                  public Student getStudent(int studentId){

                  return em.find(Student.class,studentId);

                  }

               

                  @Override 

                public void addStudent(Student student){

                  em.persist(student);

                 }

                  @Override

                  public void editStudent(Student student){

                  em.merge(student);

                }

                  @Override

                  public void deleteStudent(int studentId){

                  em.remove(getStudent(studentId));

                }

                @SuppressWarnings("unchecked")

                @Override

                  public List<Student>getAllStudents(){

                  Query query = em.createQuery("SELECT s FROM Student s");

                 return query.getResultList();

                  }

                public StudentDao() {

                 }

              }

               

              Servlet:

              package com.joseph.controller;

              import java.io.IOException;

              import javax.ejb.EJB;

              import javax.servlet.ServletException;

              import javax.servlet.annotation.WebServlet;

              import javax.servlet.http.HttpServlet;

              import javax.servlet.http.HttpServletRequest;

              import javax.servlet.http.HttpServletResponse;

               

               

              import com.joseph.dao.view.StudentDaoLocal;

              import com.joseph.model.Student;

               

              /**

              * Servlet implementation class StudentServlet

              */

              @WebServlet("/StudentServlet")

              public class StudentServlet extends HttpServlet {

                private static final long serialVersionUID = 1L;

                    

                 @EJB

                 private StudentDaoLocal studentDao;

                  public StudentServlet() {

                      super();

                      // TODO Auto-generated constructor stub

                  }

                /**

                * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

                */

                protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                String action = request.getParameter("action");

                String StudentIdStr = request.getParameter("studentId");

                int studentId=0;

                if(StudentIdStr !=null && !StudentIdStr.equals(""))

                studentId=Integer.parseInt(StudentIdStr);

                String firstname= request.getParameter("firstname");

                String lastname = request.getParameter("lastname");

                String yearLevelStr = request.getParameter("yearLevel");

                int yearLevel=0;

                if(yearLevelStr!=null && !yearLevelStr.equals(""))

                yearLevel=Integer.parseInt(yearLevelStr);

              Student student = new Student(studentId,firstname,lastname,yearLevel);

               

                if("Add".equalsIgnoreCase(action)){

                studentDao.addStudent(student);

               

                }else if("Edit".equalsIgnoreCase(action)){

                studentDao.editStudent(student);

               

                }else if("Delete".equalsIgnoreCase(action)){

                studentDao.deleteStudent(studentId);

               

                }else if("Search".equalsIgnoreCase(action)){

                student = studentDao.getStudent(studentId);

                }

                request.setAttribute("student", student);

                request.setAttribute("allStudent", studentDao.getAllStudents());

                request.getRequestDispatcher("studentinfo.jsp").forward(request, response);

                }

               

              /**

                * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

                */

                protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                // TODO Auto-generated method stub

                }

              }

              // JPA

              import java.io.Serializable;

              import javax.persistence.Entity;

              import javax.persistence.GeneratedValue;

              import javax.persistence.GenerationType;

              import javax.persistence.Id;

              import javax.persistence.Table;

               

              @Entity

              @Table(name = "student") 

              public class Student implements Serializable{

                private static final long serialVersionUID = 1L;

                 @GeneratedValue(strategy=GenerationType.AUTO)

                @Id

                private int studentId;

               

                private String firstname;

               

                private String lastname;

               

                private int yearLevel;

               

               

                public int getStudentId() {

                return studentId;

                }

                public void setStudentId(int studentId) {

                this.studentId = studentId;

                }

                public String getFirstname() {

                return firstname;

                }

                public void setFirstname(String firstname) {

                this.firstname = firstname;

                }

                public String getLastname() {

                return lastname;

                }

                public void setLastname(String lastname) {

                this.lastname = lastname;

                }

                public int getYearLevel() {

                return yearLevel;

                }

                public void setYearLevel(int yearLevel) {

                this.yearLevel = yearLevel;

                }

                public Student(int studentId, String firstname, String lastname,

                int yearLevel) {

                super();

                this.studentId = studentId;

                this.firstname = firstname;

                this.lastname = lastname;

                this.yearLevel = yearLevel;

                }

               

                public Student(){}

              }

               

              // JSP Page

              <%@ page language="java" contentType="text/html; charset=ISO-8859-1"

                  pageEncoding="ISO-8859-1"%>

                  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

                

                 

              <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

              <html>

              <head>

              <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

              <title>Student Information</title>

              </head>

               

              <body>

              <h1>Student Information</h1>

              <form action="/StudentServlet" method="POST"></form>

              <table>

              <tr>

                  <td>Student ID</td>

                  <td><input type="text" name="studentId" value="  "/></td>

                  </tr>

                  <tr>

                  <td>First Name</td>

                  <td><input type="text" name="firstname" value="  "/></td>

                  </tr>

                  <tr>

                  <td>Last Name</td>

                  <td><input type="text" name="lastname" value="      "/></td>

                  </tr>

                  <tr>

                     <td>Year Level</td>

                     <td><input type="text" name="yearLevel" value="    "/></td>

                     </tr>

                    

                    <tr>

                       <td colspan="2">

                          <input type="submit" name="action" value="Add"/>

                          <input type="submit" name="action" value="Edit"/>

                          <input type="submit" name="action" value="Delete"/>

                          <input type="submit" name="action" value="Search"/>

                          </td>

                          </tr>

              </table>

              <br>

              <table>

              <tr>

              <th>ID</th>

              <th>First Name</th>

              <th>Last Name</th>

              <th>Year Level</th>

              </tr>

              <c:forEach items="${allStudents}" var="stud">

                 <tr>

                 <td><c:out value= "${stud.studentId}"/></td>

                 <td><c:out value="${stud.firstname}"/></td>

                 <td><c:out value="${stud.lastname}"/></td>

                 <td><c:out value="${stud.yearLevel}"/></td>

                 </tr>

                 </c:forEach>

              </table>

              </body>

              </html>

              • 5. Re: I'm getting this error and I don't know why: Could not determine type for: javax.persistence.EntityManagerFactory, at table:Student, for columns: [org.hibernate.mapping.Column(em)]
                Stacy Williams Newbie

                Can someone look at my servlet, because I think that is the reason why I'm not getting any action when I click the add, search, edit and delete buttone.