Spring,Hibernate& JPA:对entitymanager的持续调用似乎没有提交数据库 [英] Spring, Hibernate & JPA: Calling persist on entitymanager does not seem to commit to database

查看:85
本文介绍了Spring,Hibernate& JPA:对entitymanager的持续调用似乎没有提交数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Hibernate和JPA来设置Spring,但是当试图持久保存一个对象时,似乎没有任何东西添加到数据库中。



正在使用以下内容:

 < bean id =dataSourceclass =org.apache.commons.dbcp.BasicDataSource> 
< property name =urlvalue =$ {jdbc.url}/>
< property name =driverClassNamevalue =$ {jdbc.driverClassName}/>
< property name =usernamevalue =$ {jdbc.username}/>
< property name =passwordvalue =$ {jdbc.password}/>
< / bean>

< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =persistenceUnitNamevalue =BankingWeb/>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>
< property name =generateDdlvalue =true/>
< property name =showSqlvalue =true/>
< property name =databasePlatformvalue =$ {hibernate.dialect}/>
< / bean>
< / property>
< / bean>

< tx:annotation-driven />

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

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

< bean class =org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor/>

< bean name =accountManagerclass =ssel.banking.dao.jpa.AccountManager/>
< bean name =userManagerclass =ssel.banking.dao.jpa.UserManager/>

在AccountManager中,我在做:

  @Repository 
public class AccountManager实现IAccountManager {

@PersistenceContext private EntityManager em;

/ * - 8 < - 省略查询方法 - 8< - * /

public account storeAccount(Account ac){
ac = em.merge(ac);
em.persist(ac);
return ac;


其中ac来自:

  Account ac = new Account(); 
ac.setId(mostRecent.getId()+ 1);
ac.setUser(user);
ac.setName(accName);
ac.setDate(new Date());
ac.setValue(0);
ac = accountManager.storeAccount(ac);
return ac;

有人能指出我做错了什么吗?持续调用返回时不会抛出异常。如果事后我做了 em.contains(ac),这将返回true。



如果有人需要,帐户已定义:

  @SuppressWarnings(serial)
@Entity
@NamedQueries({
@NamedQuery(name =Account.AllAccounts,query =SELECT a FROM Account a),
@NamedQuery(name =Account.Accounts4User,query =SELECT a FROM Account a WHERE user =:user),
@NamedQuery(name =Account.Account4Name,query =SELECT a FROM Account a WHERE name =:name),
@NamedQuery(name =Account.MaxId ,query =SELECT MAX(a.id)FROM Account a),
@NamedQuery(name =Account.Account4Id,query =SELECT a FROM Account a WHERE id =:id),
$)
public class Account扩展AbstractNamedDomain {
@Temporal(TemporalType.DATE)
@Column(name =xdate)
private日期date;

私人双重价值;

@ManyToOne(cascade = {CascadeType.PERSIST,CascadeType.MERGE})
@JoinColumn(name =userid)
private用户用户;

public User getUser(){
return user;
}

public void setUser(User user){
this.user = user;
}

@OneToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE},fetch = FetchType.EAGER)
@OrderBy(date)
private列表与LT; AccountActivity> accountActivity = new ArrayList< AccountActivity>();

公共列表< AccountActivity> getAccountActivity(){
return accountActivity;
}

public void setAccountActivity(List< AccountActivity> accountActivity){
this.accountActivity = accountActivity;
}

public Date getDate(){
return date;
}

public void setDate(Date date){
this.date = date;
}

public double getValue(){
返回值;
}

public void setValue(double value){
this.value = value;
}

public void addAccountActivity(AccountActivity activity){
//确保订单保持不变,JPA只在加载
时使用这个函数int i = 0; (getAccountActivity()。size()){
if(getAccountActivity().get(i).getDate()。compareTo(activity.getDate())< = 0)
休息;
i ++;
}
getAccountActivity()。add(i,activity);



@MappedSuperclass public abstract class AbstractNamedDomain extends AbstractDomain {

private String name;
$ b $ public AbstractNamedDomain(){

}
$ b $ public AbstractNamedDomain(String name){

this.name = name ;
}

public String getName(){
return name;
}

public void setName(String name){
this.name = name;



@MappedSuperclass public abstract class AbstractDomain implements Serializable {

@Id @GeneratedValue
private long id = NEW_ID;

public static long NEW_ID = -1;

public long getId(){
return id;
}

public void setId(long id){
this.id = id;
}

public boolean isNew(){

return id == NEW_ID;



$ div $解析方案

谢谢对eric和胡安曼努埃尔的回答,我能够弄清楚交易没有提交。



将@Transactional添加到storeAccount方法中有窍门!


I'm trying to setup Spring using Hibernate and JPA, but when trying to persist an object, nothing seems to be added to the database.

Am using the following:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="url" value="${jdbc.url}"/>
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="BankingWeb" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="true" />
            <property name="showSql" value="true" />
            <property name="databasePlatform" value="${hibernate.dialect}" />
        </bean>
    </property>
</bean>

<tx:annotation-driven/>

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

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

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

<bean name="accountManager" class="ssel.banking.dao.jpa.AccountManager" />
<bean name="userManager" class="ssel.banking.dao.jpa.UserManager" />

And in AccountManager, I'm doing:

@Repository
public class AccountManager implements IAccountManager {

    @PersistenceContext private EntityManager em;

    /* -- 8< -- Query methods omitted -- 8< -- */

    public Account storeAccount(Account ac) {
    ac = em.merge(ac);
        em.persist(ac);
        return ac;
    }
}

Where ac comes from:

    Account ac = new Account();
    ac.setId(mostRecent.getId()+1);
    ac.setUser(user);
    ac.setName(accName);
    ac.setDate(new Date());
    ac.setValue(0);
    ac = accountManager.storeAccount(ac);
    return ac;

Is there anyone who can point out what I'm doing wrong? The persist call returns without throwing exceptions. If afterwards I do em.contains(ac), this returns true.

In case anyone needed, here's how Account is defined:

@SuppressWarnings("serial")
@Entity
@NamedQueries({
        @NamedQuery(name = "Account.AllAccounts", query = "SELECT a FROM Account a"),
        @NamedQuery(name = "Account.Accounts4User", query = "SELECT a FROM Account a WHERE user=:user"), 
        @NamedQuery(name = "Account.Account4Name", query = "SELECT a FROM Account a WHERE name=:name"),
        @NamedQuery(name = "Account.MaxId", query = "SELECT MAX(a.id) FROM Account a"),
        @NamedQuery(name = "Account.Account4Id", query = "SELECT a FROM Account a WHERE id=:id"),
    })
public class Account extends AbstractNamedDomain {
    @Temporal(TemporalType.DATE)
    @Column(name = "xdate")
    private Date date;

    private double value;

    @ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name="userid")
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER)
    @OrderBy("date")
    private List<AccountActivity> accountActivity = new ArrayList<AccountActivity>();

    public List<AccountActivity> getAccountActivity() {
        return accountActivity;
    }

    public void setAccountActivity(List<AccountActivity> accountActivity) {
        this.accountActivity = accountActivity;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    public void addAccountActivity(AccountActivity activity) {
        // Make sure ordering is maintained, JPA only does this on loading
        int i = 0;
        while (i < getAccountActivity().size()) {
            if (getAccountActivity().get(i).getDate().compareTo(activity.getDate()) <= 0)
                break;
            i++;
        }
        getAccountActivity().add(i, activity);
    }
}

@MappedSuperclass public abstract class AbstractNamedDomain extends AbstractDomain {

    private String name;

    public AbstractNamedDomain() {

    }

    public AbstractNamedDomain(String name) {

        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

@MappedSuperclass public abstract class AbstractDomain implements Serializable {

    @Id @GeneratedValue
    private long id = NEW_ID;

    public static long NEW_ID = -1;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id; 
    }

    public boolean isNew() {

        return id==NEW_ID;
    }
}

解决方案

Thanks to eric and Juan Manuel's answers, I was able to figure out that the transaction wasn't committed.

Adding @Transactional to the storeAccount method did the trick!

这篇关于Spring,Hibernate&amp; JPA:对entitymanager的持续调用似乎没有提交数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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