JBoss 7.1 和 Hibernate 4.3.5 导致异常
JBoss 7.1 and Hibernte 4.3.5 cause exception
我使用 Hibernate 4.3.5 和 JBoss 7.1.1 创建了一个项目。
我把我项目的组成截图给大家:
WEB-INF/lib
上的罐子是:
下面是需要的类:
EntityDao.java
public class EntityDao implements EntityDaoInterface<Book, Integer>
{
private Session currentSession;
private Transaction currentTransaction;
public EntityDao() {
}
public Session openCurrentSession() {
currentSession = getSessionFactory().openSession();
return currentSession;
}
public Session openCurrentSessionwithTransaction() {
currentSession = getSessionFactory().openSession();
currentTransaction = currentSession.beginTransaction();
return currentSession;
}
public void closeCurrentSession() {
currentSession.close();
}
public void closeCurrentSessionwithTransaction() {
currentTransaction.commit();
currentSession.close();
}
private static SessionFactory getSessionFactory() {
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
return sessionFactory;
}
public Session getCurrentSession() {
return currentSession;
}
public void setCurrentSession(Session currentSession) {
this.currentSession = currentSession;
}
public Transaction getCurrentTransaction() {
return currentTransaction;
}
public void setCurrentTransaction(Transaction currentTransaction) {
this.currentTransaction = currentTransaction;
}
public void persist(Person entity) {
getCurrentSession().save(entity);
}
public void update(Person entity) {
getCurrentSession().update(entity);
}
public void delete(Person entity) {
getCurrentSession().delete(entity);
}
@SuppressWarnings("unchecked")
public List<Person> findAllPersons() {
List<Person> persons = (List<Person>) getCurrentSession().createQuery("from Person").list();
return persons;
}
public Person findPersonById(Integer id) {
Person person = (Person) getCurrentSession().get(Person.class, id);
return person;
}
}
EntityDaoInterface.java
public interface EntityDaoInterface<T, Id extends Serializable>
{
public void persist(T entity);
public void update(T entity);
public T findById(Id id);
public void delete(T entity);
public List<T> findAll();
public void deleteAll();
}
EntityService.java
public class EntityService
{
private static EntityDao entityDao;
public EntityService() {
entityDao = new EntityDao();
}
public void persist(Person entity) {
entityDao.openCurrentSessionwithTransaction();
entityDao.persist(entity);
entityDao.closeCurrentSessionwithTransaction();
}
public void update(Person entity) {
entityDao.openCurrentSessionwithTransaction();
entityDao.update(entity);
entityDao.closeCurrentSessionwithTransaction();
}
public void remove(Person entity) {
entityDao.openCurrentSessionwithTransaction();
entityDao.delete(entity);
entityDao.closeCurrentSessionwithTransaction();
}
public Person findPersonById(int id) {
entityDao.openCurrentSession();
Person person = entityDao.findPersonById(id);
entityDao.closeCurrentSession();
return person;
}
public List<Person> findAllPersons() {
entityDao.openCurrentSession();
List<Person> persons = entityDao.findAllPersons();
entityDao.closeCurrentSession();
return persons;
}
public void deleteAll() {
entityDao.openCurrentSessionwithTransaction();
entityDao.deleteAll();
entityDao.closeCurrentSessionwithTransaction();
}
public EntityDao entityDao() {
return entityDao;
}
}
Person.java
@Entity
@Table(name = "person")
public class Person
{
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private int id;
@Column(name = "age")
private int age;
@Column(name= "name")
String name;
public Person() {}
public Person(int id, int age, String name) {
super();
this.id = id;
this.age = age;
this.name = name;
}
// With the methods: set, get, toString, hashCode, equals
}
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;database=ccc</property>
<property name="hibernate.connection.username">lm</property>
<property name="hibernate.connection.password">pp</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="com.esprit.entity.Book"/>
<mapping class="com.esprit.entity.Person"/>
</session-factory>
</hibernate-configuration>
DataModelBeanD.java
@SuppressWarnings("serial")
@ManagedBean
@ViewScoped
public class DataModelBeanD implements Serializable
{
private List<Person> list;
private Person person = new Person();
private boolean edit;
private EntityService entityService;
@PostConstruct
public void init()
{
entityService = new EntityService();
list = entityService.findAllPersons();
}
}
项目运行后,出现如下错误:
08:32:54,297 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "HelloJPAHibernate.war"
08:33:01,282 INFO [org.hibernate.annotations.common.Version] (http-localhost-127.0.0.1-8080-1) HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
08:33:01,290 INFO [org.hibernate.Version] (http-localhost-127.0.0.1-8080-1) HHH000412: Hibernate Core {4.3.5.Final}
08:33:01,293 INFO [org.hibernate.cfg.Environment] (http-localhost-127.0.0.1-8080-1) HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.max_fetch_depth=5, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE, hibernate.bytecode.use_reflection_optimizer=false, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=5}
08:33:01,297 INFO [org.hibernate.cfg.Environment] (http-localhost-127.0.0.1-8080-1) HHH000021: Bytecode provider name : javassist
08:33:01,317 INFO [org.hibernate.cfg.Configuration] (http-localhost-127.0.0.1-8080-1) HHH000043: Configuring from resource: /hibernate.cfg.xml
08:33:01,318 INFO [org.hibernate.cfg.Configuration] (http-localhost-127.0.0.1-8080-1) HHH000040: Configuration resource: /hibernate.cfg.xml
08:33:01,366 INFO [org.hibernate.cfg.Configuration] (http-localhost-127.0.0.1-8080-1) HHH000041: Configured SessionFactory: null
08:33:01,453 WARN [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-localhost-127.0.0.1-8080-1) HHH000402: Using Hibernate built-in connection pool (not for production use!)
08:33:01,455 INFO [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-localhost-127.0.0.1-8080-1) HHH000401: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://localhost:1433;database=ccc]
08:33:01,457 INFO [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-localhost-127.0.0.1-8080-1) HHH000046: Connection properties: {user=lm, password=****}
08:33:01,458 INFO [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-localhost-127.0.0.1-8080-1) HHH000006: Autocommit mode: false
08:33:01,460 INFO [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-localhost-127.0.0.1-8080-1) HHH000115: Hibernate connection pool size: 5 (min=1)
08:33:03,123 INFO [org.hibernate.dialect.Dialect] (http-localhost-127.0.0.1-8080-1) HHH000400: Using dialect: org.hibernate.dialect.SQLServer2008Dialect
08:33:03,255 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/HelloJPAHibernate].[Faces Servlet]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet Faces Servlet threw exception: java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;
at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:936) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:824) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at com.esprit.dao.EntityDao.getSessionFactory(EntityDao.java:48) [classes:]
at com.esprit.dao.EntityDao.openCurrentSession(EntityDao.java:25) [classes:]
at com.esprit.dao.EntityService.findAllPersons(EntityService.java:86) [classes:]
at com.esprit.crud.dynamic.DataModelBeanD.init(DataModelBeanD.java:37) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_55]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_55]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_55]
at java.lang.reflect.Method.invoke(Unknown Source) [rt.jar:1.7.0_55]
我不知道遗漏了什么,请你帮我解决这个问题。任何命题都是提前appreciated.Thanks
我通过制作 Hibernate 4.2.21 而不是 Hibernate 4.3.5 解决了我的问题。然后我重新创建 class EntityDao.java
的方法 getSessionFactory()
如下所示:
private static SessionFactory getSessionFactory()
{
if (sessionFactory == null) {
Configuration configuration = new Configuration().configure();
ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
registry.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = registry.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
return sessionFactory;
}
我使用 Hibernate 4.3.5 和 JBoss 7.1.1 创建了一个项目。 我把我项目的组成截图给大家:
WEB-INF/lib
上的罐子是:
下面是需要的类:
EntityDao.java
public class EntityDao implements EntityDaoInterface<Book, Integer>
{
private Session currentSession;
private Transaction currentTransaction;
public EntityDao() {
}
public Session openCurrentSession() {
currentSession = getSessionFactory().openSession();
return currentSession;
}
public Session openCurrentSessionwithTransaction() {
currentSession = getSessionFactory().openSession();
currentTransaction = currentSession.beginTransaction();
return currentSession;
}
public void closeCurrentSession() {
currentSession.close();
}
public void closeCurrentSessionwithTransaction() {
currentTransaction.commit();
currentSession.close();
}
private static SessionFactory getSessionFactory() {
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
return sessionFactory;
}
public Session getCurrentSession() {
return currentSession;
}
public void setCurrentSession(Session currentSession) {
this.currentSession = currentSession;
}
public Transaction getCurrentTransaction() {
return currentTransaction;
}
public void setCurrentTransaction(Transaction currentTransaction) {
this.currentTransaction = currentTransaction;
}
public void persist(Person entity) {
getCurrentSession().save(entity);
}
public void update(Person entity) {
getCurrentSession().update(entity);
}
public void delete(Person entity) {
getCurrentSession().delete(entity);
}
@SuppressWarnings("unchecked")
public List<Person> findAllPersons() {
List<Person> persons = (List<Person>) getCurrentSession().createQuery("from Person").list();
return persons;
}
public Person findPersonById(Integer id) {
Person person = (Person) getCurrentSession().get(Person.class, id);
return person;
}
}
EntityDaoInterface.java
public interface EntityDaoInterface<T, Id extends Serializable>
{
public void persist(T entity);
public void update(T entity);
public T findById(Id id);
public void delete(T entity);
public List<T> findAll();
public void deleteAll();
}
EntityService.java
public class EntityService
{
private static EntityDao entityDao;
public EntityService() {
entityDao = new EntityDao();
}
public void persist(Person entity) {
entityDao.openCurrentSessionwithTransaction();
entityDao.persist(entity);
entityDao.closeCurrentSessionwithTransaction();
}
public void update(Person entity) {
entityDao.openCurrentSessionwithTransaction();
entityDao.update(entity);
entityDao.closeCurrentSessionwithTransaction();
}
public void remove(Person entity) {
entityDao.openCurrentSessionwithTransaction();
entityDao.delete(entity);
entityDao.closeCurrentSessionwithTransaction();
}
public Person findPersonById(int id) {
entityDao.openCurrentSession();
Person person = entityDao.findPersonById(id);
entityDao.closeCurrentSession();
return person;
}
public List<Person> findAllPersons() {
entityDao.openCurrentSession();
List<Person> persons = entityDao.findAllPersons();
entityDao.closeCurrentSession();
return persons;
}
public void deleteAll() {
entityDao.openCurrentSessionwithTransaction();
entityDao.deleteAll();
entityDao.closeCurrentSessionwithTransaction();
}
public EntityDao entityDao() {
return entityDao;
}
}
Person.java
@Entity
@Table(name = "person")
public class Person
{
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private int id;
@Column(name = "age")
private int age;
@Column(name= "name")
String name;
public Person() {}
public Person(int id, int age, String name) {
super();
this.id = id;
this.age = age;
this.name = name;
}
// With the methods: set, get, toString, hashCode, equals
}
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;database=ccc</property>
<property name="hibernate.connection.username">lm</property>
<property name="hibernate.connection.password">pp</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="com.esprit.entity.Book"/>
<mapping class="com.esprit.entity.Person"/>
</session-factory>
</hibernate-configuration>
DataModelBeanD.java
@SuppressWarnings("serial")
@ManagedBean
@ViewScoped
public class DataModelBeanD implements Serializable
{
private List<Person> list;
private Person person = new Person();
private boolean edit;
private EntityService entityService;
@PostConstruct
public void init()
{
entityService = new EntityService();
list = entityService.findAllPersons();
}
}
项目运行后,出现如下错误:
08:32:54,297 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "HelloJPAHibernate.war"
08:33:01,282 INFO [org.hibernate.annotations.common.Version] (http-localhost-127.0.0.1-8080-1) HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
08:33:01,290 INFO [org.hibernate.Version] (http-localhost-127.0.0.1-8080-1) HHH000412: Hibernate Core {4.3.5.Final}
08:33:01,293 INFO [org.hibernate.cfg.Environment] (http-localhost-127.0.0.1-8080-1) HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.max_fetch_depth=5, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE, hibernate.bytecode.use_reflection_optimizer=false, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=5}
08:33:01,297 INFO [org.hibernate.cfg.Environment] (http-localhost-127.0.0.1-8080-1) HHH000021: Bytecode provider name : javassist
08:33:01,317 INFO [org.hibernate.cfg.Configuration] (http-localhost-127.0.0.1-8080-1) HHH000043: Configuring from resource: /hibernate.cfg.xml
08:33:01,318 INFO [org.hibernate.cfg.Configuration] (http-localhost-127.0.0.1-8080-1) HHH000040: Configuration resource: /hibernate.cfg.xml
08:33:01,366 INFO [org.hibernate.cfg.Configuration] (http-localhost-127.0.0.1-8080-1) HHH000041: Configured SessionFactory: null
08:33:01,453 WARN [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-localhost-127.0.0.1-8080-1) HHH000402: Using Hibernate built-in connection pool (not for production use!)
08:33:01,455 INFO [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-localhost-127.0.0.1-8080-1) HHH000401: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://localhost:1433;database=ccc]
08:33:01,457 INFO [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-localhost-127.0.0.1-8080-1) HHH000046: Connection properties: {user=lm, password=****}
08:33:01,458 INFO [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-localhost-127.0.0.1-8080-1) HHH000006: Autocommit mode: false
08:33:01,460 INFO [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-localhost-127.0.0.1-8080-1) HHH000115: Hibernate connection pool size: 5 (min=1)
08:33:03,123 INFO [org.hibernate.dialect.Dialect] (http-localhost-127.0.0.1-8080-1) HHH000400: Using dialect: org.hibernate.dialect.SQLServer2008Dialect
08:33:03,255 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/HelloJPAHibernate].[Faces Servlet]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet Faces Servlet threw exception: java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;
at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:936) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:824) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at com.esprit.dao.EntityDao.getSessionFactory(EntityDao.java:48) [classes:]
at com.esprit.dao.EntityDao.openCurrentSession(EntityDao.java:25) [classes:]
at com.esprit.dao.EntityService.findAllPersons(EntityService.java:86) [classes:]
at com.esprit.crud.dynamic.DataModelBeanD.init(DataModelBeanD.java:37) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_55]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_55]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_55]
at java.lang.reflect.Method.invoke(Unknown Source) [rt.jar:1.7.0_55]
我不知道遗漏了什么,请你帮我解决这个问题。任何命题都是提前appreciated.Thanks
我通过制作 Hibernate 4.2.21 而不是 Hibernate 4.3.5 解决了我的问题。然后我重新创建 class EntityDao.java
的方法 getSessionFactory()
如下所示:
private static SessionFactory getSessionFactory()
{
if (sessionFactory == null) {
Configuration configuration = new Configuration().configure();
ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
registry.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = registry.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
return sessionFactory;
}