【问题标题】:Hibernate & Spring 3 not saving休眠和 Spring 3 不保存
【发布时间】:2013-05-06 16:02:47
【问题描述】:

我正在使用 hibernate 3 和 spring 3.0.7,我试图让我的代码将实体保存到数据库中,但它不会这样做。

我已经尝试了一切,但似乎我做错了什么。

这是我使用 dao 方法的类

public boolean saveData(DataHolder holder,int type,Owners found){

    TempData temp = new TempData();
    temp.setDate(holder.getDate());
    temp.setTimestamp(new Timestamp(holder.getDate().getTime()));
    temp.setName(holder.getName());
    temp.setComId(holder.getCom().getComId());
    temp.setSymbol(holder.getCom().getSymbol());
    temp.setPercent(holder.getPercent());
    if(type == 1){
        temp.setOwnerId(found.getOwnerId());
        temp.setOwnerType(found.getType());
        tempDao.addTemp(temp);
        return true;
    }
    else{
        tempDao.addTemp(temp);
        return false;
    }
}

当然这是一个带有组件注解的bean

这是我的 dao 的 add 方法不起作用

公共布尔 addTemp(TempData 实体){ 尝试 { getSession().save(实体); 返回真; } 捕捉(异常 e){ e.printStackTrace(); 返回假; } }

这是我的实体

@组件 公共类 TempData {

private int tempId;
private Date date;
private Timestamp timestamp;
private String name;
private String ownerType;
private Integer ownerId;
private String symbol;
private Integer comId;
private Double percent;

public int getTempId() {
    return tempId;
}

public void setTempId(int tempId) {
    this.tempId = tempId;
}

public Date getDate() {
    return date;
}

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

public Timestamp getTimestamp() {
    return timestamp;
}

public void setTimestamp(Timestamp timestamp) {
    this.timestamp = timestamp;
}

public String getName() {
    return name;
}

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

public String getOwnerType() {
    return ownerType;
}

public void setOwnerType(String ownerType) {
    this.ownerType = ownerType;
}

public Integer getOwnerId() {
    return ownerId;
}

public void setOwnerId(Integer ownerId) {
    this.ownerId = ownerId;
}

public String getSymbol() {
    return symbol;
}

public void setSymbol(String symbol) {
    this.symbol = symbol;
}

public Integer getComId() {
    return comId;
}

public void setComId(Integer comId) {
    this.comId = comId;
}

public Double getPercent() {
    return percent;
}

public void setPercent(Double percent) {
    this.percent = percent;
}

这是我的hbm

这是spring config的相关部分

<tx:advice id="tx" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="find*" propagation="REQUIRES_NEW"/>
        <tx:method name="add*" propagation="REQUIRES_NEW"/>
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:advisor advice-ref="tx" pointcut="execution(* *..AbstractDao.*(..))" />
    <aop:advisor advice-ref="tx" pointcut="execution(* *..TempDataDao.addTemp(..))" />
</aop:config>

问题是它可以完美地检索数据,但是在保存数据时它就不起作用了,并且不同项目中的相同方法可以完美地工作,但是在这里他们只是没有,并且日志没有说明任何错误或某事,我什至尝试将映射到错误的表的名称更改但仍然没有错误并完成事务,

我在这里错过了什么?

编辑

这是我的调试器所显示的,仅此而已

DEBUG [http-bio-8080-exec-7] (HibernateTransactionManager.java:569) - Exposing Hibernate transaction as JDBC transaction [jdbc:mysql://localhost:3306/parse_web, UserName=root@localhost, MySQL-AB JDBC Driver]
DEBUG [http-bio-8080-exec-7] (SessionImpl.java:265) - opened session at timestamp: 13683691714
DEBUG [http-bio-8080-exec-7] (AbstractSaveEventListener.java:134) - generated identifier: 0, using strategy: org.hibernate.id.Assigned
DEBUG [http-bio-8080-exec-7] (AbstractPlatformTransactionManager.java:752) - Initiating transaction commit
DEBUG [http-bio-8080-exec-7] (HibernateTransactionManager.java:652) - Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@573a46b6]
DEBUG [http-bio-8080-exec-7] (JDBCTransaction.java:130) - commit
DEBUG [http-bio-8080-exec-7] (JDBCTransaction.java:223) - re-enabling autocommit
DEBUG [http-bio-8080-exec-7] (JDBCTransaction.java:143) - committed JDBC Connection
DEBUG [http-bio-8080-exec-7] (ConnectionManager.java:325) - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
DEBUG [http-bio-8080-exec-7] (HibernateTransactionManager.java:734) - Closing Hibernate Session [org.hibernate.impl.SessionImpl@573a46b6] after transaction
DEBUG [http-bio-8080-exec-7] (SessionFactoryUtils.java:789) - Closing Hibernate Session
DEBUG [http-bio-8080-exec-7] (ConnectionManager.java:464) - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
DEBUG [http-bio-8080-exec-7] (ConnectionManager.java:325) - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!

这就是我获得会话的方式

public Session getSession(){
        return (this.factory.getCurrentSession()==null)?
                this.factory.getCurrentSession() : this.factory.openSession();
    }

【问题讨论】:

  • 你是在Service层还是DAO层添加@Transactional?
  • 我正在使用基于 xml 的事务管理,正如我所说,它确实管理它,因为日志显示事务已开始并已提交,一切都很好,但只是保存错误
  • 我将首先从 DAO 方法中删除 try/catch 块。如果有异常,让它传播以回滚事务,并得到错误通知。不要捕获无法正确处理的异常。
  • 我添加它是为了试图找到一些东西,也许我错过了它
  • 你如何确定它不起作用?你打开休眠 SQL 登录了吗?

标签: java mysql spring hibernate transactions


【解决方案1】:

问题可能是由您获取会话的方式引起的。您获得的会话绝不会链接到 Spring 事务管理器,并且您正在使用它执行的操作是在 Spring 事务之外完成的。您应该在您的 spring 上下文中定义一个基于 Spring 的会话工厂,并将其注入您的 DAO,如 the documentation 中所述。

【讨论】:

  • 谢谢你解释了很多,所以问题与休眠无关,我花了 2 天时间寻找错误的错误,它也解释了为什么它可以检索值但不能设置它们。
猜你喜欢
  • 1970-01-01
  • 2011-12-16
  • 1970-01-01
  • 1970-01-01
  • 2015-04-27
  • 2011-03-14
  • 1970-01-01
  • 2012-09-09
  • 2021-09-22
相关资源
最近更新 更多