java - Spring JPA 实体不保存到数据库

标签 java spring dao

我在 Spring JPA 中遇到了一些问题。我成功地配置了 Spring JPA 项目,并且能够毫无异常地运行该项目。

我打算将实体保存到数据库中。但是当我运行项目时,它既没有保存到数据库也没有抛出任何异常。

可能是什么问题?我也添加了许多与 hiberate 相关的 jar 文件,因为它在我运行时抛出异常。现在我没有任何异常(exception)。但是实体没有保存到数据库中。我附上了我的 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;
    }

}

最佳答案

在您的服务类或方法存储实体上添加 @Transactional 注释:

@Transactional
public boolean addEmployee(IEmployee employee){
    employeeDao.createEmployee(employee);       
    return false;
}

在您的 DAO 上添加 @Repository 也是一个好主意,但这不会导致您的问题。

关于java - Spring JPA 实体不保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9289798/

相关文章:

java - 在java中修改密码

java - 在 CDMA BlackBerry 设备(OS5 及更高版本)中获取 Cell ID 和 LAC

javascript - Angular 4 : Redirecting user to last visited page in previous session on login

java - 我的 'select'方法的返回类型是否错误?

c++ - CDaoRecordset 如果字段存在于 MFC C++

java - 如何更改由相同注释(例如 AspectJ 中的 @Around)注释的两个或多个建议的执行顺序?

java - 计算联合矩形周长的算法

java - Spring @Validated 和 @InitBinder

java - 无法直接在 JFrame 上绘画

java - Mybatis 一对多的集合映射总是有一个默认实体