本文介绍了EntityManager persist()方法不会将记录插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用 EntityManager.persist(Object)方法的问题。现在,当我摆脱其他问题时,通过应用程序工作而无需Exception,但对象不会放在我的数据库中。



我的实体类:

  @Entity 
@Table(name =Chain)
public class Chain implements Serializable {

@ Id
@Column(name =id)
私人长ID;
@Column(name =date)
私人日期日期;
@Column(name =name)
私人字符串名称;
// setters and getters
}

my dao class:

  @Transactional 
@Repository(ChainDao)
公共类ChainDaoImpl实现ChainDao {


私人EntityManager em;


@PersistenceContext
public void setEntityManager(EntityManager em){
this。 em = em;
}

public int saveChain(Chain chain){
chain.setDate(new Date());
chain.setId((long)44);
布尔a;
em.persist(chain);

返回222;


my xml context:

 < bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean> 
< property name =persistenceXmlLocationvalue =classpath *:META-INF / persistence.xml>< / property>< / bean>


< bean class =org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor/>

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

< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>

和 pereistence.xml :

 < persistence xmlns =http://java.sun.com/xml/ns/persistenceversion =1.0> 
< persistence-unit name =sample>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
<属性>
< property name =hibernate.archive.autodetectionvalue =class,hbm/>
< property name =hibernate.connection.driver_classvalue =org.postgresql.Driver/>
< property name =hibernate.connection.urlvalue =jdbc:postgresql:// localhost:5432 / database/>
< property name =hibernate.connection.usernamevalue =postgres/>
< property name =hibernate.connection.passwordvalue =pwd/>
< property name =hibernate.dialectvalue =org.hibernate.dialect.PostgreSQLDialect/>
< property name =hibernate.show_sqlvalue =true/>
< / properties>
< / persistence-unit>

< /持久性>

有没有人知道我错过了什么?

解决方案

你是否得到一个特定的异常?如果你知道它是什么会很有帮助的。



在Stackoverflow上有许多类似的问题:

Spring事务上下文不会持久数据 a>





这些建议您应该尝试添加 em.flush()在 em.persist(chain)后改变 @transactional 注解 p>

您还应该检查是否已通过以下行的方式启用事务注释:

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

在您的.xml配置中


I have problem with using EntityManager.persist(Object) method. Now when i get rid of other problems, by app work without Exception but object is not put in my database.

my entity class:

@Entity
@Table(name ="Chain")
public class Chain implements Serializable{

@Id
@Column(name = "id")
  private Long id;
@Column(name = "date")
  private Date date;
@Column(name = "name")
  private String name;
//setters and getters
}

my dao class:

@Transactional
@Repository("ChainDao")
public class ChainDaoImpl implements ChainDao{


    private EntityManager em;


    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        this. em = em;
    }

    public int saveChain(Chain chain) {
        chain.setDate(new Date());
        chain.setId((long)44);
        Boolean a;
        em.persist(chain);

        return 222;
    }
}

my xml context:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
    <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property></bean>


    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

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

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

and pereistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
       <persistence-unit name="sample">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <!-- Scan for annotated classes and Hibernate mapping XML files -->
        <properties>
           <property name="hibernate.archive.autodetection" value="class, hbm"/>
           <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
           <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/database"/>
           <property name="hibernate.connection.username" value="postgres"/>
           <property name="hibernate.connection.password" value="pwd"/>
           <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
           <property name="hibernate.show_sql" value="true"/>
        </properties>
        </persistence-unit>

    </persistence>

Do anyone have a idea what am i missing?

解决方案

Are you getting a specific exception? It would be helpful to know what it is if you are.

There are a number of similar problems to this already on Stackoverflow:

Spring transactional context doesn't persist data

Spring, Hibernate & JPA: Calling persist on entitymanager does not seem to commit to database

These suggest that you should try adding em.flush() after the em.persist(chain), and altering the @transactional annotations

You should also check that you have enabled transactional annotations through a line such as :

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

in your .xml configuration

这篇关于EntityManager persist()方法不会将记录插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 00:43