-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 posts ] 
Author Message
 Post subject: Hibernate not saving
PostPosted: Thu Jan 05, 2006 6:41 am 
Beginner
Beginner

Joined: Sat Oct 18, 2003 8:00 pm
Posts: 22
Hibernate look like not saving the data. I took a look in old similar posts, but I'm using transaction in the right way, it looks...
The most strange is that with oracle it saves alright, and it worked some day with HSQL. ALthough it doesn't save, it returns as it was....
BTW, Im using standalone file based HSQL.

Other question: With hibernate, when I have a field with NULL, it returns an empty string, as with oracle it returns null, how do I make they both work in the same way?

Hibernate version: 3.1

Mapping documents:

<class name="com.yell2.core.enhancer.Component" table="component">
<id name="id" type="long">
<column name="id" not-null="false" />
<generator class="increment" />
</id>
<property name="createdDate" column="created_date" />
<property name="status" column="status" />
<property name="statusMsg" column="status_msg" />
<property name="type" column="type" />
<property name="updatedDate" column="updated_date" />
<property name="value" column="value" type="java.lang.String"
length="5000" />


<set name="children" table="parent_children" cascade="all">
<key column="parent_id" />
<many-to-many class="com.yell2.core.enhancer.Component"
column="child_id" />
</set>

<set name="parents" table="parent_children">
<key column="child_id" />
<many-to-many class="com.yell2.core.enhancer.Component"
column="parent_id" />
</set>

</class>

<session-factory>
<!-- config for HSQL -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:file:.</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="show_sql">true</property>
<property name="transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property>
<property name="hibernate.cache.provider_class">
org.hibernate.cache.HashtableCacheProvider
</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/yell2/core/enhancer/Component.hbm.xml" />

</session-factory>


Code between sessionFactory.openSession() and session.close():

Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Long id = (Long) session.save(comp);
tx.commit();

HibernateUtil.closeSession();


Full stack trace of any exception that occurs:

Name and version of the database you are using:
HSQL Database Engine, version: 1.8.0

The generated SQL (show_sql=true):
Hibernate: select max(id) from component
Hibernate: insert into component (created_date, status, status_msg, type, updated_date, value, id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select component0_.id as id0_0_, component0_.created_date as created2_0_0_, component0_.status as status0_0_, component0_.status_msg as status4_0_0_, component0_.type as type0_0_, component0_.updated_date as updated6_0_0_, component0_.value as value0_0_ from component component0_ where component0_.id=?
test
Hibernate: insert into component (created_date, status, status_msg, type, updated_date, value, id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into component (created_date, status, status_msg, type, updated_date, value, id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select component0_.id as id0_0_, component0_.created_date as created2_0_0_, component0_.status as status0_0_, component0_.status_msg as status4_0_0_, component0_.type as type0_0_, component0_.updated_date as updated6_0_0_, component0_.value as value0_0_ from component component0_ where component0_.id=?
Hibernate: select component0_.id as id0_0_, component0_.created_date as created2_0_0_, component0_.status as status0_0_, component0_.status_msg as status4_0_0_, component0_.type as type0_0_, component0_.updated_date as updated6_0_0_, component0_.value as value0_0_ from component component0_ where component0_.id=?05-Jan-2006 10:12:28 org.apache.commons.configuration.ConfigurationFactory$FileConfigurationFactory createObject
INFO: Trying to load configuration yell_config.xml

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 7:34 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:20 am
Posts: 74
HSQL 1.8 no longer persists data until a SHUTDOWN command is issued (at least in embedded mode, don't know what it does in server mode).
Add
Code:
   <property name="hibernate.connection.shutdown">true</property>
to your hibernate.cfg.xml for HSQL and things should work again.


Top
 Profile  
 
 Post subject: cant insert into binding table
PostPosted: Thu Jan 05, 2006 7:51 am 
Newbie

Joined: Sun Jan 01, 2006 7:57 am
Posts: 8
Hello everybody, I used the this table as my example:

CREATE TABLE autor
(
id int4 NOT NULL DEFAULT nextval('autor_id_seq'::regclass),
meno varchar(255),
CONSTRAINT id_autor PRIMARY KEY (id)
)

CREATE TABLE kniha
(
id int4 NOT NULL DEFAULT nextval('kniha_id_seq'::regclass),
nazov varchar(255),
CONSTRAINT id_kniha PRIMARY KEY (id)
)

CREATE TABLE autor_kniha
(
id_autor int4 NOT NULL,
id_kniha int4 NOT NULL,
rating int4 NOT NULL,
CONSTRAINT fk_autor FOREIGN KEY (id_autor)
REFERENCES autor (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_kniha FOREIGN KEY (id_kniha)
REFERENCES kniha (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
as you can see an extra attribut in binding table (rating)!!

the autor.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping schema="public" package="dom">
<class name="Autor" table="autor" optimistic-lock="none">
<id name="id" type="integer" unsaved-value="null" column="id">
<generator class="native"/>
</id>
<property name="meno" type="string" column="meno" length="255"/>
<set name="knihaMany" table="autor_kniha" cascade="none">
<key column="id_autor" not-null="true"/>
<composite-element class="dom.AutorKniha">
<parent name="idAutor"/>
<property name="rating" column="rating" not-null="true" type="integer"/>
<many-to-one name="idKniha" column="id_kniha" not-null="false" entity-name="dom.Kniha" cascade="none"/>
</composite-element>
</set>
</class>
</hibernate-mapping>

and the kniha.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping schema="public" package="dom">
<class name="Kniha" table="kniha" optimistic-lock="none">
<id name="id" type="integer" unsaved-value="null" column="id">
<generator class="native"/>
</id>
<property name="nazov" type="string" column="nazov" length="255"/>
<set name="autorMany" table="autor_kniha" inverse="true" cascade="none">
<key column="id_kniha"/>
<composite-element class="dom.AutorKniha">
<parent name="idKniha"/>
<property name="rating" column="rating" not-null="true" type="integer"/>
<many-to-one name="idAutor" column="id_autor" entity-name="dom.Autor" cascade="none"/>
</composite-element>
</set>
</class>
</hibernate-mapping>

so this all was generated nothing edited....
for inserting:
test.java:

package utils;

import java.util.*;
import dom.*;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.apache.*;

public class Test {
public static void main(String[] args) {

Autor a = (Autor) HibernateUtil.getSession().load(Autor.class,1);
Kniha k = (Kniha) HibernateUtil.getSession().load(Kniha.class,3);

AutorKniha ak = new AutorKniha();
ak.setIdAutor(a);
ak.setIdKniha(k);
ak.setRating(new Integer(50));

a.getKnihaMany().add(k);
k.getAutorMany().add(a);

HibernateUtil.getSession().beginTransaction().commit();
HibernateUtil.getSession().flush();

}

}
the generated exception:

Exception in thread "main" java.lang.ClassCastException: dom.Kniha$$EnhancerByCGLIB$$5da7bf89
at org.hibernate.type.ComponentType.toLoggableString(ComponentType.java:279)
at org.hibernate.type.CollectionType.toLoggableString(CollectionType.java:147)
at org.hibernate.pretty.Printer.toString(Printer.java:53)
at org.hibernate.pretty.Printer.toString(Printer.java:90)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:91)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at utils.Test.main(Test.java:56)
--------------------------
1. How (which methods should i use) can I insert a row into the binding table(exactly how to bind rating attribute with autor,or kniha in test.java???).(I read the hibernate in action(page 230) and also the documentation for hib. but I never found the construction of methods for this example for inserting a row to the binding table, there are only mappings)

2. Everytime I asked for help in this example everybody told me that I should have another hbm.file and so not 3 classe and 2 hbm files BUT 3classes(4) and 3 hbm files also (the third mapping should be the mapping of the binding table).

3. Why this mapping wont work? Because Im sure that this mapping is good(Hibernate_in_action - page 230) but it just wont insert any rows to the binding table(How should the test.java look like? to insert a row to the binding table).

thx for every help.

_________________
--johnypt--


Top
 Profile  
 
 Post subject: Not yet
PostPosted: Thu Jan 05, 2006 8:19 am 
Beginner
Beginner

Joined: Sat Oct 18, 2003 8:00 pm
Posts: 22
Thanks for the tip jwenting, but it really didn't work. I tested with the HSQL server and it worked fine. I think just that standalone is not very reliable :) any ideas???


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 06, 2006 4:48 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:20 am
Posts: 74
hmm, got that from searching on the problem (I'm encountering it myself and want a cleaner solution than the one I use now).

What works for me with the embedded db is adding the following in the shutdown code for my application:
Code:
session.connection().createStatement().execute("SHUTDOWN");


Not pretty, as it makes the whole thing database dependent (and you wouldn't want that line when you're working with a non-embedded database of course) but it works.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 06, 2006 6:36 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Hello jwenting,

I did not investigate the complete case but you are loading data from the db outside an transaction. Your Autor and your Kniha are detached. So you will probably need a saveOrUpdate statement or you should put the transaction around your code as shown below.

Regards Sebastian

Transaction tx = HibernateUtil.getSession().beginTransaction();
Autor a = (Autor) HibernateUtil.getSession().load(Autor.class,1);
Kniha k = (Kniha) HibernateUtil.getSession().load(Kniha.class,3);

AutorKniha ak = new AutorKniha();
ak.setIdAutor(a);
ak.setIdKniha(k);
ak.setRating(new Integer(50));

a.getKnihaMany().add(k);
k.getAutorMany().add(a);
// not needed: HibernateUtil.getSession().flush();
tx.commit();

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject: Hibernate team is denying the issue
PostPosted: Fri Jan 06, 2006 6:01 pm 
Newbie

Joined: Wed Jan 04, 2006 10:01 pm
Posts: 17
Hi

I posted a very similar problem but really the hibernate team is denying the issue. I posted very detailed, easy to reproduce case, but they are denying it. Hibernate works great in ejb environment and junit environment, other than that there is an issue with JTA environment, at least with 3.1 rc2

Philippe


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 07, 2006 6:17 am 
Newbie

Joined: Sun Jan 01, 2006 7:57 am
Posts: 8
LaLiLuna wrote:
Hello jwenting,

I did not investigate the complete case but you are loading data from the db outside an transaction. Your Autor and your Kniha are detached. So you will probably need a saveOrUpdate statement or you should put the transaction around your code as shown below.

Regards Sebastian

Transaction tx = HibernateUtil.getSession().beginTransaction();
Autor a = (Autor) HibernateUtil.getSession().load(Autor.class,1);
Kniha k = (Kniha) HibernateUtil.getSession().load(Kniha.class,3);

AutorKniha ak = new AutorKniha();
ak.setIdAutor(a);
ak.setIdKniha(k);
ak.setRating(new Integer(50));

a.getKnihaMany().add(k);
k.getAutorMany().add(a);
// not needed: HibernateUtil.getSession().flush();
tx.commit();



thx I will try and reply how it ended.

_________________
--johnypt--


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 07, 2006 9:41 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:20 am
Posts: 74
Having a transaction does nothing without a shutdown statement in the hib config file. I use a transaction on everything (else Hib 3.1 complains anyway) and without that shutdown even the hbm2ddl ANT task doesn't lead to anything happening to the database (and that one I assume is written correctly).

The problem here is clearly either with Hsql or in the way Hibernate accesses Hsql.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 08, 2006 11:27 am 
Newbie

Joined: Sun Jan 01, 2006 7:57 am
Posts: 8
LaLiLuna wrote:
Hello jwenting,

I did not investigate the complete case but you are loading data from the db outside an transaction. Your Autor and your Kniha are detached. So you will probably need a saveOrUpdate statement or you should put the transaction around your code as shown below.

Regards Sebastian

Transaction tx = HibernateUtil.getSession().beginTransaction();
Autor a = (Autor) HibernateUtil.getSession().load(Autor.class,1);
Kniha k = (Kniha) HibernateUtil.getSession().load(Kniha.class,3);

AutorKniha ak = new AutorKniha();
ak.setIdAutor(a);
ak.setIdKniha(k);
ak.setRating(new Integer(50));

a.getKnihaMany().add(k);
k.getAutorMany().add(a);
// not needed: HibernateUtil.getSession().flush();
tx.commit();


Hi LaLiLuna,
the reorder didnt work. Still the same exception.

_________________
--johnypt--


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 4:12 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Hello,
I recreated an example using Hibernate 3.1, PostgreSQL 8 and I do not have any problem. To narrow the problem you may verifiy which combination of Hibernate, database you used.

Sebastian.

Code:
package de.laliluna.component4;

import java.util.HashSet;
import java.util.Set;

public class Author {
   private Integer id;

   private String name;

   private Set ratings = new HashSet();

   public Set getRatings() {
      return ratings;
   }

   public void setRatings(Set ratings) {
      this.ratings = ratings;
   }

   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

}
Code:
package de.laliluna.component4;

public class Rating {
   private Author author;

   private Integer rating;

   private Book book;

   public Author getAuthor() {
      return author;
   }

   public void setAuthor(Author author) {
      this.author = author;
   }

   public Book getBook() {
      return book;
   }

   public void setBook(Book book) {
      this.book = book;
   }

   public Integer getRating() {
      return rating;
   }

   public void setRating(Integer rating) {
      this.rating = rating;
   }

}
Code:
package de.laliluna.component4;

import java.util.HashSet;
import java.util.Set;

public class Book {
   private Integer id;

   private String title;

   private Set ratings = new HashSet();

   public Set getRatings() {
      return ratings;
   }

   public void setRatings(Set ratings) {
      this.ratings = ratings;
   }

   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   public String getTitle() {
      return title;
   }

   public void setTitle(String title) {
      this.title = title;
   }

}
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="de.laliluna.component4">
  <class name="Author" table="author" >
    <id name="id" >
      <generator class="sequence">
        <param name="sequence" >author_id_seq</param>
      </generator>
    </id>
    <property name="name" type="string"></property>
<set name="ratings" table="author_book" cascade="none">
<key column="id_author" not-null="true"/>
<composite-element class="Rating">
<parent name="author"/>
<property name="rating" column="rating" not-null="true" type="integer"/>
<many-to-one name="book" column="id_book" not-null="false" entity-name="de.laliluna.component4.Book" cascade="none"/>
</composite-element>
</set>
  </class>

</hibernate-mapping>
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="de.laliluna.component4">
  <class name="Book" table="book" optimistic-lock="none">
    <id name="id" type="integer" unsaved-value="null" column="id">
      <generator class="sequence">
        <param name="sequence">book_id_seq</param>
      </generator>
    </id>
    <property name="title" type="string" column="title" length="255" />
    <set name="ratings" table="author_book" inverse="true" cascade="none">
      <key column="id_book" />
      <composite-element class="Rating">
        <parent name="book" />
        <property name="rating" column="rating" not-null="true" type="integer" />
        <many-to-one name="author" column="id_author" entity-name="de.laliluna.component4.Author" cascade="none" />
      </composite-element>
    </set>
  </class>
</hibernate-mapping>


Testing with ordinary HibernateSessionFactory (not HibernateUtil)
Code:
Author author = new Author();
      author.setName("Peter");
      Book book = new Book();
      book.setTitle("My life");
      Session session = HibernateSessionFactory.currentSession();
      Transaction tx = session.beginTransaction();
      session.save(author);
      session.save(book);
      tx.commit();
      
      tx = session.beginTransaction();
       session.update(author);
       session.update( book);
       Rating rating = new Rating();
       rating.setAuthor(author);
       rating.setBook(book);
       rating.setRating(5);
       author.getRatings().add(rating);
       book.getRatings().add(rating);
       tx.commit();

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 9:35 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:20 am
Posts: 74
The problem is not in the Hibernate code but is specific to Hsql not persisting data properly.

I'd venture that the same would be observed running direct JDBC through Hsql and then quitting the application without taking precautions as I described.

Bit of a bummer really, especially if the flag in the hibernate config doesn't have the intended effect (in my experience it does work).
Relevant part of the hibernate.xfg.xml:

Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
   <property name="connection.url">jdbc:hsqldb:file:/dev/hib/data/database</property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>
   <property name="hibernate.connection.shutdown">true</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

</hibernate-configuration>



Quick test:
[code]
Configuration config = new Configuration();

SessionFactory sessionFactory = config.configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
Transaction tx = null;
try {
session.beginTransaction();

// insert some data

session.flush();
session.getTransaction().commit();
} catch (Exception ex) {
ex.printStackTrace();
if (session.getTransaction() != null) {
session.getTransaction().rollback();
}
throw ex;
} finally {
session.close();
}
sessionFactory.close();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 10, 2006 9:56 am 
Newbie

Joined: Sun Jan 01, 2006 7:57 am
Posts: 8
LaLiLuna wrote:
Hello,
I recreated an example using Hibernate 3.1, PostgreSQL 8 and I do not have any problem. To narrow the problem you may verifiy which combination of Hibernate, database you used.

Sebastian.



Thx this wok just fine the problem was that I had SETs to each other between book and author and NOT on the rating...
thx once again

_________________
--johnypt--


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.