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); true を返します。} catch (例外 e) { e.printStackTrace(); 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です

これは関連する春の設定の部分です

<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 に答える