保存方法不持久化实体对象Spring Data JPA

时间:2017-11-17 11:38:52

标签: java spring hibernate spring-data spring-data-jpa

我正在将当前代码从hibernate和mybatis移动到spring数据jpa。我使用现有应用程序配置了spring数据并尝试执行某些操作。

当我尝试使用save(T t)持久保存实体时,它没有返回任何异常并返回相同的实体对象,但结果却没有。

的persistence.xml

<!--
          Configure Spring Data JPA and set the base package of the
          repository interfaces
        -->
<jpa:repositories base-package="com.moneyhaul.repositary"/>


    <!-- Create default configuration for Hibernate -->
    <bean id="hibernateJpaVendorAdapter"
          class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>

    <!-- Configure the entity manager factory bean -->
    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
        <!-- Set JPA properties -->
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="javax.persistence.schema-generation.database.action">none</prop>
                <prop key="hibernate.ejb.use_class_enhancer">false</prop>
            </props>
        </property>
        <!-- Set base package of your entities -->
        <property name="packagesToScan" value="com.moneyhaul.dao.model"/>
        <!-- Set share cache mode -->
        <property name="sharedCacheMode" value="ENABLE_SELECTIVE"/>
        <!-- Set validation mode -->
        <property name="validationMode" value="NONE"/>

    </bean>

    <!-- Configure the transaction manager bean -->
    <bean id="transactionManagerJpa"
          class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <!-- Enable annotation driven transaction management -->
    <tx:annotation-driven transaction-manager="transactionManagerJpa"/>









<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- Mybatis session factory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
    p:dataSource-ref="dataSource" p:configLocation="WEB-INF/mybatis/sqlmap-config.xml" />

<bean id="caarmoMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
    p:mapperInterface="com.caarmo.mapper.CaarmoMapper"
    p:sqlSessionFactory-ref="sqlSessionFactory" />

<bean id="paymentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
    p:mapperInterface="com.caarmo.mapper.PaymentMapper"
    p:sqlSessionFactory-ref="sqlSessionFactory" />

<bean id="reportingMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
    p:mapperInterface="com.caarmo.mapper.ReportingMapper"
    p:sqlSessionFactory-ref="sqlSessionFactory" />

<bean id="reportingTrendsMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
    p:mapperInterface="com.caarmo.mapper.ReportingTrendsMapper"
    p:sqlSessionFactory-ref="sqlSessionFactory" />

<bean id="customerDBMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
    p:mapperInterface="com.caarmo.mapper.CustomerDBMapper"
    p:sqlSessionFactory-ref="sqlSessionFactory" />

<bean id="vehicleDBMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
    p:mapperInterface="com.caarmo.mapper.VehicleDBMapper"
    p:sqlSessionFactory-ref="sqlSessionFactory" />

<bean id="geomaticsMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
    p:mapperInterface="com.caarmo.mapper.GeomaticsMapper"
    p:sqlSessionFactory-ref="sqlSessionFactory" />

<bean id="serviceCenterMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
    p:mapperInterface="com.caarmo.mapper.ServiceCenterMapper"
    p:sqlSessionFactory-ref="sqlSessionFactory" />

<bean id="moneyhaulMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
    p:mapperInterface="com.moneyhaul.mapper.MoneyHaulMapper"
    p:sqlSessionFactory-ref="sqlSessionFactory" />

<bean id="mhGeomaticsMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
    p:mapperInterface="com.moneyhaul.mapper.MHGeomaticsMapper"
    p:sqlSessionFactory-ref="sqlSessionFactory" />

<bean id="configurationMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
    p:mapperInterface="com.caarmo.mapper.ConfigurationMapper"
    p:sqlSessionFactory-ref="sqlSessionFactory" />
@Repository
public interface DriverRepositary extends CrudRepository<Driver,Long>{}

DriverRepositary.java

@Transactional
public void addDriver(Driver driver) {
    driver.setTabletPassword(setDriverPassword(driver.getPhone()));
    driver.setUpdated(new Date());
    driverRepositary.save(driver);

}

坚持实体

{{1}}

1 个答案:

答案 0 :(得分:0)

您可以查看以下内容:

  1. 声明了两个Transaction Manager Bean。他们需要吗?
  2. 为什么需要会话工厂bean声明?
  3. 将hibernate.show_sql设置为true,作为JPA Properties(用于调试)的一部分
  4. 是显式设置主键还是使用序列?
  5. 如果可能的话,还提供服务器启动时可用的所有日志,直到此错误点进行进一步分析。