一尘不染

EntityManagerpersist()不保存任何东西到数据库

hibernate

我的entityManagerpersist()从序列中获取ID,并将其放入我的Image对象,但是Image对象本身未显示在数据库中。EntityManager.flush()给出错误,所以我不能以这种方式提交。这是我的代码。

@Repository
public class ImageDaoImpl extends BaseDao implements ImageDao {

@PersistenceContext
protected EntityManager entityManager;

@Override
@Transactional
public void create(Image image) {       
    JpaTemplate jpaTemplate = getJpaTemplate(entityManager);
    jpaTemplate.persist(image);
}



@Repository
public class BaseDao {

private JpaTemplate jpaTemplate;


public JpaTemplate getJpaTemplate(EntityManager entityManager){
    if(jpaTemplate == null)
        jpaTemplate = new JpaTemplate(entityManager);
    return jpaTemplate;
}



<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="showSql" value="true" />
                <property name="generateDdl" value="true" />
                <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
            </bean>
        </property>
        <property name="persistenceUnitName" value="sample"></property>
    </bean>



    <!-- DataSource Setup -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/imageCapture" />
        <property name="username" value="myusername" />
        <property name="password" value="mypassword" />
    </bean>


    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

阅读 332

收藏
2020-06-20

共1个答案

一尘不染

这通常在未应用Transaction in时发生。我怀疑@Transactional拦截器没有正确拦截。

2020-06-20