2013-05-12 77 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