2013-05-12 74 views
0

我使用休眠3,春季3.0.7,我试图让我的代码实体保存到数据库中,但它只是不会做。Hibernate + Spring 3不节能

我已经试过各种与它,但它好像有东西我做错了。

这里是我的类,它使用DAO方法

公共布尔SAVEDATA(DataHolder架,整型,发现业主){

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它与组件

注释一个bean

这里是我的dao的添加方法thats not working

公共布尔addTemp(TempData的实体){ 尝试{ 的getSession()保存(实体)。 返回true; (例外e){ e。的printStackTrace(); 返回false; }}

,这是我的实体

@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配置的一部分,多数民众赞成有关

<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, [email protected], 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 [[email protected]] 
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 [[email protected]] 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(); 
    } 
+0

您是否在服务层或DAO层中添加了@Transactional? – user962206 2013-05-12 14:06:34

+0

我使用的是基于xml的事务管理,正如我所说的那样,它确实管理它,因为日志说事务已经开始并且已经提交,并且一切都很好,但是保存错误 – engma 2013-05-12 14:10:10

+0

我会从DAO中删除try/catch块方法。如果存在异常,则让它传播以回滚事务,并通知错误。不要捕捉你无法正确处理的异常。 – 2013-05-12 14:31:37

回答

1

的问题可能是由的方式造成你得到一个会议。你得到的会话与Spring事务管理器没有任何关系,你在做什么是在Spring事务之外完成的。您应该在Spring环境中定义一个基于Spring的会话工厂,并将其注入到DAO中,如the documentation中所述。

+0

非常感谢,解决了这个问题与hibernate无关,我花了2天的时间查找错误的bug,这也解释了为什么它可以检索值但不设置它们。 – engma 2013-05-13 00:29:58