• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mappings from different table

 
James Johnsona
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose I currently have a @OneToMany relationship in an entity call it A (that maps to its own table) to another entity B (that maps to its own table)



And suppose the class B has a field or column in its table, lets call it c.

Is it possible to map that field c directly from A? So move it from B class (but not from B's table) into A's class (while leaving it in B's table)
So have something like this in A:



The reason this is being done is because the field c is null in all cases but 1 when returned from B, thus we are getting back 100k rows of B, where a certain field is null in all cases but 1. It would be better to not have it return with the B objects to save memory space. I guess ideally in A we would want to just have a field c (rather than a list of Cs) but that might be too much to ask for. I guess a Set<c> would make the most sense if it was a HashSet (I assume the size of this set would be 1).

But anyways is this possible to do through Hibernate annotations?


 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It wouldn't exactly be third normal form, in database terms, but there's no reason C couldn't have a foreign key to A as well as one to B.
Of course there's no way to ensure (in the database) that the A that C refers to is the same one that the B that C refers to.
If that makes sense.

In other words:

is what you want, but the database would allow:


So you'll have to have to code make sure that doesn't happen.
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I would provide a separate query to access the List<C> and try and get the devs to avoid using the List on B...indeed I'd remove the List from B if that was a possibility.
 
James Johnsona
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Personally, I would provide a separate query to access the List<C> and try and get the devs to avoid using the List on B...indeed I'd remove the List from B if that was a possibility.



For clarity I am going to call what I was calling "c" before...."fieldB"

Hey David,

Ok suppose we did get rid of the List<B> completely from A. Would that work smoothly then? What annotation would you use? I'm thinking @SecondaryTable seems the most helpful. Also would we still have that @OneToMany mapping for List<fieldB> in A? I mean technically that is their relationship still.


My only concern is B had a primary key pair including A's primary key and fieldB. So A's primary key is also B's foreign key.
When entity A is saved, will hibernate be smart enough to know that List<fieldB> is mapped to Table B (which we will tell it by annotation @SecondaryTable I assume) AND that the primary key to Table B is say fieldA (which is on A) and fieldB? Do you understand my concern? Let me write another snippet describing this

Before:








And of course after we just want a List<fieldB> in A and completely forget about B together but still being able to read/write the values from List<fieldB>

We just need to make sure Hibernate KNOWS that the primary key for Table B is fieldA and fieldB. Before we explicitly did this in entity B, not sure if we can do this anymore.
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would keep the List<B> in A.
I would remove the List<C> from B, unless you interact with it directly via B.
I would provide a query to get all the C's relevant to an A.

The second sentence is optional.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic