我正在配置我的第一个官方EJB项目的项目设置。我已经在名为Model的单层EJB模块和另一个名为Controller的EJB模块中设置了所有POJO,从那里我试图使用模型模块上的JPA执行从数据库中的简单选择。 我没有在JNDI上使用数据源定义,我的所有连接参数都在persistence.xml中设置,当我在模型模块中本地尝试它时工作正常,换句话说,在模型模块上运行一个简单的主方法工作正常与所有POJO一起,但只要我通过无状态SessionBean调用相同的方法就给我以下内容
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/util/xml/Origin
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at com.opus.utils.Hibernate5Test.<init>(Hibernate5Test.java:30)
at com.opus.utils.Hibernate5Test.newInstance(Hibernate5Test.java:43)
at com.opus.ejb.beans.TestSessionBean.getRoleById(TestSessionBean.java:24)
at com.opus.classes.ControllerTest.testing(ControllerTest.java:20)
at com.opus.classes.ControllerTest.main(ControllerTest.java:26)
Caused by: java.lang.ClassNotFoundException: org.hibernate.util.xml.Origin
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 8 more
Java Result: 1
BUILD SUCCESSFUL (total time: 12 seconds)
我正在使用JBoss 6和MySQL数据库。以下是Stateless SessionBean的代码:
package com.myproject.ejb.beans;
import com.myproject.dao.DAOrole;
import com.myproject.jpa.pojos.Role;
import com.myproject.utils.Hibernate5Test;
import com.myproject.utils.PrintLog;
import javax.ejb.Stateful;
@Stateful
public class TestSessionBean implements TestSessionBeanLocal {
// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
public Object getRoleById(long id){
PrintLog log = new PrintLog("myproject-ejb");
Hibernate5Test hib5 = Hibernate5Test.newInstance();
if(hib5.entityMngr!=null){
// Test para roles y llaves complejas con Menu
DAOrole daoRole = new DAOrole(hib5.entityMngr,"testingHib5");
Role role = daoRole.selectById(id);
log.println("Hibernate5Test", "main", "testingHib5", role.showInfo());
return role;
}
return null;
}
}
以下是Hibernate Manager的代码:
package com.myproject.utils;
import com.myproject.dao.DAOrole;
import com.myproject.jpa.pojos.Role;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class Hibernate5Test {
public EntityManager entityMngr;
public EntityManagerFactory factory;
private static Hibernate5Test hib5 = null;
private Hibernate5Test(){
try{
factory = Persistence.createEntityManagerFactory("myproject-Model-PU");
entityMngr = factory.createEntityManager();
System.out.println("Connection Succesfull!");
}
catch(Exception e){
e.printStackTrace();
entityMngr = null;
System.out.println("Connection failed!");
}
}
public static Hibernate5Test newInstance(){
if(hib5 ==null)
return new Hibernate5Test();
else
return hib5;
}
public void close(){
try{
System.out.println("Closing Factory and EntityManager...");
entityMngr.close();
factory.close();
System.out.println("Closed.");
}
catch(Exception e){
e.printStackTrace();
}
}
}
以下是Controller Module上EJB客户端的代码:
package com.myproject.classes;
import com.myproject.ejb.beans.TestSessionBean;
import com.myproject.jpa.pojos.Role;
import javax.ejb.EJB;
public class ControllerTest {
@EJB
TestSessionBean test = new TestSessionBean();
public void testing(long roleId){
Role role = (Role)test.getRoleById(roleId);
role.showInfo();
}
public static void main(String[] args){
ControllerTest test = new ControllerTest();
test.testing(new Long(1));
}
}
任何帮助都会受到赞赏,也许我误解了EJB的工作方式。