Hibernate不保存和不抛出异常? [英] Hibernate doesn't save and doesn't throw exceptions?

查看:79
本文介绍了Hibernate不保存和不抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经坚持这个已经有几个星期,我没有最坏的想法是什么问题。因为我已经浪费了这么多时间。

I'm stuck with this already for some weeks and I don't have the faintest idea what's going wrong. I'm quite desparate as I've wasted so much time already

我使用下面描述的数据模型(MySQL)。我已经通过反向工程(Eclipse / JBoss Tools)创建了hbm.xml和java类(参见下面的示例)。

I use the data model described below (MySQL). I have created the hbm.xml and java classes by reverse engeneering (Eclipse/JBoss Tools) (see example below).

当我尝试保存tweets,单词或事件时,我可以在日志消息中看到生成的pk值和参数绑定正确,但没有什么曾经写入数据库。 (请参阅帖子最后的日志消息)

When I try to save tweets, words or events I can see in the log messages that the pk values are generated and that the parameters are bound correctly, but there is nothing ever written to the database. (See the log message at the very ending of the post)

但最奇怪的是我保存到event_has_words表中的对象存储完美(使用生成的id从词和事件表)!?!?!最重要的是没有异常被抛出!?

But the strangest thing is that the objects I save to the event_has_words table are stored perfectly (with the generated id from the word and the event table)!?!?! And foremost no exception gets thrown!?!

任何想法?我会疯了!

最好的问候,

John

这里是一个不起作用的映射:

And here is a mapping that doesn't work:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="de.brotkasting.buki.cep.data.hibernate.entities.Event" table="event" catalog="cep1">
        <id name="pkEventsId" type="java.lang.Integer">
            <column name="PK_Events_Id" />
            <generator class="identity" />
        </id>
        <many-to-one name="sourceSystems" class="de.brotkasting.buki.cep.data.hibernate.entities.SourceSystems" fetch="select">
            <column name="SourceSystems_PK_SourceSystems_Id" not-null="true" />
        </many-to-one>
        <many-to-one name="tweets" class="de.brotkasting.buki.cep.data.hibernate.entities.Tweets" fetch="select">
            <column name="Tweets_pk_tweet_id" not-null="true" />
        </many-to-one>
        <property name="systemTimeStamp" type="timestamp">
            <column name="System_Time_Stamp" length="19" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

和相应的类别:

    package de.brotkasting.buki.cep.data.hibernate.entities;

 // Generated 28.04.2009 21:24:54 by Hibernate Tools 3.2.4.GA

 @Entity
 @Table(name = "event", catalog = "cep1")
 public class Event implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = 3530010885697280401L;
private Integer pkEventsId;
private SourceSystems sourceSystems;
private Tweets tweets;
private Date systemTimeStamp;

public Event() {
}

public Event(SourceSystems sourceSystems, Tweets tweets,
        Date systemTimeStamp) {
    this.sourceSystems = sourceSystems;
    this.tweets = tweets;
    this.systemTimeStamp = systemTimeStamp;
}

@Id
@Column(name = "PK_Events_Id", unique = true, nullable = false)
public Integer getPkEventsId() {
    return this.pkEventsId;
}

public void setPkEventsId(Integer pkEventsId) {
    this.pkEventsId = pkEventsId;
}

@JoinColumn(name = "SourceSystems_PK_SourceSystems_Id", nullable = false)
public SourceSystems getSourceSystems() {
    return this.sourceSystems;
}

public void setSourceSystems(SourceSystems sourceSystems) {
    this.sourceSystems = sourceSystems;
}

@JoinColumn(name = "Tweets_pk_tweet_id", nullable = false)
public Tweets getTweets() {
    return this.tweets;
}

public void setTweets(Tweets tweets) {
    this.tweets = tweets;
}

//@Temporal(TemporalType.TIMESTAMP)
@Column(name = "System_Time_Stamp", nullable = false, length = 19)
public Date getSystemTimeStamp() {
    return this.systemTimeStamp;
}

public void setSystemTimeStamp(Date systemTimeStamp) {
    this.systemTimeStamp = systemTimeStamp;
}

   }

p>

and the class to persists

public class TweetPersistence extends HibernateBase {

private static int batchCounter = 0;    

private static void store(Object object) throws HibernateException
{
    try {
        if(session == null)
        {
            session = sessionFactory.openSession();             
        }
        if(!session.isOpen())
        {
            session = sessionFactory.openSession();
        }
        session.save(object);
        LoggingConfigurator.getInstance().info("Objekt:" +object);
        //flush cache every 20 saved entities
        //if(batchCounter%20 == 0)
        //{
            session.flush();
            session.clear();
        //} 
        //increment batchCounter
        batchCounter++;
    } catch (HibernateException e) {
        e.printStackTrace(System.out);
    } 
}

public static void storeTweet(Tweets tweet) {

    try {

        if (tweet != null) {
            store(tweet);
        }
    } catch (HibernateException e) {
        e.printStackTrace(System.out);
    }       
  }
}


- 生成的标识符:component [eventsPkEventsId,wordsPkWordListId,position] {position = 128,wordsPkWordListId = 128,eventsPkEventsId = 56}

- generated identifier: component[eventsPkEventsId,wordsPkWordListId,position]{position=128, wordsPkWordListId=128, eventsPkEventsId=56}

推荐答案

http://docs.jboss.org/hibernate/stable/core/reference/en/html/rel =noreferrer> Hibernate参考手册:

From the Hibernate reference manual:

数据库事务从不是
可选,与
数据库的所有通信都必须在
事务中进行,无论是读取还是
写数据

"Database transactions are never optional, all communication with a database has to occur inside a transaction, no matter if you read or write data"

我建议您在事务中包含所有持久化操作。
例如

I would recommend you to enclose all your persistant operations within a transaction. Eg.

Session session = factory.openSession();
Transaction tx;
try {
    tx = session.beginTransaction();
    session.save(object);
    tx.commit();
}
catch (Exception e) {
    if (tx != null) tx.rollback();
    throw e;
}
finally {
    session.close();
}

这篇关于Hibernate不保存和不抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆