在修复previous issue之后,我又遇到了一些麻烦。我尝试了许多常见的修复,但似乎没有一个适合我。以下是我项目中的相关文件:
hibernate.cfg.xml中
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/accountdb</property>
<property name="connection.username">root</property>
<property name="connection.password">pass</property>
<!-- Hibernate properties -->
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property>
<!-- Mapping Files -->
<mapping resource="com/gto/test/Account.hbm.xml"/>
</session-factory>
</hibernate-configuration>
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="TestEJB3Project">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.gto.test.Account</class>
</persistence-unit>
Account.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.gto.test.Account" table="account">
<id name="id" column="id">
<generator class="increment" />
</id>
<property name="accessCount" column="accessCount"/>
</class>
</hibernate-mapping>
Account.java
package com.gto.test;
import java.io.Serializable;
import javax.persistence.*;
import java.util.List;
@Entity
@Table(name="account")
@NamedQuery(name="Account.findAll", query="SELECT a FROM Account a")
public class Account implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private int id;
private int accessCount;
//bi-directional many-to-one association to Accountaccesslog
@OneToMany(mappedBy="account")
private List<Accountaccesslog> accountaccesslogs;
public Account() {
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public int getAccessCount() {
return this.accessCount;
}
public void setAccessCount(int accessCount) {
this.accessCount = accessCount;
}
public List<Accountaccesslog> getAccountaccesslogs() {
return this.accountaccesslogs;
}
public void setAccountaccesslogs(List<Accountaccesslog> accountaccesslogs) {
this.accountaccesslogs = accountaccesslogs;
}
public Accountaccesslog addAccountaccesslog(Accountaccesslog accountaccesslog) {
getAccountaccesslogs().add(accountaccesslog);
accountaccesslog.setAccount(this);
return accountaccesslog;
}
public Accountaccesslog removeAccountaccesslog(Accountaccesslog accountaccesslog) {
getAccountaccesslogs().remove(accountaccesslog);
accountaccesslog.setAccount(null);
return accountaccesslog;
}
}
服务器日志
12:58:58,694 WARN [org.hibernate.orm.connections] (EJB default - 1) HHH10001002: Using Hibernate built-in connection pool (not for production use!)
12:58:58,698 INFO [org.hibernate.orm.connections] (EJB default - 1) HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/accountdb]
12:58:58,699 INFO [org.hibernate.orm.connections] (EJB default - 1) HHH10001001: Connection properties: {user=root, password=****}
12:58:58,699 INFO [org.hibernate.orm.connections] (EJB default - 1) HHH10001003: Autocommit mode: false
12:58:58,701 INFO [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (EJB default - 1) HHH000115: Hibernate connection pool size: 20 (min=1)
12:58:58,847 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management
12:58:58,847 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990
12:58:58,847 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: JBoss EAP 7.0.0.GA (WildFly Core 2.1.2.Final-redhat-1) started in 5746ms - Started 401 of 670 services (379 services are lazy, passive or on-demand)
12:58:58,860 ERROR [stderr] (EJB default - 1) Thu Jul 28 12:58:58 IST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
12:58:58,930 INFO [org.hibernate.dialect.Dialect] (EJB default - 1) HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
12:58:58,949 INFO [org.hibernate.envers.boot.internal.EnversServiceImpl] (EJB default - 1) Envers integration enabled? : true
12:58:58,957 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (EJB default - 1) HHH000228: Running hbm2ddl schema update
12:58:59,000 ERROR [org.jboss.as.ejb3] (EJB default - 1) WFLYEJB0020: Error invoking timeout for timer: [id=e4f1edcd-d8cb-4f38-9a17-78df26ccc2ea timedObjectId=TestEJB3Project.TestEJB3Project.TimerBean auto-timer?:true persistent?:true timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@5550bd initialExpiration=null intervalDuration(in milli sec)=0 nextExpiration=Thu Jul 28 12:59:00 IST 2016 timerState=IN_TIMEOUT info=Calls AccountAccessBean every 30 seconds]: javax.ejb.EJBException: org.hibernate.UnknownEntityTypeException: Unable to locate persister: com.gto.test.Account
at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:187)
at org.jboss.as.ejb3.tx.TimerCMTTxInterceptor.handleExceptionInOurTx(TimerCMTTxInterceptor.java:53)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:277)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:327)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:239)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ejb3.security.SecurityContextInterceptor.processInvocation(SecurityContextInterceptor.java:100)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ejb3.component.interceptors.ShutDownInterceptorFactory$1.processInvocation(ShutDownInterceptorFactory.java:64)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:54)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.java:64)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:356)
at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:636)
at org.jboss.invocation.AccessCheckingInterceptor.processInvocation(AccessCheckingInterceptor.java:61)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:356)
at org.jboss.invocation.PrivilegedWithCombinerInterceptor.processInvocation(PrivilegedWithCombinerInterceptor.java:80)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
at org.jboss.as.ejb3.timerservice.TimedObjectInvokerImpl.callTimeout(TimedObjectInvokerImpl.java:99)
at org.jboss.as.ejb3.timerservice.CalendarTimerTask.invokeBeanMethod(CalendarTimerTask.java:64)
at org.jboss.as.ejb3.timerservice.CalendarTimerTask.callTimeout(CalendarTimerTask.java:53)
at org.jboss.as.ejb3.timerservice.TimerTask.run(TimerTask.java:155)
at org.jboss.as.ejb3.timerservice.TimerServiceImpl$Task$1.run(TimerServiceImpl.java:1214)
at org.wildfly.extension.requestcontroller.RequestController$QueuedTask$1.run(RequestController.java:497)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: org.hibernate.UnknownEntityTypeException: Unable to locate persister: com.gto.test.Account
at org.hibernate.internal.SessionFactoryImpl.locateEntityPersister(SessionFactoryImpl.java:797)
at org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2710)
at org.hibernate.internal.SessionImpl.access$2500(SessionImpl.java:164)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2648)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2635)
at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1102)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:975)
at com.gto.test.AccountAccessBean.resigterAccountEvent(AccountAccessBean.java:47)
at com.gto.test.TimerBean.scheduledTimeout(TimerBean.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:57)
at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:61)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:437)
at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:82)
at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:95)
at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:61)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:57)
at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:61)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:47)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:437)
at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:73)
at org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor.processInvocation(EjbRequestScopeActivationInterceptor.java:83)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:52)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:275)
... 33 more
我一直坚持设置Hibernate一段时间了,感谢任何帮助。
答案 0 :(得分:4)
修正了它!我在使用Hibernate访问数据库的bean中构建我的SessionFactory时使用了addAnnotatedClass()方法。这是整个代码:
<强> AccountAccessBean.java 强>
@Stateless(mappedName = "AccountAccessBean")
@LocalBean
public class AccountAccessBean {
public void resigterAccountEvent() {
Configuration configuration = new Configuration().configure();
ServiceRegistry registry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.build();
SessionFactory sessionFactory = configuration
.addAnnotatedClass(Account.class)
.addAnnotatedClass(Accountaccesslog.class)
.buildSessionFactory(registry);
Session session = sessionFactory.openSession();
session.beginTransaction();
Account accountEntity = (Account) session.get(Account.class, new Integer(1));
System.out.println(accountEntity.getId());
}
}