我想学一下怎么用 Hibernate 实现的 JPA,结果报错:
...
Caused by: java.lang.NoSuchMethodError:
org.hibernate.cfg.AnnotationConfiguration.setProperty(Ljava/lang/String;
Ljava/lang/String;)Lorg/hibernate/cfg/AnnotationConfiguration;
at org.hibernate.ejb.Ejb3Configuration.setProperty(Ejb3Configuration.java:535)
at org.hibernate.ejb.Ejb3Configuration.setDataSource(Ejb3Configuration.java:149)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:399)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory
(HibernatePersistence.java:131)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntity
ManagerFactory(LocalContainerEntityManagerFactoryBean.java:224)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet
(AbstractEntityManagerFactoryBean.java:291)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1368)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
.initializeBean(AbstractAutowireCapableBeanFactory.java:1334)
... 39 more
相关类库(貌似没什么冲突啊):
hibernate-annotations-3.4.0.GA
hibernate-core-3.3.1.GA
hibernate-entitymanager-3.4.0.GA
相关 jar 包:
antlr-2.7.6.jar
cglib.jar
commons-beanutils.jar
commons-chain-1.1.jar
commons-collections-3.1.jar
commons-configuration-1.2.jar
commons-dbcp-1.2.1.jar
commons-digester-1.7.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-lang-2.4.jar
commons-logging-api-1.1.jar
commons-logging.jar
commons-pool-1.2.jar
commons-validator-1.3.0.jar
dom4j-1.6.1.jar
ejb3-persistence.jar
freemarker.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-core.jar
hibernate-entitymanager.jar
javassist-3.4.GA.jar
jta-1.1.jar
log4j-1.2.13.jar
mysql-connector-java-5.1.7-bin.jar
ognl.jar
slf4j-api-1.5.6.jar
slf4j-simple-1.5.6.jar
spring-webmvc.jar
spring.jar
struts2-core.jar
struts2-spring-plugin-2.1.6.jar
xwork.jar
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/test"/>
<property name="username" value="user"/>
<property name="password" value="user"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="/WEB-INF/persistence.xml"/>
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- spring 配置文件中配置 action -->
<bean id="userService" class="login.DefaultUserService">
<property name="userDao" ref="userDao"/>
</bean>
<bean id="userDao" class="login.DefaultUserDao">
</bean>
</beans>
persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>