我對着在春天JPA的一些問題。我成功地配置了Spring JPA項目,並且能夠在沒有任何異常的情況下運行項目。春天JPA實體不保存到數據庫
我內涵的實體保存到數據庫中。但是當我運行該項目時,它既不保存到數據庫也不拋出任何異常。
可能是什麼問題?我還添加了許多與hiberate相關的jar文件,因爲它在運行時拋出異常。現在我沒有得到任何例外。但實體不保存到數據庫中。我附加了我的Spring配置和Java類。
小枝配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- Connection Pool -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- JPA EntityManagerFactory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="${jdbc.database}"/>
<property name="showSql" value="${jdbc.showSql}"/>
</bean>
</property>
</bean>
<!-- Transaction manager for a single JPA EntityManagerFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"/>
<!-- Activates various annotations to be detected in bean classes for eg @Autowired-->
<context:annotation-config/>
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- <context:component-scan base-package="com.vemanchery.timesheet.dao"/> -->
<!-- Implementation Class -->
<bean id="employeeDao" class="com.test.dao.impl.EmployeeDao" />
<!-- services -->
<bean id="employeeService" class="com.test.service.impl.EmployeeService" >
<property name="employeeDao" ref="employeeDao"/>
</bean>
</beans>
DAO
package com.test.dao.impl;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.test.dao.interfaces.IEmployeeDao;
import com.test.model.interfaces.IEmployee;
//@Repository
//@Component
public class EmployeeDao implements IEmployeeDao{
@PersistenceContext()
private EntityManager entityManager ;
@Override
public boolean createEmployee(IEmployee employee) {
this.entityManager.persist(employee);
return true;
}
}
服務層
package com.test.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import com.test.dao.impl.EmployeeDao;
import com.test.dao.interfaces.IEmployeeDao;
import com.test.model.interfaces.IEmployee;
import com.test.service.interfaces.IEmployeeService;
public class EmployeeService implements IEmployeeService{
@Autowired
IEmployeeDao employeeDao;
public IEmployeeDao getEmployeeDao() {
return employeeDao;
}
public void setEmployeeDao(IEmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}
/**
*
*/
public boolean addEmployee(IEmployee employee){
employeeDao.createEmployee(employee);
return false;
}
}
感謝您的回覆..我添加了註釋。現在,我得到一個例外,例外在線程「主要」 java.lang.ClassCastException:$ Proxy14不能main.Main.main(Main.java:16) – user414967 2012-02-15 08:43:24
被轉換爲com.test.service.impl.EmployeeService \t @ user414967:使用IEmployeeService接口everwyhere而不是實現或啓用* proxy-target-class *選項。 – 2012-02-15 09:02:33
@Tomaz現在正在..多謝..還有一個疑問,如果春天JPA意味着它必須包括任何其他的ORM框架嗎?如果沒有ORM無法達到的效果吧?因爲它是一個巨大的文件現在包括大量的jar文件.. – user414967 2012-02-15 11:12:52