Spring JPA实体未保存到数据库 [英] Spring JPA entities not saving to database

查看:99
本文介绍了Spring JPA实体未保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring JPA面临一些问题。我成功配置了Spring JPA项目,并且能够在没有任何异常的情况下运行该项目。

I am facing some issues in Spring JPA. I successfully configured the Spring JPA project and am able to run the project without having any exception.

我想将实体保存到数据库。但是当我运行该项目时,它既没有保存到数据库中,也没有引发任何异常。

I intension to save the entities to the database. But when I am running the project, it is neither saving to the database nor throwing any exceptions.

可能是什么问题?我还添加了许多与休眠相关的jar文件,因为它在运行时引发异常。现在我没有任何异常。但是实体不会保存到数据库中。我已经附加了Spring配置和Java类。

What could be the problem? I have added many hiberate related jar files as well because it was throwing exceptions when I run. Now i am not getting any exception. But entities are not saved into database. I have attached my Spring configuration and Java classes.

小枝配置

<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 批注:

Add @Transactional annotation over your service class or method storing entities:

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

添加 @存储库在您的DAO上也是一个好主意,但这不会引起您的问题。

Adding @Repository over your DAO is also a good idea, but it is not causing your problems.

这篇关于Spring JPA实体未保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆