• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

non-standard join in ManyToOne

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

In a ManyToOne RelationShip, for example Employee(many) -> Company(One), is it always required that join ia allowed only based on a foreign key?

How can I make a join if there is no foreign key?


Thank you in advance
Binu K Idicula
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to join entities on properties that are not mapped as associations, you can do so using a JPQL query, and a theta-style join. In your example:

where Employee.fieldA and Company.fieldB are persistent properties of the entities. Of course, they must be of types that can be directly compared. It also has the limitation that is is inherently like an inner join, so that if null values for the properties used in the where clause exist, the entities will not be returned.

I am not sure if this is what you asked about, though... If not, please describe your problem more clearly.

Cheers.
[ November 28, 2008: Message edited by: Raf Szczypiorski ]
 
Binu K Idicula
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raf,

Thanks for the Quick Reply. Here is the situation in much detail

Class Person { int personId, String name, String street}
Class Address{ int addressId, String street, String location}

Now,

is it possible for me to define Address directly in Person using a @OneToOne annotation.

OR

is it possible for me to define Set<Person> in Address using a @ManyToOne annotation.

In Schema, street is NOT a foreign key.

regards
Binu K Idicula
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can (at least in Hibernate used as a JPA provider). Follow this code:


The tables are:
ADDRESS: ID, STREET, LOCATION
PERSON: ID, NAME, STREET - where street is not mapped as a persistent property, but rather as a key for the OneToOne association. Remeber that for OneToOne the STREET in ADDRESS table must be unique, Hibernate throws exceptions as it tries to join with address, but there are two (or more) addressses for a OneToOne relation.

The code to persist is standard:

You can do the same for OneToMany, in which case the ADDRESS.STREET doesn't have to be unique.

Hope this helps.
[ November 29, 2008: Message edited by: Raf Szczypiorski ]
 
Binu K Idicula
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Raf,
Thanks a lot for the detail with which you answered my doubt. It is extremely useful. I doubt why these examples are missing out from JPA examples and SUN Tutorials.
regards
Binu K Idicula
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic