• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"Logical join" in hibernate

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

I have 2 entities that dosnt have any physical (FK) from each other.
I want that Entity AAA will have collection of entity BBB based on two common columns (which are not PK or key of the entities).

Is there a way to map this?

Thank you
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My initial thought is if this is unreleated data why are you trying to relate it? Should you not just change the data model? Or is this a legacy data model?
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The data is related.
But, i dont want to have physical relation between the two.

For example:

Say that you have Custoemer and you have additional entity - Book

I want that customer will have a "logical collection" of books.
By logical collection i mean something like "view".The tables will not have any comuns for this relation.

The collection of books for the customer will be loaded by a join on columns "author, publisher "in customer and in Book.

Just load the data by join on the two columns.

Thank you
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The data is related.
But, i dont want to have physical relation between the two.


I don't want to sound like a pedant here, but no it is not, at least not in relational data modelling terms. Without the constraint your book can be related to a customer who doesn't exist. Why don't you want the constraint to exist? What is it you are trying to achieve by doing this?


The collection of books for the customer will be loaded by a join on columns "author, publisher "in customer and in Book.


So do I understand that the properties author and publisher have been denormalized on to both tables? e.g

 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your query is exactly the query that i do.

i want that i will have a mapping that will hide this query.

I can run the query and fill the collection by myself.
Or - i can find a way to tell hibernate to fill the collection by execute this query.

I know a way to do this - by use in entityresultset mapping in the entity header,but this approach is very complicated.

I dont have any constraints heere. just want that hibernate will load the collection of book by join query.

A simple way to achieve this is by JoinColumns and defined the join columns ,but ... i didnt sucess to do this in this way because hibernate want that the join column will point to the key. I think that once in the past i figure out how to overcome on this limitiation.

Thank you
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


A simple way to achieve this is by JoinColumns and defined the join columns ,but ... i didnt sucess to do this in this way because hibernate want that the join column will point to the key. I think that once in the past i figure out how to overcome on this limitiation.


This is a perfectly reasonable thing for Hibernate to expect, its not a limitation, its a deliberate constraint (see that word again ). Your join uses author and publisher as a composite key, Hibernate is correctly stopping you because its not a composite key.If you want to generate an adhoc view like this you can do it with a query in HQL. But it sounds like you need to fix the data model if you can otherwise you are just introducing problems. Your denormalized fields will end up diverging and you are going to end up with orphaned books and customers related to non-existant books.
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you will not have any constarints problem.

Customers have thier own life and Books also have their own life.

When you load customers you execute a join agains Books and load exists book.
There is no save or merge. only read.

If you delete a customer you dont need to delete a book.

EntityResultSet - solve this problem.

and i did something like this before a long time.

You also can have a readonly relation.

This make sence to me to have the ability to join additional info on join criteria which is not the PK.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Customers have thier own life and Books also have their own life


If you have denormalized data on to the customer table this is no longer true. Books have their own life, except when their data has to be kept in sync with data on customer records (delete or update a book and you have to update costomer records). I still can't see what you gain by modelling this the way you have. If it is a many to many relationship you are modelling, whould a ternery join table not be a better approach?

If you can't do anything about the data model, and this is a read only view only view, I'd suggest you create a named query for it rather than mapping the pseudo-association.
 
reply
    Bookmark Topic Watch Topic
  • New Topic