本文介绍了配置hibernate 3.6依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用hibernate 3.6,但无法正确配置
使用我的maven配置
< ; hibernate.version> 3.6.6.Final< /hibernate.version>
< dependency>
< groupId> org.hibernate.javax.persistence< / groupId>
< artifactId> hibernate-jpa-2.0-api< / artifactId>
< version> 1.0.0.Final< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-entitymanager< / artifactId>
< version> $ {hibernate.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-core< / artifactId>
< version> $ {hibernate.version}< / version>
< /依赖关系>
< dependency>
< groupId> javax.persistence< / groupId>
< artifactId> persistence-api< / artifactId>
< version> 1.0< / version>
< /依赖关系>
< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql-connector-java< / artifactId>
< version> 5.1.6< / version>
< /依赖关系>
我得到
javax.servlet.ServletException:Servlet.init()用于servlet springapp抛出异常
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache .catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
org.apache.coyote.http11.Http11AprProcessor .process(Http11AprProcessor.java:284)
org.apache.coyote.http11.Http11AprProtocol $ Http11ConnectionHandler.process(Http11AprProtocol.java:322)
org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor .run(AprEndpoint.java:1699)
java.util.concurrent.ThreadPoolExecutor $ Worker.runTask(ThreadPoolExecutor.java:886)
java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java :908)
java.lang.Thread.run(Thread.java:662)
....
java.lang.NoClassDefFoundError:org / hibernate / MappingException
java.lang.Class。 getDeclaredConstructors0(本地方法)
java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
java.lang.Class.getDeclaredConstructors(Class.java:1836)
...
java.lang.ClassNotFoundException:org.hibernate.MappingException
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
org.apache.catalina.loader.WebappClassLoader.loadClass (WebappClassLoader.java:1521)
java.lang.Class.getDeclaredConstructors0(Native Method)
java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
java.lang.Class。 getDeclaredConstructors(Class.java:1836)
...
我将它与 spring 3.0.5.RELEASE
,如果有的话。我错过了什么?
UPD
我在我的依赖项中添加了spring-orm,但它没有帮助
异常抛出时我试着直接注入EntityManager
private EntityManager entityManager;
pre $ b $ public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
或者当我尝试使用Spring Data Project Repository
。
我的配置
<! - 指示Spring在注释类上执行声明性事务managemenet - >
< tx:注解驱动的事务管理器=transactionManager/>
<! - 使用本地JPA API驱动事务 - >
< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< property name =dataSourceref =dataSource/>
< property name =rollbackOnCommitFailurevalue =true/>
< property name =jpaDialect>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaDialect/>
< / property>
< / bean>
< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =persistenceUnitNamevalue =reportsPersistence/>
< property name =jpaVendorAdapterref =jpaVendor/>
< property name =jpaDialect>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaDialect/>
< / property>
< / bean>
< bean id =jpaVendorclass =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>
< property name =databasevalue =MYSQL/>
< / bean>
<! - 数据源配置 - >
< bean id =dataSourceclass =com.mchange.v2.c3p0.ComboPooledDataSource>
< property name =driverClassvalue =com.mysql.jdbc.Driver/>
< property name =jdbcUrlvalue =jdbc:mysql:// localhost:3306 / someProj?autoReconnect = true/>
< property name =uservalue =someUser/>
< property name =passwordvalue =somePass/>
< property name =autoCommitOnClosevalue =false/>
< property name =minPoolSizevalue =5/>
< property name =numHelperThreadsvalue =20/>
< property name =initialPoolSizevalue =25/>
< property name =maxPoolSizevalue =250/>
< property name =breakAfterAcquireFailurevalue =true/>
< property name =acquireRetryDelayvalue =20/>
< property name =maxIdleTimeExcessConnectionsvalue =600/>
< property name =maxIdleTimevalue =600/>
< / bean>
< bean id =sessionFactory
class =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.MySQLDialect< / prop>
< prop key =hibernate.hbm2ddl.auto> update< / prop>
< /道具>
< / property>
< property name =annotatedClasses>
< list>
<值> com.domain.User< /值>
< / list>
< / property>
< / bean>
persistence.xml
< persistence-unit name =reportsPersistencetransaction-type =RESOURCE_LOCAL>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQL5Dialect/>
< property name =hibernate.show_sqlvalue =true/>
< / properties>
< / persistence-unit>
用户
是一个简单的JPA实体
@Entity
@Table(name =USERS)
public class User {
private long id = -1;
私人字符串名称;
$ b $ public User(){
}
public User(String name){
this.name = name;
$ b @Id
@GeneratedValue()
public long getId(){
return id;
}
public void setId(long id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
@Override
public boolean equals(Object o){
if(this == o)return true;
if(o == null || getClass()!= o.getClass())return false;
用户用户=(用户)o;
if(id!= user.id)return false;
返回true;
}
@Override
public int hashCode(){
return(int)(id ^(id>>> 32));
$ div $解析方案
你不要说你正在运行什么或者你在做什么来获得这个错误。 MappingException ,所以你的依赖不是问题。然而,无论您运行的是否正确使用Maven类路径。
I want to use hibernate 3.6 but I can't configure it properly
With my maven configuration
<hibernate.version>3.6.6.Final</hibernate.version>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
I get
javax.servlet.ServletException: Servlet.init() for servlet springapp threw exception
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:284)
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:322)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1699)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
java.lang.Thread.run(Thread.java:662)
....
java.lang.NoClassDefFoundError: org/hibernate/MappingException
java.lang.Class.getDeclaredConstructors0(Native Method)
java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
java.lang.Class.getDeclaredConstructors(Class.java:1836)
...
java.lang.ClassNotFoundException: org.hibernate.MappingException
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
java.lang.Class.getDeclaredConstructors0(Native Method)
java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
java.lang.Class.getDeclaredConstructors(Class.java:1836)
...
I use it together with spring 3.0.5.RELEASE
, if it matter. What does I miss?
UPDI added spring-orm to my dependencies, but it diddn't helpException is throwing when I try inject EntityManager directly private EntityManager entityManager;
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
Or when I try using Spring Data Project Repository
.My configuration
<!-- Instructs Spring to perfrom declarative transaction managemenet on annotated classes -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- Drives transactions using local JPA APIs -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="dataSource" ref="dataSource"/>
<property name="rollbackOnCommitFailure" value="true"/>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property>
</bean>
<!-- Creates a EntityManagerFactory for use with the Hibernate JPA provider and a simple in-memory data source populated with test data -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="reportsPersistence"/>
<property name="jpaVendorAdapter" ref="jpaVendor"/>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property>
</bean>
<bean id="jpaVendor" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL"/>
</bean>
<!-- DataSource configuration -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/someProj?autoReconnect=true"/>
<property name="user" value="someUser"/>
<property name="password" value="somePass"/>
<property name="autoCommitOnClose" value="false"/>
<property name="minPoolSize" value="5"/>
<property name="numHelperThreads" value="20"/>
<property name="initialPoolSize" value="25"/>
<property name="maxPoolSize" value="250"/>
<property name="breakAfterAcquireFailure" value="true"/>
<property name="acquireRetryDelay" value="20"/>
<property name="maxIdleTimeExcessConnections" value="600"/>
<property name="maxIdleTime" value="600"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.domain.User</value>
</list>
</property>
</bean>
persistence.xml
<persistence-unit name="reportsPersistence" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
User
is a simple JPA entity
@Entity
@Table(name = "USERS")
public class User {
private long id = -1;
private String name;
public User() {
}
public User(String name) {
this.name = name;
}
@Id
@GeneratedValue()
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
if (id != user.id) return false;
return true;
}
@Override
public int hashCode() {
return (int) (id ^ (id >>> 32));
}
}
解决方案
You don't say what you're running or what you're doing to get that error. MappingException comes from hibernate-core, so your dependencies aren't the problem. However and whatever you're running isn't correctly using the Maven classpath.
这篇关于配置hibernate 3.6依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!