我正在使用[netbeans + glassfish]开发一个项目,基于Java EE Web应用程序(JSP-Servlet,EJB,JPA),基于Djerby的java数据库,我运行它时无法部署的问题。
当我删除注释@PersistenceContext时,部署工作
,但需要把那个注释。
这里有一些例外:
In-place deployment at C:\Users\ahmed\Documents\NetBeansProjects\WebApplication6\build\web
GlassFish Server (1), deploy, null, false
C:\Users\ahmed\Documents\NetBeansProjects\WebApplication6\nbproject\build-impl.xml:1045: The module has not been deployed.
See the server log for details.
BUILD FAILED (total time: 1 second)
C:\Users\ahmed\Documents\NetBeansProjects\WebApplication6\build\web\WEB-INF\classes
Created dir: C:\Users\ahmed\Documents\NetBeansProjects\WebApplication6\build\web\META-INF
Copying 1 file to C:\Users\ahmed\Documents\NetBeansProjects\WebApplication6\build\web\META-INF
Created dir: C:\Users\ahmed\Documents\NetBeansProjects\WebApplication6\build\web\WEB-INF\classes\META-INF
Copying 1 file to C:\Users\ahmed\Documents\NetBeansProjects\WebApplication6\build\web\WEB-INF\classes\META-INF
Copying 3 files to C:\Users\ahmed\Documents\NetBeansProjects\WebApplication6\build\web
library-inclusion-in-archive:
library-inclusion-in-manifest:
Created dir: C:\Users\ahmed\Documents\NetBeansProjects\WebApplication6\build\empty
Created dir: C:\Users\ahmed\Documents\NetBeansProjects\WebApplication6\build\generated-sources\ap-source-output
Compiling 4 source files to C:\Users\ahmed\Documents\NetBeansProjects\WebApplication6\build\web\WEB-INF\classes
Note: C:\Users\ahmed\Documents\NetBeansProjects\WebApplication6\src\java\test\StudentDao.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
compile:
compile-jsps:
Created dir: C:\Users\ahmed\Documents\NetBeansProjects\WebApplication6\dist
Building jar: C:\Users\ahmed\Documents\NetBeansProjects\WebApplication6\dist\WebApplication6.war
do-dist:
dist:
BUILD SUCCESSFUL (total time: 0 seconds)
Avertissement: RAR5038:Unexpected exception while creating resource for pool { PoolInfo : (name=java:app/derby_net_Student_Pool), (applicationName=WebApplication6) }. Exception : javax.resource.spi.ResourceAllocationException: Connection could not be allocated because: La longueur de l'ID utilisateur (0) se situe hors de la plage comprise entre 1 et 255.
Avertissement: RAR5117 : Failed to obtain/create connection from connection pool [ { PoolInfo : (name=java:app/derby_net_Student_Pool), (applicationName=WebApplication6) } ]. Reason : com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: La longueur de l'ID utilisateur (0) se situe hors de la plage comprise entre 1 et 255.
Avertissement: RAR5114 : Error allocating connection : [Error in allocating a connection. Cause: Connection could not be allocated because: La longueur de l'ID utilisateur (0) se situe hors de la plage comprise entre 1 et 255.]
Grave: Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: La longueur de l'ID utilisateur (0) se situe hors de la plage comprise entre 1 et 255.
Error Code: 0
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:316)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:147)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.setOrDetectDatasource(DatabaseSessionImpl.java:207)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:750)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:265)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:731)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:205)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:305)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:337)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:303)
at org.glassfish.persistence.jpa.JPADeployer$2.visitPUD(JPADeployer.java:451)
at org.glassfish.persistence.jpa.JPADeployer$PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:510)
at org.glassfish.persistence.jpa.JPADeployer.iterateInitializedPUsAtApplicationPrepare(JPADeployer.java:492)
at org.glassfish.persistence.jpa.JPADeployer.event(JPADeployer.java:398)
at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:131)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:487)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:539)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:535)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:360)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:534)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:565)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:557)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:360)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:109)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1722)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: La longueur de l'ID utilisateur (0) se situe hors de la plage comprise entre 1 et 255.
at com.sun.gjc.spi.base.AbstractDataSource.getConnection(AbstractDataSource.java:121)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:135)
... 53 more
Caused by: javax.resource.spi.ResourceAllocationException: Error in allocating a connection. Cause: Connection could not be allocated because: La longueur de l'ID utilisateur (0) se situe hors de la plage comprise entre 1 et 255.
at com.sun.enterprise.connectors.ConnectionManagerImpl.internalGetConnection(ConnectionManagerImpl.java:319)
at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:196)
at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:171)
at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:166)
at com.sun.gjc.spi.base.AbstractDataSource.getConnection(AbstractDataSource.java:114)
... 54 more
Caused by: com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: La longueur de l'ID utilisateur (0) se situe hors de la plage comprise entre 1 et 255.
at com.sun.enterprise.resource.pool.datastructure.RWLockDataStructure.addResource(RWLockDataStructure.java:103)
at com.sun.enterprise.resource.pool.ConnectionPool.addResource(ConnectionPool.java:282)
at com.sun.enterprise.resource.pool.ConnectionPool.createResourceAndAddToPool(ConnectionPool.java:1512)
at com.sun.enterprise.resource.pool.ConnectionPool.createResources(ConnectionPool.java:944)
at com.sun.enterprise.resource.pool.ConnectionPool.initPool(ConnectionPool.java:230)
at com.sun.enterprise.resource.pool.ConnectionPool.internalGetResource(ConnectionPool.java:511)
at com.sun.enterprise.resource.pool.ConnectionPool.getResource(ConnectionPool.java:381)
at com.sun.enterprise.resource.pool.PoolManagerImpl.getResourceFromPool(PoolManagerImpl.java:245)
at com.sun.enterprise.resource.pool.PoolManagerImpl.getResource(PoolManagerImpl.java:170)
at com.sun.enterprise.connectors.ConnectionManagerImpl.getResource(ConnectionManagerImpl.java:360)
at com.sun.enterprise.connectors.ConnectionManagerImpl.internalGetConnection(ConnectionManagerImpl.java:307)
... 58 more
Caused by: com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: La longueur de l'ID utilisateur (0) se situe hors de la plage comprise entre 1 et 255.
at com.sun.enterprise.resource.pool.ConnectionPool.createSingleResource(ConnectionPool.java:924)
at com.sun.enterprise.resource.pool.ConnectionPool.createResource(ConnectionPool.java:1189)
at com.sun.enterprise.resource.pool.datastructure.RWLockDataStructure.addResource(RWLockDataStructure.java:98)
... 68 more
Caused by: com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: La longueur de l'ID utilisateur (0) se situe hors de la plage comprise entre 1 et 255.
at com.sun.enterprise.resource.allocator.LocalTxConnectorAllocator.createResource(LocalTxConnectorAllocator.java:110)
at com.sun.enterprise.resource.pool.ConnectionPool.createSingleResource(ConnectionPool.java:907)
... 70 more
Caused by: javax.resource.spi.ResourceAllocationException: Connection could not be allocated because: La longueur de l'ID utilisateur (0) se situe hors de la plage comprise entre 1 et 255.
at com.sun.gjc.spi.DSManagedConnectionFactory.createManagedConnection(DSManagedConnectionFactory.java:129)
at com.sun.enterprise.resource.allocator.LocalTxConnectorAllocator.createResource(LocalTxConnectorAllocator.java:87)
... 71 more
Caused by: java.sql.SQLNonTransientConnectionException: La longueur de l'ID utilisateur (0) se situe hors de la plage comprise entre 1 et 255.
at org.apache.derby.client.am.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.jdbc.BasicClientDataSource40.getConnection(Unknown Source)
at com.sun.gjc.spi.DSManagedConnectionFactory.createManagedConnection(DSManagedConnectionFactory.java:117)
... 72 more
Caused by: ERROR 08001: La longueur de l'ID utilisateur (0) se situe hors de la plage comprise entre 1 et 255.
at org.apache.derby.client.net.NetConnection.checkUserLength(Unknown Source)
at org.apache.derby.client.net.NetConnection.checkUser(Unknown Source)
at org.apache.derby.client.net.NetConnection.checkUserPassword(Unknown Source)
at org.apache.derby.client.net.NetConnection.flowConnect(Unknown Source)
at org.apache.derby.client.net.NetConnection.initialize(Unknown Source)
at org.apache.derby.client.net.NetConnection.<init>(Unknown Source)
at org.apache.derby.client.net.ClientJDBCObjectFactoryImpl.newNetConnection(Unknown Source)
at org.apache.derby.jdbc.BasicClientDataSource40.getConnectionX(Unknown Source)
... 74 more
Grave: Exception while deploying the app [WebApplication6]
Grave: Exception during lifecycle processing
org.glassfish.deployment.common.DeploymentException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: La longueur de l'ID utilisateur (0) se situe hors de la plage comprise entre 1 et 255.
Error Code: 0
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:815)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:205)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:305)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:337)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:303)
at org.glassfish.persistence.jpa.JPADeployer$2.visitPUD(JPADeployer.java:451)
at org.glassfish.persistence.jpa.JPADeployer$PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:510)
at org.glassfish.persistence.jpa.JPADeployer.iterateInitializedPUsAtApplicationPrepare(JPADeployer.java:492)
at org.glassfish.persistence.jpa.JPADeployer.event(JPADeployer.java:398)
at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:131)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:487)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:539)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:535)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:360)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:534)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:565)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:557)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:360)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:109)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1722)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)
Grave: Exception while deploying the app [WebApplication6] : Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: La longueur de l'ID utilisateur (0) se situe hors de la plage comprise entre 1 et 255.
Error Code: 0
Avertissement
package test;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author ahmed
*/
@Entity
@Table(name = "STUDENT")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Student.findAll", query = "SELECT s FROM Student s")
, @NamedQuery(name = "Student.findByStudentid", query = "SELECT s FROM Student s WHERE s.studentid = :studentid")
, @NamedQuery(name = "Student.findByName", query = "SELECT s FROM Student s WHERE s.name = :name")
, @NamedQuery(name = "Student.findBySecondname", query = "SELECT s FROM Student s WHERE s.secondname = :secondname")
, @NamedQuery(name = "Student.findByYearlevel", query = "SELECT s FROM Student s WHERE s.yearlevel = :yearlevel")})
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "STUDENTID")
private Integer studentid;
@Size(max = 255)
@Column(name = "NAME")
private String name;
@Size(max = 255)
@Column(name = "SECONDNAME")
private String secondname;
@Column(name = "YEARLEVEL")
private Integer yearlevel;
public Student() {
}
public Student(Integer studentid, String name, String secondname, Integer yearlevel) {
this.studentid = studentid;
this.name = name;
this.secondname = secondname;
this.yearlevel = yearlevel;
}
public Student(Integer studentid) {
this.studentid = studentid;
}
public Integer getStudentid() {
return studentid;
}
public void setStudentid(Integer studentid) {
this.studentid = studentid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSecondname() {
return secondname;
}
public void setSecondname(String secondname) {
this.secondname = secondname;
}
public Integer getYearlevel() {
return yearlevel;
}
public void setYearlevel(Integer yearlevel) {
this.yearlevel = yearlevel;
}
@Override
public int hashCode() {
int hash = 0;
hash += (studentid != null ? studentid.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Student)) {
return false;
}
Student other = (Student) object;
if ((this.studentid == null && other.studentid != null) || (this.studentid != null && !this.studentid.equals(other.studentid))) {
return false;
}
return true;
}
@Override
public String toString() {
return "test.Student[ studentid=" + studentid + " ]";
}
}
package test;
import javax.persistence.*;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
/**
*
* @author ahmed
*/
@Stateless
public class StudentDao implements StudentDaoLocal {
@PersistenceContext
private EntityManager em ;
public StudentDao(EntityManager em) {
this.em = em;
}
@Override
public void addStudent(Student student) {
em.persist(student);
}
// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
@Override
public void editStudent(Student student) {
em.merge(student);
}
@Override
public void removeStudent(int studentId) {
Student NewStudent=em.find(Student.class, studentId);
em.remove(NewStudent);
}
@Override
public Student getStudent(int studentId) {
Student NewStudent=em.find(Student.class, studentId);
return NewStudent;
}
@Override
public List<Student> getAll() {
return em.createNamedQuery("Student.findAll").getResultList();
}
}
package test;
import java.util.List;
import javax.ejb.Local;
/**
*
* @author ahmed
*/
@Local
public interface StudentDaoLocal {
void addStudent(Student student);
void editStudent(Student student);
void removeStudent(int studentId);
Student getStudent(int studentId);
List<Student> getAll();
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.io.IOException;
import java.io.PrintWriter;
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;
/**
*
* @author ahmed
*/
@WebServlet(name = "NewServlet", urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {
@EJB
private StudentDaoLocal studentDao;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String action = request.getParameter("action");
String studentStr = request.getParameter("studentId");
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String yearlevel = request.getParameter("yearlevel");
if(action!=null && studentStr !=null && firstname!=null && lastname!=null && yearlevel!=null ){
int id=Integer.parseInt(studentStr);
int year = Integer.parseInt(yearlevel);
request.setAttribute("year", year);
request.setAttribute("id", id);
request.setAttribute("n", firstname);
request.setAttribute("s", lastname);
Student s = new Student();
studentDao.addStudent(s);
//Student s=new Student(id,firstname,lastname,year);
}
request.getRequestDispatcher("student.jsp").forward(request, response);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
答案 0 :(得分:1)
首先,确保使用Payara作为应用程序服务器,而不是Glassfish,它是Glassfish的更新和修补版本。没有维护Glassfish,而像4.1这样的版本包含无法修复的错误 - 比如资源创建的错误也可能是你问题的根源。
其次,检查你的persistence.xml文件并确保你有正确的JTA persitence名称,与Glassfish / Payara服务器上的数据源JNDI名称相同:<jta-data-source>YOURDSNAMEHERE</jta-data-source>
。
第三,转到Glassfish / Payara管理并检查您是否正确设置了JDBC连接池和JDBC资源并命名了 - &gt; instructions
在继续并将其放入persistence.xml之前,先在应用服务器的管理控制台中测试JDBC资源,但最重要的是 - 确保切换到Payara而不是Glassfish,以避免浪费时间处理未修复的错误 - 它们基本上是相同的,除了Payara被修补并积极维护。