java - Hibernate 和 Spring 3 不保存

标签 java mysql spring hibernate transactions

我正在使用 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); return true; } catch (Exception e) { e.printStackTrace(); return false; } }

这是我的实体

@Component public class 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 配置的相关部分

<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!

这就是我获得 session 的方式

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

最佳答案

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

关于java - Hibernate 和 Spring 3 不保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16508188/

相关文章:

mysql - Spring Boot JPA 一对一映射生成 StackOverflow 错误

php - 警告:mysql_fetch_assoc():提供的参数不是第22行上/home/a5751969/public_html/func/album.func.php中的有效MySQL结果资源。

java - 模板设计模式的 Spring 配置问题

java - 从 root pom 中排除 findbugs 插件

Java图形批量渲染

带有两个 IN 语句的大表上的 MySQL 查询性能悬崖

java - spring boot 更改服务器端口

java - 谷歌分析 : Connect acquisition data with server side reporting

java - 跳过父构造函数调用祖 parent 的

java - 如何使用加载时间编织在一个 jar 中创建 @Aspect 并拦截另一个 jar 中的方法?