0

我正在使用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 方法,它不起作用

public boolean addTemp(TempData entity){ try { getSession().save(entity); 返回真;} 捕捉(异常 e){ e.printStackTrace(); 返回假;} }

这是我的实体

@Component 公共类 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();
    }
4

1 回答 1

1

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

于 2013-05-12T18:05:45.233 回答