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.  [ 1 post ] 
Author Message
 Post subject: Many to Many with List problem
PostPosted: Tue Oct 21, 2008 9:10 pm 
Newbie

Joined: Tue Oct 21, 2008 9:01 pm
Posts: 1
I was following the Person/Event example in the documentation. One thing I noticed was that when adding a new event to a person, it fetched all of the existing events linked to the person. After doing some searching, I found that this is expected behavior with Sets. So I switched it to a List but its still fetching the existing data. This seems inefficient, so I was wondering how I can have it not fetch all the existing data when I want to add a new event.

Thank you in advance.

Hibernate version: 3.3.1.

Mapping documents:
<hibernate-mapping>
<class name="javaapplication2.Person" table="person">
<id name="id">
<generator class="identity"/>
</id>
<property name="age"/>
<property name="firstname"/>
<property name="lastname"/>
<list name="events" table="personevents" lazy="true">
<key column="personid"/>
<index column="id" />
<many-to-many class="javaapplication2.Event" column="eventid" />

</list>
</class>

<class name="javaapplication2.Event" table="events">
<id name="id">
<generator class="native"/>
</id>
<property name="title" />
<property name="date" />
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Person person = (Person) session.load(Person.class, 1);
Event event = (Event)session.load(Event.class, 1);
person.getEvents().add(event);

Name and version of the database you are using: MySQL 5
CREATE TABLE `events` (
`Id` int(10) unsigned NOT NULL auto_increment,
`Title` varchar(45) NOT NULL,
`Date` datetime NOT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

CREATE TABLE `person` (
`Id` int(10) unsigned NOT NULL auto_increment,
`FirstName` varchar(45) NOT NULL,
`LastName` varchar(45) NOT NULL,
`Age` int(10) unsigned NOT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

CREATE TABLE `personevents` (
`Id` int(10) unsigned NOT NULL auto_increment,
`PersonId` int(10) unsigned NOT NULL,
`EventId` int(10) unsigned NOT NULL,
PRIMARY KEY (`Id`),
KEY `FK_personevents_person` (`PersonId`),
KEY `FK_personevents_events` (`EventId`),
CONSTRAINT `FK_personevents_person` FOREIGN KEY (`PersonId`) REFERENCES `person` (`Id`),
CONSTRAINT `FK_personevents_events` FOREIGN KEY (`EventId`) REFERENCES `events` (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1;

The generated SQL (show_sql=true):
Hibernate: select person0_.id as id1_0_, person0_.age as age1_0_, person0_.firstname as firstname1_0_, person0_.lastname as lastname1_0_ from person person0_ where person0_.id=?
Hibernate: select events0_.personid as personid1_, events0_.eventid as eventid1_, events0_.id as id1_, event1_.id as id0_0_, event1_.title as title0_0_, event1_.date as date0_0_ from personevents events0_ left outer join events event1_ on events0_.eventid=event1_.id where events0_.personid=?
Hibernate: insert into personevents (personid, id, eventid) values (?, ?, ?)


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

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.