简体   繁体   English

Hibernate和Spring 3无法保存

[英]Hibernate & Spring 3 not saving

I am using hibernate 3 and spring 3.0.7, I am trying to get my code to save an entity to the database but it just won't do it. 我正在使用hibernate 3和spring 3.0.7,我正在尝试获取代码以将实体保存到数据库中,但是这样做不会。

I have tried everything with it but it seems like there is something I am doing wrong. 我已经尝试了一切,但似乎我做错了什么。

here is my class that uses the dao method 这是我的使用dao方法的课程

public boolean saveData(DataHolder holder,int type,Owners found){ public boolean saveData(DataHolder持有者,int类型,找到所有者){

    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;
    }
}

ofcourse it's a bean annotated with component 当然,它是一个带有组件注释的bean

and here is my dao's add method thats not working 这是我的道的添加方法不起作用

public boolean addTemp(TempData entity){ try { getSession().save(entity); 公共布尔addTemp(TempData实体){试试{getSession()。save(entity); return true; 返回true; } catch (Exception e) { e.printStackTrace(); } catch(Exception e){e.printStackTrace(); return false; 返回false; } } }}

and this is my entity 这是我的实体

@Component public class TempData { @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;
}

and this is my hbm 这是我的hbm

and this is spring config's part thats related 这是与弹簧配置有关的部分

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

thing is it retrieves data perfectly but when it comes to saving data it just don't work, and the same methods in just different projects work perfectly, but here they just don't and the log says nothing about any error or something, i even tried to change the name of the table mapped to a false one but still no errors and completes the transaction, 事情是,它可以完美地检索数据,但是在保存数据时它根本不起作用,并且在不同项目中使用相同的方法也可以很好地工作,但是在这里它们只是不起作用,并且日志中没有任何关于任何错误或其他内容的提示,我甚至试图将映射表的名称更改为一个错误的表,但仍然没有错误并完成了交易,

what am i missing here ? 我在这里想念什么?

EDIT 编辑

This is what my debugger shows, and just that 这就是我的调试器显示的内容,仅此而已

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!

this is how i get my session 这就是我的会议方式

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

The problem is likely caused by the way you get a session. 该问题很可能是由您获取会话的方式引起的。 The session you get is in no way linked to the Spring transaction manager, and what you're doing with it is done outside of the Spring transaction. 您获得的会话绝不会链接到Spring事务管理器,而您正在执行的操作是在Spring事务之外完成的。 You should define a Spring-based session factory in your spring context, and inject it in your DAO, as explained in the documentation . 您应该在spring上下文中定义一个基于Spring的会话工厂,并将其注入到DAO中,如文档所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM