Ok, I swear I sent this last night, but as it is not in the forum, I'll try again. I'm trying to understand the ramifications of various mappings. I'm trying to figure out how a many-to-many mapping behaves. In particular, I'm curious about creating an assoication between two entities where I know the IDs of the entities. This is similar to the question in the Tips and Tricks FAQ. I tried that and it worked just fine with a many-to-one association.
However, when I tried the same thing with a many-to-many association, mapped as either a set or a list (bag), I find that an add to the collection results in reading the entire collection. I was hoping that lazy=extra would not actually load the collection, at least for a bag. (Perhaps I was hoping for too much). I could do this in SQL simply by doing an insert into the join table with the two IDs. Is there another way to do this?
Thanks
Relevant documents below, the Select SQL is in bold at the end.
Hibernate version: 3.1
Mapping documents:
Person.hbm.xml:
<hibernate-mapping>
<class name="events.Person" table="PERSON" lazy="true">
<id name="id" column="PERSON_ID">
<generator class="native"></generator>
</id>
<property name="firstName" />
<property name="lastName" />
<property name="age"></property>
<bag name="events" table="PERSON_EVENT" lazy="extra">
<key column="PERSON_ID" />
<many-to-many column="EVENT_ID" class="events.Event"/>
</bag>
</class>
</hibernate-mapping>
Event.hbm.xml:
<hibernate-mapping>
<class name="events.Event" table="EVENTS" lazy="true">
<id name="id" column="EVENT_ID">
<generator class="native"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
private void addPersonToEvent(Long personId, Long eventId) {
System.out.println("entering addPersonToEvent");
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
System.out.println("before load of person");
Person aPerson = (Person) session.load(Person.class, personId);
System.out.println("after load of person, before load of event");
Event anEvent = (Event) session.load(Event.class, eventId);
System.out.println("after load of event, before getEvents");
List events = aPerson.getEvents();
System.out.println("getEvents, before adding anEvent");
System.out.println("...");
events.add(anEvent);
System.out.println("...");
System.out.println("after adding anEvent");
session.getTransaction().commit();
System.out.println("leaving addPersonToEvent");
}
Full stack trace of any exception that occurs:
Name and version of the database you are using:
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
14:09:49,562 INFO Environment:479 - Hibernate 3.1
14:09:49,578 INFO Environment:509 - hibernate.properties not found
14:09:49,578 INFO Environment:525 - using CGLIB reflection optimizer
14:09:49,578 INFO Environment:555 - using JDK 1.4 java.sql.Timestamp handling
14:09:49,656 INFO Configuration:1286 - configuring from resource: /hibernate.cfg.xml
14:09:49,656 INFO Configuration:1263 - Configuration resource: /hibernate.cfg.xml
14:09:49,781 INFO Configuration:468 - Reading mappings from resource: events/Event.hbm.xml
14:09:49,921 INFO HbmBinder:265 - Mapping class: events.Event -> EVENTS
14:09:49,953 INFO Configuration:468 - Reading mappings from resource: events/Person.hbm.xml
14:09:50,000 INFO HbmBinder:265 - Mapping class: events.Person -> PERSON
14:09:50,000 INFO HbmBinder:1332 - Mapping collection: events.Person.events -> PERSON_EVENT
14:09:50,015 INFO Configuration:468 - Reading mappings from resource: events/Item.hbm.xml
14:09:50,046 INFO HbmBinder:265 - Mapping class: events.Item -> ITEM
14:09:50,046 INFO Configuration:468 - Reading mappings from resource: events/Bid.hbm.xml
14:09:50,078 INFO HbmBinder:265 - Mapping class: events.Bid -> BID
14:09:50,156 INFO Configuration:1397 - Configured SessionFactory: null
14:09:50,156 INFO Configuration:1022 - processing extends queue
14:09:50,156 INFO Configuration:1026 - processing collection mappings
14:09:50,156 INFO Configuration:1035 - processing association property references
14:09:50,156 INFO Configuration:1057 - processing foreign key constraints
14:09:50,187 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
14:09:50,187 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 1
14:09:50,187 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
14:09:50,187 INFO DriverManagerConnectionProvider:80 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/hyberschema
14:09:50,187 INFO DriverManagerConnectionProvider:86 - connection properties: {user=hyberuser, password=****}
14:09:50,484 INFO SettingsFactory:77 - RDBMS: MySQL, version: 5.0.16-nt
14:09:50,484 INFO SettingsFactory:78 - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.12 ( $Date: 2005-11-17 15:53:48 +0100 (Thu, 17 Nov 2005) $, $Revision$ )
14:09:50,500 INFO Dialect:103 - Using dialect: org.hibernate.dialect.MySQLDialect
14:09:50,515 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
14:09:50,515 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
14:09:50,562 INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled
14:09:50,562 INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled
14:09:50,562 INFO SettingsFactory:136 - JDBC batch size: 15
14:09:50,562 INFO SettingsFactory:139 - JDBC batch updates for versioned data: disabled
14:09:50,562 INFO SettingsFactory:144 - Scrollable result sets: enabled
14:09:50,562 INFO SettingsFactory:152 - JDBC3 getGeneratedKeys(): enabled
14:09:50,562 INFO SettingsFactory:160 - Connection release mode: auto
14:09:50,562 INFO SettingsFactory:184 - Maximum outer join fetch depth: 2
14:09:50,562 INFO SettingsFactory:187 - Default batch fetch size: 1
14:09:50,562 INFO SettingsFactory:191 - Generate SQL with comments: disabled
14:09:50,562 INFO SettingsFactory:195 - Order SQL updates by primary key: disabled
14:09:50,562 INFO SettingsFactory:338 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
14:09:50,562 INFO ASTQueryTranslatorFactory:21 - Using ASTQueryTranslatorFactory
14:09:50,562 INFO SettingsFactory:203 - Query language substitutions: {}
14:09:50,562 INFO SettingsFactory:209 - Second-level cache: enabled
14:09:50,562 INFO SettingsFactory:213 - Query cache: disabled
14:09:50,562 INFO SettingsFactory:325 - Cache provider: org.hibernate.cache.NoCacheProvider
14:09:50,578 INFO SettingsFactory:228 - Optimize cache for minimal puts: disabled
14:09:50,578 INFO SettingsFactory:237 - Structured second-level cache entries: disabled
14:09:50,578 INFO SettingsFactory:257 - Echoing all SQL to stdout
14:09:50,578 INFO SettingsFactory:264 - Statistics: disabled
14:09:50,578 INFO SettingsFactory:268 - Deleted entity synthetic identifier rollback: disabled
14:09:50,578 INFO SettingsFactory:283 - Default entity-mode: pojo
14:09:50,625 INFO SessionFactoryImpl:153 - building session factory
14:09:51,062 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
14:09:51,078 INFO Configuration:1022 - processing extends queue
14:09:51,078 INFO Configuration:1026 - processing collection mappings
14:09:51,078 INFO Configuration:1035 - processing association property references
14:09:51,078 INFO Configuration:1057 - processing foreign key constraints
14:09:51,078 INFO Configuration:1022 - processing extends queue
14:09:51,078 INFO Configuration:1026 - processing collection mappings
14:09:51,078 INFO Configuration:1035 - processing association property references
14:09:51,078 INFO Configuration:1057 - processing foreign key constraints
14:09:51,078 INFO SchemaExport:155 - Running hbm2ddl schema export
14:09:51,078 DEBUG SchemaExport:173 - import file not found: /import.sql
14:09:51,078 INFO SchemaExport:182 - exporting generated schema to database
14:09:51,078 DEBUG SchemaExport:296 - alter table BID drop foreign key FK100DD394289FB
14:09:51,250 DEBUG SchemaExport:296 - alter table PERSON_EVENT drop foreign key FKAD91D9107708282F
14:09:51,453 DEBUG SchemaExport:296 - alter table PERSON_EVENT drop foreign key FKAD91D910F96D1A45
14:09:51,625 DEBUG SchemaExport:296 - drop table if exists BID
14:09:51,656 DEBUG SchemaExport:296 - drop table if exists EVENTS
14:09:51,687 DEBUG SchemaExport:296 - drop table if exists ITEM
14:09:51,703 DEBUG SchemaExport:296 - drop table if exists PERSON
14:09:51,734 DEBUG SchemaExport:296 - drop table if exists PERSON_EVENT
14:09:51,750 DEBUG SchemaExport:296 - create table BID (BID_ID bigint not null auto_increment, item bigint not null, name varchar(255), primary key (BID_ID))
14:09:51,828 DEBUG SchemaExport:296 - create table EVENTS (EVENT_ID bigint not null auto_increment, EVENT_DATE datetime, title varchar(255), primary key (EVENT_ID))
14:09:51,906 DEBUG SchemaExport:296 - create table ITEM (ITEM_ID bigint not null auto_increment, name varchar(255), primary key (ITEM_ID))
14:09:52,000 DEBUG SchemaExport:296 - create table PERSON (PERSON_ID bigint not null auto_increment, firstName varchar(255), lastName varchar(255), age integer, primary key (PERSON_ID))
14:09:52,078 DEBUG SchemaExport:296 - create table PERSON_EVENT (PERSON_ID bigint not null, EVENT_ID bigint not null)
14:09:52,156 DEBUG SchemaExport:296 - alter table BID add index FK100DD394289FB (item), add constraint FK100DD394289FB foreign key (item) references ITEM (ITEM_ID)
14:09:52,328 DEBUG SchemaExport:296 - alter table PERSON_EVENT add index FKAD91D9107708282F (PERSON_ID), add constraint FKAD91D9107708282F foreign key (PERSON_ID) references PERSON (PERSON_ID)
14:09:52,468 DEBUG SchemaExport:296 - alter table PERSON_EVENT add index FKAD91D910F96D1A45 (EVENT_ID), add constraint FKAD91D910F96D1A45 foreign key (EVENT_ID) references EVENTS (EVENT_ID)
14:09:52,609 INFO SchemaExport:202 - schema export complete
14:09:52,625 INFO SessionFactoryImpl:353 - Checking 0 named HQL queries
14:09:52,625 INFO SessionFactoryImpl:373 - Checking 0 named SQL queries
Hibernate: insert into EVENTS (EVENT_DATE, title) values (?, ?)
Hibernate: insert into PERSON (firstName, lastName, age) values (?, ?, ?)
entering addPersonToEvent
before load of person
after load of person, before load of event
after load of event, before getEvents
Hibernate: select person0_.PERSON_ID as PERSON1_1_0_, person0_.firstName as firstName1_0_, person0_.lastName as lastName1_0_, person0_.age as age1_0_ from PERSON person0_ where person0_.PERSON_ID=?
getEvents, before adding anEvent
...
Hibernate: select events0_.PERSON_ID as PERSON1_1_, events0_.EVENT_ID as EVENT2_1_, event1_.EVENT_ID as EVENT1_0_0_, event1_.EVENT_DATE as EVENT2_0_0_, event1_.title as title0_0_ from PERSON_EVENT events0_ left outer join EVENTS event1_ on events0_.EVENT_ID=event1_.EVENT_ID where events0_.PERSON_ID=? ...
after adding anEvent
Hibernate: insert into PERSON_EVENT (PERSON_ID, EVENT_ID) values (?, ?)
leaving addPersonToEvent
14:09:52,984 INFO SessionFactoryImpl:727 - closing
14:09:52,984 INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:mysql://localhost/hyberschema
|