-->
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: On object related (through a list) to two different objects.
PostPosted: Mon Jul 20, 2009 12:17 pm 
Newbie

Joined: Mon Oct 20, 2008 11:37 am
Posts: 7
Hello everybody,
I have a problem with an object related (through a list) to two different objects.
Bellow is a model of the problem:
@Entity
@Table(name = "table1", schema = "public")
public class Class1 implements java.io.Serializable {
....
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "parent")
public List<ObjectComponent> getObjectComponents() {
return this.objectComponents;
}

public void setObjectComponents(List<ObjectComponent> objectComponents) {
this.objectComponents = objectComponents;
}
....
}

@Entity
@Table(name = "table2", schema = "public")
public class Class2 implements java.io.Serializable {
....
}


@Entity
@Table(name="object_components"
,schema="public"
)
public class ObjectComponent implements java.io.Serializable {

private Object1 parent;
private Object2 child;

.....


@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="parent_id")
public Object2 getChild() {
return this.child;
}

public void setChild(Object2 child) {
this.child = child;
}


@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="child_id")
public Object1 getParent() {
return this.parent;
}

public void setParent(Object1 parent) {
this.parent = parent;
}
...
}

Nothing in this model prevents me from including the same instance of Object2 in to two different lists of two different instances of Object1. Indeed my business model requires this.
But here is a problem:
When I delete the first instance of Object1, the one and only instance of Object2 will be deleted because of the "cascade" clause. But it is not what I want. I want it to be deleted only if there is no other objects that it belongs to.

My question is can I achieve the required behavior through correct setting, or I have to remove the "cascade" clause altogether and to deletion manually?

The same problem arises when I try to "evict" the first instance of Object1. The instance of Object2 became evicted and successful reading the second instance of Object1 fails.

Again my question is: how to fight this and other problems, created by above object structure, naturally for Hibernate, instead of using brute force?

Thank you,
Lev Tannen


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.