-->
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.  [ 6 posts ] 
Author Message
 Post subject: ManyToMany dropping of related entities
PostPosted: Fri Nov 07, 2008 6:33 am 
Newbie

Joined: Tue Jul 15, 2008 1:39 pm
Posts: 4
I know that this is no new topic, but the threads I found so far do not help me.

I am working with hibernate 3.2.1.GA

I setup two entites, both bound by a many-to-many mapping.

Entity1:
=======

@Entity
@Table(name="t1",
uniqueConstraints={@UniqueConstraint(columnNames={"login", "provider_id"})}
)
public class BuddyLoginEntity implements Serializable {
...
...
@ManyToMany()
@JoinTable(
name="mapping_table",
joinColumns=@JoinColumn(name="buddy_alias_id"),
inverseJoinColumns=@JoinColumn(name="buddy_login_id")
)
@javax.persistence.SequenceGenerator(name="buddyAliasLoginSeq",sequenceName="BUDDY_ALIAS_LOGIN_SEQ", allocationSize=1)
private Set<BuddyAliasEntity> aliases;

...

Entity2:
======

@Entity
@Table(name="t2")
public class BuddyAliasEntity implements Serializable {
...
...
@ManyToMany(fetch=FetchType.EAGER,
mappedBy = "aliases")
private Set<BuddyLoginEntity> buddyLogins;

...

Expected behavior:
================
Removing a BuddyLoginEntity entity should remove as well all entries within the BuddyAliasEntity.

Real behavior:
============
The BuddyLoginEntity entries within BuddyAliasEntity are not removed if I remove a BuddyLoginEntity

Log:
===
all tables are properly created.
Running hibernate in debug mode reveals ALL insert and select statements, but neither update nor delete statements.

More Infos:
==========
The correct adding and removal of BuddyLogin entities is guaranteed by integration tests against a local hsqldb.

I assume that my mapping config is wrong ...

pls help

thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 08, 2008 9:05 pm 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
For one-to-many relationships you can use Cascade with the option to delete orphans (this is a hibernate extension, make sure you use the proper annotation). I am not sure how this works for a many-to-many, give it a try. It seems to me it should be possible since you are just deleting records in the third table.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 12:33 pm 
Newbie

Joined: Tue Jul 15, 2008 1:39 pm
Posts: 4
this is the trace from hibernate:

2008-11-10 17:27:22,470 DEBUG [main] org.hibernate.tool.hbm2ddl.SchemaExport: create table ms_alias_buddy_login_mapping (buddy_login_id bigint not null, buddy_alias_id bigint not null, primary key (buddy_login_id, buddy_alias_id))
2008-11-10 17:27:22,472 DEBUG [main] org.hibernate.tool.hbm2ddl.SchemaExport: create table ms_buddy_alias (id bigint generated by default as identity (start with 1), id_identity_id bigint not null, alias varchar(255) not null, created timestamp, updated timestamp, primary key (id))
2008-11-10 17:27:22,472 DEBUG [main] org.hibernate.tool.hbm2ddl.SchemaExport: create table ms_buddy_login (id bigint generated by default as identity (start with 1), login varchar(255), created timestamp, provider_id bigint not null, primary key (id), unique (login, provider_id))
2008-11-10 17:27:22,475 DEBUG [main] org.hibernate.tool.hbm2ddl.SchemaExport: create table ms_network_login (id bigint generated by default as identity (start with 1), id_identity_id bigint not null, login varchar(255) not null, password longvarbinary not null, priority integer not null, network varchar(255) not null, state varchar(255), removedAt timestamp, created timestamp, primary key (id), unique (id_identity_id, login, network))
2008-11-10 17:27:22,476 DEBUG [main] org.hibernate.tool.hbm2ddl.SchemaExport: create table ms_provider (id bigint not null, name varchar(255), type varchar(255), primary key (id))
2008-11-10 17:27:22,476 DEBUG [main] org.hibernate.tool.hbm2ddl.SchemaExport: alter table ms_alias_buddy_login_mapping add constraint FK6260C677C868F8BB foreign key (buddy_alias_id) references ms_buddy_login
2008-11-10 17:27:22,476 DEBUG [main] org.hibernate.tool.hbm2ddl.SchemaExport: alter table ms_alias_buddy_login_mapping add constraint FK6260C6778A1BFAA9 foreign key (buddy_login_id) references ms_buddy_alias

from my point of view it looks like there is a foreign key from the mapping to its parent table.
Consequently removing an entry from a parent should automatically remove its child entry.

The log informs me about the removal:
2008-11-10 17:27:23,487 DEBUG [main] org.hibernate.engine.StatefulPersistenceContext: initializing non-lazy collections
2008-11-10 17:27:23,487 TRACE [main] org.hibernate.event.def.DefaultLoadEventListener: loading entity: [com.steademy.service.ms.domain.BuddyLoginEntity#3]
2008-11-10 17:27:23,487 TRACE [main] org.hibernate.event.def.DefaultLoadEventListener: attempting to resolve: [com.steademy.service.ms.domain.BuddyLoginEntity#3]
2008-11-10 17:27:23,487 TRACE [main] org.hibernate.event.def.DefaultLoadEventListener: resolved object in session cache: [com.steademy.service.ms.domain.BuddyLoginEntity#3]
2008-11-10 17:27:23,487 TRACE [main] org.hibernate.event.def.DefaultDeleteEventListener: deleting a persistent instance
2008-11-10 17:27:23,487 TRACE [main] org.hibernate.event.def.DefaultDeleteEventListener: deleting [com.steademy.service.ms.domain.BuddyLoginEntity#3]
2008-11-10 17:27:23,487 TRACE [main] org.hibernate.impl.SessionImpl: setting cache mode to: GET
2008-11-10 17:27:23,488 TRACE [main] org.hibernate.impl.SessionImpl: setting cache mode to: NORMAL
2008-11-10 17:27:23,488 TRACE [main] org.hibernate.impl.SessionImpl: setting cache mode to: GET
2008-11-10 17:27:23,488 TRACE [main] org.hibernate.impl.SessionImpl: setting cache mode to: NORMAL

My tests reveal that the entity itself has been properly removed, the enty within the mapping entity (the child) though is still available.

any idea ??


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 3:57 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You've specified a many-to-many relationship so the concept of parent-child doesn't exist in your case. Removing a BuddyLoginEntity will have no effect on the BuddyAliasEntity.

I'm not entirely clear on what you're expecting to happen. Are you saying that when you delete a BuddyLoginEntity it should automatically be removed from the collection of an already loaded BuddyAliasEntity?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2008 2:15 pm 
Newbie

Joined: Tue Jul 15, 2008 1:39 pm
Posts: 4
I was hoping that exactly this would happen.
I thought that the foreign key constraint would lead to a removal of rows within the mapping table and thus to a removal of collection entries in the buddyAliasEntity.buddyLogins ...

does it work this way ???


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2008 2:49 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I'm afraid not. Quote from docs: "hibernate does not affect normal java sematics". An _existing_ collection in java will not be affected by the removal of one of its contained entities from the database. You need to keep the java model in sync with the database model in this case. Of course, next time you reload the collection, in a new session, it won't contain the deleted entity.


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