与JPA中的实体合作

时间:2013-11-18 15:50:40

标签: java java-ee jpa persistence entities

我的申请表有问题。

以下是代码:

Student.java

package mainPack;
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;


@Entity
@Table(name = "student")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Student.findAll", query = "SELECT s FROM Student s"),
@NamedQuery(name = "Student.findById", query = "SELECT s FROM Student s WHERE s.id = :id"),
 @NamedQuery(name = "Student.findByNume", query = "SELECT s FROM Student s WHERE s.nume = :nume"),
 @NamedQuery(name = "Student.findByPrenume", query = "SELECT s FROM Student s WHERE s.prenume = :prenume"),
 @NamedQuery(name = "Student.findByVarsta", query = "SELECT s FROM Student s WHERE      s.varsta = :varsta")})

 public class Student implements Serializable {
 private static final long serialVersionUID = 1L;
 @Id
 @Basic(optional = false)
@NotNull
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 20)
@Column(name = "nume")
private String nume;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 20)
@Column(name = "prenume")
private String prenume;
@Basic(optional = false)
@NotNull
@Column(name = "varsta")
private int varsta;

public Student() {
}

public Student(Integer id) {
    this.id = id;
}

public Student(Integer id, String nume, String prenume, int varsta) {
    this.id = id;
    this.nume = nume;
    this.prenume = prenume;
    this.varsta = varsta;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getNume() {
    return nume;
}

public void setNume(String nume) {
    this.nume = nume;
}

public String getPrenume() {
    return prenume;
}

public void setPrenume(String prenume) {
    this.prenume = prenume;
}

public int getVarsta() {
    return varsta;
}

public void setVarsta(int varsta) {
    this.varsta = varsta;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.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.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "mainPack.Student[ id=" + id + " ]";
}

}

StudentBeanLocal.java

 package mainPack;
 import javax.ejb.Local;
 @Local
 public interface StudentBeanLocal {
 void addStudent(int id, String nume, String prenume, int varsta);
 void removeStudent(int id);
 void updateStudent(int id);
 String findStudent(int id);
 }

StudentBean.java

package mainPack;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class StudentBean implements StudentBeanLocal {
@PersistenceContext(unitName="Studentdemo-ejbPU")
private EntityManager em;

public void persist(Object object){
    em.persist(object);
}

@Override
public void addStudent(int id, String nume, String prenume, int varsta) {

    Student obj=new Student();

    obj.setId(id);
    obj.setNume(nume);
    obj.setPrenume(prenume);
    obj.setVarsta(varsta);

    persist(obj);

}

@Override
public void removeStudent(int id) {
    Student obj=em.find(Student.class, id);
    em.remove(obj);
    persist(obj);
}

@Override
public void updateStudent(int id) {
    Student obj=em.find(Student.class, id);
    obj.setNume("NumeModificat");
    persist(obj);
}

@Override
public String findStudent(int id) {
    Student obj=em.find(Student.class, id);
    return obj.getNume() ;
}


}

UseStudent.java

  package pack;

  import java.io.IOException;
  import java.io.PrintWriter;
  import javax.ejb.EJB;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import mainPack.StudentBeanLocal;
  import mainPack.StudentBeanLocal;


  public class UseStudent extends HttpServlet {
  @EJB
private StudentBeanLocal studentBean;


 protected void processRequest(HttpServletRequest request, HttpServletResponse  response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        studentBean.addStudent(2, "ion", "achim", 20);

        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet UseStudent</title>");            
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet UseStudent at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    } finally {            
        out.close();
    }
}


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}


@Override
public String getServletInfo() {
    return "Short description";
}
}

我只想在桌上学生中添加一名新学生 当我尝试运行UseStudent.java时,我得到了这个

  SEVERE:   Exception while loading the app
  SEVERE:   Undeployment failed for context /StudentDemo-war
  SEVERE:   Exception while loading the app : CDI deployment failure:Error loading class pack.UseStudent
 org.jboss.weld.resources.spi.ResourceLoadingException: Error loading class pack.UseStudent
at       org.jboss.weld.resources.ClassTransformer.getBackedAnnotatedType(ClassTransformer.java:179)
at org.jboss.weld.resources.ClassTransformer.getBackedAnnotatedType(ClassTransformer.java:188)
at org.jboss.weld.manager.BeanManagerImpl.createAnnotatedType(BeanManagerImpl.java:1136)
at org.glassfish.weld.WeldDeployer.firePITEvent(WeldDeployer.java:389)
at org.glassfish.weld.WeldDeployer.fireProcessInjectionTargetEvents(WeldDeployer.java:360)
at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:212)
at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:131)
at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:328)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:493)
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:527)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:523)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:356)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:522)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:546)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1423)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1500(CommandRunnerImpl.java:108)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1762)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1674)
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.StaticHttpHandler.service(StaticHttpHandler.java:297)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:246)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:724)
   Caused by: java.lang.NoClassDefFoundError: LmainPack/StudentBeanLocal;
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Class.java:2387)
at java.lang.Class.getDeclaredFields(Class.java:1796)
at org.jboss.weld.security.GetDeclaredFieldsAction.run(GetDeclaredFieldsAction.java:30)
at org.jboss.weld.security.GetDeclaredFieldsAction.run(GetDeclaredFieldsAction.java:22)
at java.security.AccessController.doPrivileged(Native Method)
at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$BackedAnnotatedFields.computeValue(BackedAnnotatedType.java:177)
at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$BackedAnnotatedFields.computeValue(BackedAnnotatedType.java:171)
at org.jboss.weld.util.LazyValueHolder.get(LazyValueHolder.java:35)
at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$EagerlyInitializedLazyValueHolder.<init>(BackedAnnotatedType.java:154)
at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$BackedAnnotatedFields.<init>(BackedAnnotatedType.java:171)
at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$BackedAnnotatedFields.<init>(BackedAnnotatedType.java:171)
at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType.<init>(BackedAnnotatedType.java:65)
at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType.of(BackedAnnotatedType.java:47)
at org.jboss.weld.resources.ClassTransformer$TransformClassToBackedAnnotatedType.apply(ClassTransformer.java:85)
at org.jboss.weld.resources.ClassTransformer$TransformClassToBackedAnnotatedType.apply(ClassTransformer.java:82)
at com.google.common.collect.ComputingConcurrentHashMap$ComputingValueReference.compute(ComputingConcurrentHashMap.java:358)
at com.google.common.collect.ComputingConcurrentHashMap$ComputingSegment.compute(ComputingConcurrentHashMap.java:184)
at com.google.common.collect.ComputingConcurrentHashMap$ComputingSegment.getOrCompute(ComputingConcurrentHashMap.java:153)
at com.google.common.collect.ComputingConcurrentHashMap.getOrCompute(ComputingConcurrentHashMap.java:69)
at com.google.common.collect.ComputingConcurrentHashMap$ComputingMapAdapter.get(ComputingConcurrentHashMap.java:396)
at org.jboss.weld.resources.ClassTransformer.getBackedAnnotatedType(ClassTransformer.java:174)
... 41 more
   Caused by: java.lang.ClassNotFoundException: mainPack.StudentBeanLocal
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1761)
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1611)
... 63 more

StudentDemo是我的应用名称。 有人可以解释一下我该怎么做,拜托!

0 个答案:

没有答案