线程"main"中的异常 javax.ejb.EJBException: java.lang.ClassNotFoundException: javax.persistence.Persistence



我遇到了这个问题,无法使用JNDI解决它 类ITCenterManagementPersistenceBeanRemote

public interface ITCenterManagementPersistenceBeanRemote {
public void insertStudent(long id, String name, String birthday, String address, String phonenumber, String email);
public void insertStudent(long id, String name, String birthday, String address, String phonenumber, String email);}

class ITCenterManagementPersistenceBean

@Stateless
public class ITCenterManagementPersistenceBean implements ITCenterManagementPersistenceBeanRemote {
@PersistenceContext(unitName = "QuanLiTTTinHocPU")
private EntityManager entityManager;
public ITCenterManagementPersistenceBean() {
}
@Override
public void insertStudent(long id, String name, String birthday, String address, String phonenumber, String email) {
Student s = new Student(id, name, birthday, address, phonenumber, email);
entityManager.persist(s);
}
@Override
public List<Student> getStudent() {
return entityManager.createNamedQuery("Student.findAll").getResultList();
}}

测试类

public class ITCenterManagementTester {
private Properties props;
private InitialContext ctx;
public ITCenterManagementTester() {
readJNDI();
}
private void readJNDI() {
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException e) {
System.err.println(e.toString());
}
try {
ctx = new InitialContext(props);
//ctx.close();
} catch (NamingException ex) {
System.err.println(ex.toString());
}
}
private String getJNDI() {
String appName = "";
String moduleName = "QuanLiTTTinHoc";
String distinctName = "";
String sessionBeanName = ITCenterManagementPersistenceBean.class.getSimpleName();
String viewClassName = ITCenterManagementPersistenceBeanRemote.class.getName();
return "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + sessionBeanName + "!" + viewClassName;
}
private void showGUI() {
System.out.println("n===============================");
System.out.println("Welcome to IT center Management");
System.out.println("===============================");
System.out.print("Options: n1. student n2. List All student n3. Teacher n4. List all Tacher"
+ "n5. Staff n6. List all Staff n7. Add class n8. List of classn9. Add Subject"
+ "n10. Add Class Shift n11. Class Shift list n12. Add Prerequisite Subject n13. Add Schedule n14. Add Exam Schedule"
+ "n15. Add Result n16. Addtendance n17. Teaching Schedule n18. Add Register Information"
+ "n19. List of Register Informationn20. Exit nEnter Choice: ");
}
/*private void getAllBooks() {
try {
// We can define another bean to access the LibrarySessionBeanRemote
ITCenterManagementPersistenceBeanRemote CenterBean2 = (ITCenterManagementPersistenceBeanRemote) ctx.lookup(getJNDI());
//List<String> studentList = CenterBean2.getStudent();
// Print all books
System.out.println("n===============================");
System.out.println("Listing Student");
System.out.println("===============================");
//for (int i = 0; i < studentList.size(); i++) {
//    System.out.println(i + "t" + studentList.get(i));
//}
System.out.println("");
} catch (NamingException ex) {
System.err.println(ex.toString());
}
}*/
public void testStatelessEJB() {
try {
// Scanner definition
Scanner sc = new Scanner(System.in);
// ITCenterManagementPersistenceBeanRemote
ITCenterManagementPersistenceBeanRemote session = (ITCenterManagementPersistenceBeanRemote) ctx.lookup(getJNDI());
int choice = 0;
long studentID;
String name;
String birthday;
String address;
String phonenumber;
String email;
Subject subjectName;
long classID;
String className;
Date beginDay;
Date endDay;
BigInteger stdNumber;
BigInteger fee;
long shiftID;
String shiftName;
String beginTime;
String endTime;
double point;
long PSubjectID;
boolean absent;
Date absentDate;
Staff staffID;
long registerID;
Date registerDate;
ClassShift ShiftID;
Date testDate;
String time;
BigInteger hour;
String room;
do {
showGUI();
choice = Integer.parseInt(sc.nextLine());
switch (choice) {
case 1:
System.out.print("insert Student ID: ");
studentID = Long.parseLong(sc.nextLine()) ;
System.out.print("insert Student name: ");
name = sc.nextLine();
System.out.print("insert Student birthday: ");
birthday = sc.nextLine();
System.out.print("insert Student address: ");
address = sc.nextLine();
System.out.print("insert Student phone number: ");
phonenumber = sc.nextLine();
System.out.print("insert Student email: ");
email = sc.nextLine();
session.insertStudent(studentID, name, birthday, address, phonenumber, email);
System.out.println("Done!");
break;
case 2:
List<Student> s = session.getStudent();
if (s.isEmpty()) {
System.out.println("There is no student in list!");
}
System.out.println("n=========================");
System.out.println("Listing Student");
System.out.println("=========================");
for(int i = 0; i<s.size();i++){
System.out.println((i+1)+"t"+s.get(i));
}
System.out.println();
break;
default:
break;
}
} while (choice != 50);
} catch (NamingException ex) {
System.err.println(ex.toString());
}
}

}

**

这是班上的学生

**

package edu.tdt.persistence;
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
/**
*
* @author MSI
*/
@Entity
@Table(name = "Student", catalog = "ITCenterManagement", schema = "public")
@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.findByBirthDay", query = "SELECT s FROM Student s WHERE s.birthDay = :birthDay")
, @NamedQuery(name = "Student.findByAddress", query = "SELECT s FROM Student s WHERE s.address = :address")
, @NamedQuery(name = "Student.findByPhoneNumber", query = "SELECT s FROM Student s WHERE s.phoneNumber = :phoneNumber")
, @NamedQuery(name = "Student.findByEmail", query = "SELECT s FROM Student s WHERE s.email = :email")
, @NamedQuery(name = "Student.findByA", query = "SELECT s FROM Student s WHERE s.a = :a")})
public class Student implements Serializable {
@Size(max = 2147483647)
@Column(name = "Birthday", length = 2147483647)
private String birthday;
@Size(max = 2147483647)
@Column(name = "PhoneNumber", length = 2147483647)
private String phoneNumber;
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "StudentID", nullable = false)
private Long studentID;
@Size(max = 2147483647)
@Column(name = "Name", length = 2147483647)
private String name;
@Size(max = 2147483647)
@Column(name = "BirthDay", length = 2147483647)
private String birthDay;
@Size(max = 2147483647)
@Column(name = "Address", length = 2147483647)
private String address;
// @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
@Size(max = 2147483647)
@Column(name = "Email", length = 2147483647)
private String email;
@Size(max = 2147483647)
@Column(name = "", length = 2147483647)
private String a;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "student")
private Collection<Attendance> attendanceCollection;
@OneToMany(mappedBy = "studentID")
private Collection<RegisterInformation> registerInformationCollection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "student")
private Collection<TestResult> testResultCollection;
public Student() {
}
public Student(long studentID) {
this.studentID = studentID;
}
public Student(long studentID, String name, String birthday, String address, String phonenumber, String email) {
this.studentID = studentID;
this.name = name;
this.birthDay = birthday;
this.address = address;
this.phoneNumber = phonenumber;
this.email = email;
}
public Long getStudentID() {
return studentID;
}
public void setStudentID(Long studentID) {
this.studentID = studentID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirthDay() {
return birthDay;
}
public void setBirthDay(String birthDay) {
this.birthDay = birthDay;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
@XmlTransient
public Collection<Attendance> getAttendanceCollection() {
return attendanceCollection;
}
public void setAttendanceCollection(Collection<Attendance> attendanceCollection) {
this.attendanceCollection = attendanceCollection;
}
@XmlTransient
public Collection<RegisterInformation> getRegisterInformationCollection() {
return registerInformationCollection;
}
public void setRegisterInformationCollection(Collection<RegisterInformation> registerInformationCollection) {
this.registerInformationCollection = registerInformationCollection;
}
@XmlTransient
public Collection<TestResult> getTestResultCollection() {
return testResultCollection;
}
public void setTestResultCollection(Collection<TestResult> testResultCollection) {
this.testResultCollection = testResultCollection;
}
@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 "edu.tdt.persistence.Student[ studentID=" + studentID + " ]";
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}

public class QuanLiTTTinHocTester {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws EJBException, IllegalStateException{
// TODO code application logic here
ITCenterManagementTester test = new ITCenterManagementTester();
test.testStatelessEJB();
}
}

当我运行main并收到这个错误时

Exception in thread "main" javax.ejb.EJBException: java.lang.ClassNotFoundException: javax.persistence.PersistenceException
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:238)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:183)
at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:146)
at com.sun.proxy.$Proxy2.getStudent(Unknown Source)
at edu.tdt.test.ITCenterManagementTester.testStatelessEJB(ITCenterManagementTester.java:153)
at edu.tdt.test.QuanLiTTTinHocTester.main(QuanLiTTTinHocTester.java:20)
Caused by: java.lang.ClassNotFoundException: javax.persistence.PersistenceException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.jboss.marshalling.AbstractClassResolver.loadClass(AbstractClassResolver.java:131)
at org.jboss.marshalling.AbstractClassResolver.resolveClass(AbstractClassResolver.java:112)
at org.jboss.marshalling.river.RiverUnmarshaller.doReadClassDescriptor(RiverUnmarshaller.java:948)
at org.jboss.marshalling.river.RiverUnmarshaller.doReadNewObject(RiverUnmarshaller.java:1255)
at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:276)
at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:224)
at org.jboss.marshalling.river.RiverUnmarshaller.readFields(RiverUnmarshaller.java:1745)
at org.jboss.marshalling.river.RiverObjectInputStream.defaultReadObject(RiverObjectInputStream.java:81)
at java.lang.Throwable.readObject(Throwable.java:914)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jboss.marshalling.reflect.SerializableClass.callReadObject(SerializableClass.java:307)
at org.jboss.marshalling.river.RiverUnmarshaller.doInitSerializable(RiverUnmarshaller.java:1637)
at org.jboss.marshalling.river.RiverUnmarshaller.doInitSerializable(RiverUnmarshaller.java:1606)
at org.jboss.marshalling.river.RiverUnmarshaller.doInitSerializable(RiverUnmarshaller.java:1606)
at org.jboss.marshalling.river.RiverUnmarshaller.doInitSerializable(RiverUnmarshaller.java:1606)
at org.jboss.marshalling.river.RiverUnmarshaller.doReadNewObject(RiverUnmarshaller.java:1285)
at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:276)
at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:209)
at org.jboss.marshalling.AbstractObjectInput.readObject(AbstractObjectInput.java:41)
at org.jboss.ejb.client.remoting.InvocationExceptionResponseHandler$MethodInvocationExceptionResultProducer.getResult(InvocationExceptionResponseHandler.java:79)
at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:276)
at org.jboss.ejb.client.EJBObjectInterceptor.handleInvocationResult(EJBObjectInterceptor.java:64)
at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:290)
at org.jboss.ejb.client.EJBHomeInterceptor.handleInvocationResult(EJBHomeInterceptor.java:88)
at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:290)
at org.jboss.ejb.client.TransactionInterceptor.handleInvocationResult(TransactionInterceptor.java:46)
at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:290)
at org.jboss.ejb.client.ReceiverInterceptor.handleInvocationResult(ReceiverInterceptor.java:142)
at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:265)
at org.jboss.ejb.client.EJBClientInvocationContext.awaitResponse(EJBClientInvocationContext.java:453)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:204)
... 5 more
C:UsersMSIAppDataLocalNetBeansCache8.2executor-snippetsrun.xml:53: Java returned: 1
BUILD FAILED (total time: 4 seconds)

*我实现了学生类形式postgresql,其他也实现了,但我只是先以学生类为例。当我运行测试项目时,它显示了错误,但我不知道如何解决。

我不知道为什么,所以请帮忙!

*

您的客户端需要在其类路径上拥有JPAAPI类。

您对EJB的调用失败,javax.persistence.PersistenceException正在传播到客户端。

然后客户端失败,因为它在类路径中找不到该类。

相关内容

最新更新