-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to prevent Hibernate from nullifying relationship column
PostPosted: Sun May 16, 2010 5:15 pm 
Newbie

Joined: Mon Dec 14, 2009 5:27 pm
Posts: 7
I have two entities, A and B. I need to easily retrieve entities A, joined with entities B on the condition of equal values of some column (some column from A equal to some column in B). Those columns are not primary or foreign keys, they contain same business data. I need to have access from each instance of A to the collection of B's with the same value of this column. So I model it like this:
Code:
class A {
  @OneToMany
  @JoinColumn(name="column_in_B", referencedColumnName="column_in_A")
  Collection<B> bs;
}

This way, I can run queries like "select A join fetch a.bs b where b...."
(Actually, the real relationship here is many-to-many. But when I use @ManyToMany, Hibernate forces me to use join table, which doesnt exist here. So I have to use @OneToMany as workaround).
So far so good. The main problem is: whenever I delete an instance of A, hibernate calls "Update B set column_in_B = null", becuase it thinks the column_in_B is foreign key pointing at primary key in A (and because row in A is deleted, it tries to clean the foreign key in B). BUT the column_in_B IS NOT a foreign key, and can't be modified, because it causes data lost (and this column is NOT NULL anyway in my case, causing data integerity exception to be thrown).

Plese help me with this. How to model such relationships with Hibernate? (I would call it "virtual relationships", or "secondary relationships" or so: as they are not based on foreign keys, they are just some shortcuts which allows for retrieving related objects and quering for them with HQL)


Top
 Profile  
 
 Post subject: Re: How to prevent Hibernate from nullifying relationship column
PostPosted: Mon May 17, 2010 8:37 am 
Regular
Regular

Joined: Thu Dec 10, 2009 10:53 am
Posts: 50
What hibernate version are you using?

How does the mapping at class B look like?

You might be better of doing this join manually with criteria/hql.


Top
 Profile  
 
 Post subject: Re: How to prevent Hibernate from nullifying relationship column
PostPosted: Mon May 17, 2010 9:24 am 
Newbie

Joined: Mon Dec 14, 2009 5:27 pm
Posts: 7
Quote:
What hibernate version are you using?

3.5
Quote:
How does the mapping at class B look like?

There is no mapping of this relation on B side - this is unidirectional relation (it can't be bidirectional, because A is actually @MappedSuperclass, so it can be a target of relationship). Class B is just simple, plain @Entity with some simple-type attributes.
Quote:
You might be better of doing this join manually with criteria/hql.

What do you mean by this? Remove the relationship field "bs" from class A? But how then I get access from each instance of A to the joined instances of B, if there is no field "bs" in A?


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