File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes JPA ManyToOne composite foreign key reference for optional target Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA ManyToOne composite foreign key reference for optional target" Watch "JPA ManyToOne composite foreign key reference for optional target" New topic
Author

JPA ManyToOne composite foreign key reference for optional target

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15665
    
  15

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?


Customer surveys are for companies who didn't pay proper attention to begin with.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15665
    
  15

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
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

Joined: Jun 25, 2001
Posts: 15665
    
  15

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.
 
 
subject: JPA ManyToOne composite foreign key reference for optional target
 
Similar Threads
JPA composite key mapping
Column index not populated?
Hibernate 3.3.1 - Delete doesn't work
@PrimaryKeyJoinColumn: no insertable and no updatable
Hibernate trying to insert recursive object - I don't want it to