我的 Webservlet 有问题:当我尝试使用EntityManager
方法中的init(ServletConfig config)
在NetBeans中初始化我的数据库时,glassfish说:
Information: /file:/C:/Users/Alina/Documents/Uni/Semester 4/SE Projekt/Topfschlag/Rezept/target/Rezept-1.0-SNAPSHOT/WEB-INF/classes/_default logout successful
Information: visiting unvisited references
Information: visiting unvisited references
Information: visiting unvisited references
Information: visiting unvisited references
Information: EclipseLink, version: Eclipse Persistence Services - 2.6.1.v20150605-31e8258
Information: /file:/C:/Users/Alina/Documents/Uni/Semester 4/SE Projekt/Topfschlag/Rezept/target/Rezept-1.0-SNAPSHOT/WEB-INF/classes/_default login successful
Information: Portable JNDI names for EJB AnlassBean: [java:global/Rezept/AnlassBean!rezept.ejb.AnlassBean, java:global/Rezept/AnlassBean]
Information: Portable JNDI names for EJB RezeptBean: [java:global/Rezept/RezeptBean!rezept.ejb.RezeptBean, java:global/Rezept/RezeptBean]
Information: Portable JNDI names for EJB AllergieBean: [java:global/Rezept/AllergieBean, java:global/Rezept/AllergieBean!rezept.ejb.AllergieBean]
Information: Portable JNDI names for EJB GrundzutatBean: [java:global/Rezept/GrundzutatBean!rezept.ejb.GrundzutatBean, java:global/Rezept/GrundzutatBean]
WARN: WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN: WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN: WELD-000411: Observer method [BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
Information: Loading application [Rezept] at [/Rezept]
Information: Rezept was successfully deployed in 657 milliseconds.
Information: keepstate options resolved to true, saving appId 99721694532403200 for application Rezept.
Information: /file:/C:/Users/Alina/Documents/Uni/Semester 4/SE Projekt/Topfschlag/Rezept/target/Rezept-1.0-SNAPSHOT/WEB-INF/classes/_default logout successful
Information: visiting unvisited references
Information: visiting unvisited references
Information: visiting unvisited references
Information: visiting unvisited references
Information: EclipseLink, version: Eclipse Persistence Services - 2.6.1.v20150605-31e8258
Information: /file:/C:/Users/Alina/Documents/Uni/Semester 4/SE Projekt/Topfschlag/Rezept/target/Rezept-1.0-SNAPSHOT/WEB-INF/classes/_default login successful
Information: Portable JNDI names for EJB RezeptBean: [java:global/Rezept/RezeptBean!rezept.ejb.RezeptBean, java:global/Rezept/RezeptBean]
Information: Portable JNDI names for EJB AllergieBean: [java:global/Rezept/AllergieBean, java:global/Rezept/AllergieBean!rezept.ejb.AllergieBean]
Information: Portable JNDI names for EJB GrundzutatBean: [java:global/Rezept/GrundzutatBean!rezept.ejb.GrundzutatBean, java:global/Rezept/GrundzutatBean]
Information: Portable JNDI names for EJB AnlassBean: [java:global/Rezept/AnlassBean!rezept.ejb.AnlassBean, java:global/Rezept/AnlassBean]
WARN: WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN: WELD-000411: Observer method [BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN: WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
Information: Loading application [Rezept] at [/Rezept]
Information: Rezept was successfully deployed in 1.050 milliseconds.
Information: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Schwerwiegend: WebModule[/Rezept]StandardWrapper.Throwable
javax.persistence.TransactionRequiredException
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTxRequiredCheck(EntityManagerWrapper.java:161)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTransactionScopedTxCheck(EntityManagerWrapper.java:151)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:281)
at rezept.web.StartServlet.init(StartServlet.java:50)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1583)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:1212)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:237)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
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)
Warnung: StandardWrapperValve[rezept.web.StartServlet]: Allocate exception for servlet rezept.web.StartServlet
javax.persistence.TransactionRequiredException
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTxRequiredCheck(EntityManagerWrapper.java:161)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTransactionScopedTxCheck(EntityManagerWrapper.java:151)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:281)
at rezept.web.StartServlet.init(StartServlet.java:50)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1583)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:1212)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:237)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
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)
我不知道如何避免这个问题因为我需要在我的WebServlet启动之前填充我的数据库..所以这是我的代码到目前为止:
package rezept.web;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
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 rezept.ejb.RezeptBean;
import rezept.jpa.Rezept;
import java.io.*;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.*;
import javax.servlet.http.*;
import rezept.jpa.Allergie;
import rezept.jpa.Anlass;
import rezept.jpa.Grundzutat;
@WebServlet(urlPatterns = {"/index.html"})
public class StartServlet extends HttpServlet {
// The WebServlet is about a recipe-side which shows you after you clicked on some filters which kind of recipes would fit to your prefernces
// I need to fill the database first to make some checkboxes...
@EJB
private RezeptBean rezeptBean;
@PersistenceContext
EntityManager em;
public void init(ServletConfig config) throws ServletException {
super.init(config);
// The Syso is to test if the method works...
System.out.println("Test string");
//Fills the "Anlass"-table which means the time you want to eat.. like breakfast, dinner etc..
List<Anlass> anlässe = em.createQuery("SELECT a FROM Anlass a ").getResultList();
if (anlässe.isEmpty()) {
Anlass anlass;
anlass = new Anlass("Frühstück");
em.persist(anlass);
anlass = new Anlass("Brunch");
em.persist(anlass);
anlass = new Anlass("Mittagessen");
em.persist(anlass);
anlass = new Anlass("Abendessen");
em.persist(anlass);
}
//This is to fill and check the ingredients-table
List<Grundzutat> zutaten = em.createQuery("SELECT z FROM Grundzutat z ").getResultList();
if (zutaten.isEmpty()) {
Grundzutat zutat;
zutat = new Grundzutat("Eier");
em.persist(zutat);
zutat = new Grundzutat("Kartoffeln");
em.persist(zutat);
zutat = new Grundzutat("Tomaten");
em.persist(zutat);
zutat = new Grundzutat("Mehl");
em.persist(zutat);
zutat = new Grundzutat("Nudeln");
em.persist(zutat);
}
//to check and fill an allergies-table
List<Allergie> allergien = em.createQuery("SELECT a FROM Allergie a ").getResultList();
if (allergien.isEmpty()) {
Allergie allergie;
allergie = new Allergie("Laktose");
em.persist(allergie);
allergie = new Allergie("Weizen");
em.persist(allergie);
allergie = new Allergie("Gluten");
em.persist(allergie);
}
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
//Zuerst Fall-Unterscheidung ob der "Rezepte filtern" oder "Rezepte suchen"-Button gedrückt wurde oder keines von beidem
request.setCharacterEncoding("utf-8");
String action = request.getParameter("action");
if (action == null || action.equals("")) {
action="";
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/startseite.jsp");
dispatcher.forward(request, response);
}
有谁知道如何解决这个问题?也许我可以填写我的数据库,但我不知道如何...