简体   繁体   English

Spring JPA 实体未保存到数据库

[英]Spring JPA entities not saving to database

I am facing some issues in Spring JPA.我在 Spring JPA 中遇到了一些问题。 I successfully configured the Spring JPA project and am able to run the project without having any exception.我成功配置了 Spring JPA 项目并且能够运行该项目而没有任何异常。

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.但是当我运行该项目时,它既不保存到数据库也不抛出任何异常。

What could be the problem?可能是什么问题呢? I have added many hiberate related jar files as well because it was throwing exceptions when I run.我还添加了许多与休眠相关的 jar 文件,因为它在我运行时抛出异常。 Now i am not getting any exception.现在我没有得到任何例外。 But entities are not saved into database.但实体不会保存到数据库中。 I have attached my Spring configuration and Java classes.我附上了我的 Spring 配置和 Java 类。

sprig configuration小枝配置

<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;
    }


}

Service Layer服务层

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;
    }

}

Add @Transactional annotation over your service class or method storing entities: 在服务类或方法存储实体上添加@Transactional注释:

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

Adding @Repository over your DAO is also a good idea, but it is not causing your problems. 在DAO上添加@Repository也是一个好主意,但它不会导致您的问题。

I also faced such a problem.我也遇到过这样的问题。 Two methods worked.两种方法奏效。 I added super() in all argument constructor method in Entity.我在实体的所有参数构造函数方法中添加了super()

The other method is to replace the JPA Repository in the Repository file I wrote PagingAndSortingRepository.另一种方法是替换我写的PagingAndSortingRepository的Repository文件中的JPA Repository。

@Repository
public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Long> {

}

long: empoloyeeId data type long:employeeId 数据类型

Enable annotation based processing for service and dao (I have never mixed xml config and annotation,so dont know if config setting is fine) and mark your service method 为服务和dao启用基于注释的处理(我从未混合过xml配置和注释,因此不知道配置设置是否正常)并标记您的服务方法

addEmployee @Transactional addEmployee @Transactional

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM