dabuki
dabuki

Reputation: 1031

Hibernate doesn't save and doesn't throw exceptions?

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

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

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)

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

Any ideas? I'm going crazy!

Best regards,

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>

and the according class:

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

   }

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

and in the log I can see that the ids are generated correctly

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

Upvotes: 4

Views: 16369

Answers (3)

bharat salunkhe
bharat salunkhe

Reputation: 1

John, I think you should change your integer to long as well as change precision and scale in your database (which is NUMBER) eq. 10 and 0 make changes in your models bigdecimal or integer to long(all getter and setter) then make changes in your hbm.xml = bigdecimal or integer to long as well as precision and scale. e.g. hbm.xml

<id name="empid" type="long">
            <column name="EMPID" precision="10" scale="0" />
            <generator class="increment" />
        </id>

I was also facing that problem (oracle 10g) but now resolved I hope it helps you! just try once

Upvotes: 0

George Armhold
George Armhold

Reputation: 31064

Check your logging. Most likely your log4j.xml (or equivalent) is sending log messages somewhere you are not expecting, or the level is not set appropriately to see messages from Hibernate. Set the level to DEBUG or INFO; this will at least let you be able to debug whatever is going wrong.

<logger name="org.hibernate">
    <level value="DEBUG"/>
</logger>
<logger name="org.hibernate.SQL">
    <level value="TRACE"/>
</logger>

PS: providing source is great, but you need to boil it down to something concise. No one is going to read through all those details.

Upvotes: 4

Schildmeijer
Schildmeijer

Reputation: 20946

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

Upvotes: 8

Related Questions