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