• 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

JPA ManyToOne composite foreign key reference for optional target

 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not too hard to find good examples on how to do this with OneToOne, but the same tactic doesn't work for me in Hibernate on a OneToMany.

I have 2 objects, both of which are subclassed from a base object type which has a composite primary key. The key is in the form (string, integer).

the common base class for these entities is using an embeddedId class. The actual columns of interest are:

PK_STRING string;
PK_INT numeric;

And in the child entity:

FK_INT numeric;

A reasonably intuitive mapping would be:


However, that will fail because you can't mix insert/update true and insert/update false on the same joincolumn set in this configuration.

Adding a "@MapsId" annotation to the front of that will get rid of the insert/update conflict, but then I get the error that FK_INT cannot be null, despite having been explicitly advised otherwise.

PK_STRING must never be null, since it's part of the object's primary key.
FK_INT must be nullable, because the parent object of this relationship is optional, and therefore the returned property for the parent object can be null.

Since I can't get in touch with the people on the inside of Hibernate, does anybody here have any ideas?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you thought about creating an Embeddable Id? Whenever I have composite IDs I always put them in their own class and make it Embeddable.

Example



The domain object



And code in another entity that references the EventVisit object.




Hope this is like what you are looking for. Whenever you have a composite id and referencing that object, it becomes very interesting, Embeddable Ids are definitely the way to go with this and makes for many things to work easier. I believe in your case you can put that insertable/updateable = "true" or "false directly in the Embeddable object.

Mark
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess I forgot to mention it. The 2 entity types, as I mentioned, both inherit from the same base class, and yes, it has an EmbeddedID. What's killing me is that since the parent of the entity relationship has a composite key, a simple @JoinColumn doesn't help - I need to join on BOTH columns. In fact, barring some quick inspiration, I'm going to end up with an indirection table just so I can have a simple (nullable) JoinColumn on the child side.

What kills me is that since the parent is optional, in actual SQL terms, the PK_STRING is never allowed to be null, since it's shared between the composite key of the child and the composite foreign key of the parent. But if there is no parent, the FK_INT will be null. The idea, therefore is to get such a construct to be rendered up as a null object reference. Which is made more difficult because Hibernate refuses to let FK_INT be null.

I have a brief, delirious moment of hope when I saw the @NotFound annotation, then I remembered that it's a hack for Hibernate for stuff coming the other way. Or not coming, to be more accurate.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While @NotFound sounds cool, you would also be shocked at the number of queries it makes and runs.

The other option is to create a HibernateListener or HibernateInterceptor where you can write the code for loading from and writing to the database.

Mark
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was looking for a general JPA solution, not a bunch of Hibernate hacks. JPA2 really should be able to handle this on its own.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic