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.  [ 7 posts ] 
Author Message
 Post subject: Persisting a hashmap, basic problem
PostPosted: Tue Apr 12, 2005 6:19 am 
Newbie

Joined: Wed Apr 06, 2005 3:42 am
Posts: 5
Location: Berlin
Hello,

as a hibernate-newbie (using hibernate 3.0 final), I'm having major problems storing a HashMap in the database. After searching the forums, and finding multiple posts related to HashMaps, I figured that I can solve this myself, but after browsing the documentatin, the forum, the wiki, and the faq's I'm still not getting this to work. Any help is highly appreciated.

The relevant tables look like this (the idea here is to have several multilanguage titles for each tour):

Code:
table: TOUR_TOUR
id INTEGER,
PRIMARY KEY(id)

table: TOUR_TEXT
tour_id INTEGER,
language VARCHAR(2),
title VARCHAR(2),
PRIMARY KEY(tour_id, language)



The Tour class has a Map named titles, with the appropriate getter/setter:

Code:
public class Tour
    implements Serializable{
    private Map titles = null;
   
    public Tour(){
       this.titles = new HashMap();
    }
   
    public void setTitles(Map titles){
       System.err.println("[Tour.setTitles] titles.size:"+titles.size());
       this.titles = titles;
    }
   
    public Map getTitles(){
       System.err.println("[Tour.getTitles] titles.size:"+titles.size());
       return this.titles;
    }
   
    public void addTitle(Locale l, String title){
       titles.put(l, title);
    }

[...]
}



Here's the hibernate mapping:

Code:
<class name="Tour" table="TOUR_TOUR">
    [...]
    <map name="titles" table="TOUR_TEXT" cascade="all" inverse="true" lazy="false">
        <key column="ID"/>
        <index column="LANGUAGE" type="string"/>
        <element column="TITLE" type="string"/>
    </map>
    [...]
</class>


For testing purposes, I create a Tour with a single title. The tour itself is made persistent & stored in the database. However, the Map containing the title is not stored in the database. Switching all logging to debug, I saw that the mapping itself seems to be correct, and PreparedStatements for TOUR_TEXT are created:

Quote:

261 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: com.kanzow.modules.tour.Tour -> TOUR_TOUR
271 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: id -> ID
291 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: com.kanzow.modules.tour.Tour.titles -> TOUR_TEXT
291 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: titles
[...]
1302 [main] DEBUG org.hibernate.persister.collection.AbstractCollectionPersister - Row insert: insert into TOUR_TEXT (ID, LANGUAGE, TITLE) values (?, ?, ?)
1302 [main] DEBUG org.hibernate.persister.collection.AbstractCollectionPersister - Row update: update TOUR_TEXT set TITLE=? where ID=? and LANGUAGE=?
1302 [main] DEBUG org.hibernate.persister.collection.AbstractCollectionPersister - Row delete: delete from TOUR_TEXT where ID=? and LANGUAGE=?
1312 [main] DEBUG org.hibernate.persister.collection.AbstractCollectionPersister - One-shot delete: delete from TOUR_TEXT where ID=?
[...]
1342 [main] DEBUG org.hibernate.loader.collection.CollectionLoader - Static select for collection com.kanzow.modules.tour.Tour.titles: select titles0_.ID as ID__, titles0_.TITLE as TITLE__, titles0_.LANGUAGE as LANGUAGE__ from TOUR_TEXT titles0_ where titles0_.ID=?



I don't know if this is any help, but here's the logging output when inserting a tour. The tour itself is being made persistent, and several calls from the hibernate-framework to tour.getTitles() are made, but the TOUR_TEST table remains empty...


Quote:
1342 [main] DEBUG org.hibernate.impl.SessionFactoryObjectFactory - initializing class SessionFactoryObjectFactory
1342 [main] DEBUG org.hibernate.impl.SessionFactoryObjectFactory - registered: 4028828f0335bea2010335bea4b80000 (unnamed)
1342 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
1342 [main] DEBUG org.hibernate.impl.SessionFactoryImpl - instantiated session factory
1342 [main] INFO org.hibernate.impl.SessionFactoryImpl - Checking 0 named queries
1372 [main] DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 4560069493227520
1372 [main] DEBUG org.hibernate.transaction.JDBCTransaction - begin
1372 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - opening JDBC connection
1372 [main] DEBUG org.hibernate.connection.DriverManagerConnectionProvider - total checked-out connections: 0
1372 [main] DEBUG org.hibernate.connection.DriverManagerConnectionProvider - using pooled JDBC connection, pool size: 0
1372 [main] DEBUG org.hibernate.transaction.JDBCTransaction - current autocommit status: false
1372 [main] DEBUG org.hibernate.event.def.DefaultSaveOrUpdateEventListener - saving transient instance
1372 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
1372 [main] DEBUG org.hibernate.SQL - select gen_id( hibernate_sequence, 1 ) from RDB$DATABASE
Hibernate: select gen_id( hibernate_sequence, 1 ) from RDB$DATABASE
1372 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - preparing statement
1462 [main] DEBUG org.hibernate.id.SequenceGenerator - Sequence identifier generated: 134
1462 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
1462 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - closing statement
1472 [main] DEBUG org.hibernate.event.def.AbstractSaveEventListener - generated identifier: 134, using strategy: org.hibernate.id.SequenceGenerator
1472 [main] DEBUG org.hibernate.event.def.AbstractSaveEventListener - saving [com.kanzow.modules.tour.Tour#134]
[Tour.getTitles] titles.size:1
1472 [main] DEBUG org.hibernate.engine.Cascades - processing cascade ACTION_SAVE_UPDATE for: com.kanzow.modules.tour.Tour
1472 [main] DEBUG org.hibernate.engine.Cascades - done processing cascade ACTION_SAVE_UPDATE for: com.kanzow.modules.tour.Tour
[Tour.getTitles] titles.size:1
1482 [main] DEBUG org.hibernate.event.def.WrapVisitor - Wrapped collection in role: com.kanzow.modules.tour.Tour.titles
[Tour.setTitles] titles.size:1
1482 [main] DEBUG org.hibernate.event.def.WrapVisitor - Wrapped collection in role: com.kanzow.modules.tour.Tour.grundrisse
1482 [main] DEBUG org.hibernate.event.def.WrapVisitor - Wrapped collection in role: com.kanzow.modules.tour.Tour.panoramas
[Tour.getTitles] titles.size:1
1492 [main] DEBUG org.hibernate.engine.Cascades - processing cascade ACTION_SAVE_UPDATE for: com.kanzow.modules.tour.Tour
1492 [main] DEBUG org.hibernate.engine.Cascades - cascade ACTION_SAVE_UPDATE for collection: com.kanzow.modules.tour.Tour.grundrisse
[Tour.getTitles] titles.size:1
[Tour.getTitles] titles.size:1
1492 [main] DEBUG org.hibernate.engine.Cascades - done cascade ACTION_SAVE_UPDATE for collection: com.kanzow.modules.tour.Tour.grundrisse
1492 [main] DEBUG org.hibernate.engine.Cascades - deleting orphans for collection: com.kanzow.modules.tour.Tour.grundrisse
1492 [main] DEBUG org.hibernate.engine.Cascades - done deleting orphans for collection: com.kanzow.modules.tour.Tour.grundrisse
1492 [main] DEBUG org.hibernate.engine.Cascades - cascade ACTION_SAVE_UPDATE for collection: com.kanzow.modules.tour.Tour.panoramas
1492 [main] DEBUG org.hibernate.engine.Cascades - done cascade ACTION_SAVE_UPDATE for collection: com.kanzow.modules.tour.Tour.panoramas
1492 [main] DEBUG org.hibernate.engine.Cascades - deleting orphans for collection: com.kanzow.modules.tour.Tour.panoramas
1492 [main] DEBUG org.hibernate.engine.Cascades - done deleting orphans for collection: com.kanzow.modules.tour.Tour.panoramas
1492 [main] DEBUG org.hibernate.engine.Cascades - done processing cascade ACTION_SAVE_UPDATE for: com.kanzow.modules.tour.Tour
1492 [main] DEBUG org.hibernate.transaction.JDBCTransaction - commit
1492 [main] DEBUG org.hibernate.impl.SessionImpl - automatically flushing session
1492 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - flushing session
1492 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - processing flush-time cascades
1492 [main] DEBUG org.hibernate.engine.Cascades - processing cascade ACTION_SAVE_UPDATE for: com.kanzow.modules.tour.Tour
1492 [main] DEBUG org.hibernate.engine.Cascades - cascade ACTION_SAVE_UPDATE for collection: com.kanzow.modules.tour.Tour.grundrisse
1492 [main] DEBUG org.hibernate.engine.Cascades - done cascade ACTION_SAVE_UPDATE for collection: com.kanzow.modules.tour.Tour.grundrisse
1502 [main] DEBUG org.hibernate.engine.Cascades - deleting orphans for collection: com.kanzow.modules.tour.Tour.grundrisse
1502 [main] DEBUG org.hibernate.engine.Cascades - done deleting orphans for collection: com.kanzow.modules.tour.Tour.grundrisse
1502 [main] DEBUG org.hibernate.engine.Cascades - cascade ACTION_SAVE_UPDATE for collection: com.kanzow.modules.tour.Tour.panoramas
1502 [main] DEBUG org.hibernate.engine.Cascades - done cascade ACTION_SAVE_UPDATE for collection: com.kanzow.modules.tour.Tour.panoramas
1502 [main] DEBUG org.hibernate.engine.Cascades - deleting orphans for collection: com.kanzow.modules.tour.Tour.panoramas
1502 [main] DEBUG org.hibernate.engine.Cascades - done deleting orphans for collection: com.kanzow.modules.tour.Tour.panoramas
1502 [main] DEBUG org.hibernate.engine.Cascades - done processing cascade ACTION_SAVE_UPDATE for: com.kanzow.modules.tour.Tour
1502 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - dirty checking collections
1502 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - Flushing entities and processing referenced collections
1502 [main] DEBUG org.hibernate.engine.Collections - Collection found: [com.kanzow.modules.tour.Tour.titles#134], was: [<unreferenced>] (initialized)
1502 [main] DEBUG org.hibernate.engine.Collections - Collection found: [com.kanzow.modules.tour.Tour.grundrisse#134], was: [<unreferenced>] (initialized)
1502 [main] DEBUG org.hibernate.engine.Collections - Collection found: [com.kanzow.modules.tour.Tour.panoramas#134], was: [<unreferenced>] (initialized)
1502 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - Processing unreferenced collections
1502 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - Scheduling collection removes/(re)creates/updates
1502 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - Flushed: 1 insertions, 0 updates, 0 deletions to 1 objects
1502 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - Flushed: 3 (re)creations, 0 updates, 0 removals to 3 collections
[Tour.getTitles] titles.size:1
1502 [main] DEBUG org.hibernate.pretty.Printer - listing entities:
1513 [main] DEBUG org.hibernate.pretty.Printer - com.kanzow.modules.tour.Tour{panoramas=[], grundrisse=[], id=134, titles=[TEST TITLE]}
1513 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - executing flush
1513 [main] DEBUG org.hibernate.persister.entity.BasicEntityPersister - Inserting entity: [com.kanzow.modules.tour.Tour#134]
1513 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
1513 [main] DEBUG org.hibernate.SQL - insert into TOUR_TOUR (DIRECTION, ID) values (?, ?)
Hibernate: insert into TOUR_TOUR (DIRECTION, ID) values (?, ?)
1513 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - preparing statement
1513 [main] DEBUG org.hibernate.persister.entity.BasicEntityPersister - Dehydrating entity: [com.kanzow.modules.tour.Tour#134]
1513 [main] DEBUG org.hibernate.type.IntegerType - binding '-1' to parameter: 1
1513 [main] DEBUG org.hibernate.type.IntegerType - binding '134' to parameter: 2
1513 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
1513 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - closing statement
1513 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - post flush
1513 [main] DEBUG org.hibernate.jdbc.JDBCContext - before transaction completion
1513 [main] DEBUG org.hibernate.impl.SessionImpl - before transaction completion
1513 [main] DEBUG org.hibernate.transaction.JDBCTransaction - committed JDBC Connection
1513 [main] DEBUG org.hibernate.jdbc.JDBCContext - after transaction completion
1513 [main] DEBUG org.hibernate.impl.SessionImpl - after transaction completion


So, if somebody could help me out here, I'd highly appreciate that!

Thanks in advance,
sen

_________________
"The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in; we're computer professionals. We cause accidents."
Nathaniel Borenstein (1957 - )


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 3:35 am 
Newbie

Joined: Wed Apr 06, 2005 3:42 am
Posts: 5
Location: Berlin
anybody?

_________________
"The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in; we're computer professionals. We cause accidents."
Nathaniel Borenstein (1957 - )


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 7:38 am 
Newbie

Joined: Wed Apr 06, 2005 3:42 am
Posts: 5
Location: Berlin
Ok, this morning I started debugging the hibernate source, and after tracking it down to the recreate() method in AbstractCollectionPersister,

Code:
public void recreate(PersistentCollection collection, Serializable id, SessionImplementor session)
    throws HibernateException {
   if ( !isInverse && isRowInsertEnabled() ) {
    [...]


i switched the inverse property in my mapping document to false:

Code:
<map name="titles" table="TOUR_TEXT" cascade="all" inverse="false" lazy="false" order-by="language">
    <key column="ID" not-null="true"/>
    <map-key column="LANGUAGE" type="string"/>
    <element column="TITLE" type="string"/>
</map>


After that, I modified my addTitle method to

Code:
public void addTitle(Locale l, String title){
    titles.put(l.toString(), title);
}


Now it works like a charm. I'm a bit disappointed regarding the newbie support in this forum, perhaps it would be a good idea to divide the forum in 'hibernate advanced' and 'hibernate beginner'.

Regards,
sen

_________________
"The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in; we're computer professionals. We cause accidents."
Nathaniel Borenstein (1957 - )


Top
 Profile  
 
 Post subject: It's a bug?
PostPosted: Tue Jul 26, 2005 6:44 pm 
Newbie

Joined: Tue Apr 12, 2005 10:37 am
Posts: 6
Thanks for the workaround! Just spent far too long on this issue.

I'm using 3.0.5, and have the same problem.

You must explicitly state inverse="false", although the reference docs say false is the default.

Couldn't find this in JIRA, but looks like a bug to me.

John


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 26, 2005 6:53 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
You must explicitly state inverse="false", although the reference docs say false is the default.


This is not true. It is also not what the first user observed. He had explicitly set inverse="true" (which does not usually make sense for a simple collection of values) in his first attempt.


Top
 Profile  
 
 Post subject: Sorry
PostPosted: Tue Jul 26, 2005 7:02 pm 
Newbie

Joined: Tue Apr 12, 2005 10:37 am
Posts: 6
Gavin,

Sorry, I take it back. As usual, I'm more stupid than Hibernate.

My problem was that in my testcase I wasn't calling flush().

This is why I shouldn't be coding at 23:59 :-)

Thanks,
John


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 1:11 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
np


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.