Hibernate - Foreign keys instead of Entities

Currently, Hibernate allows me to load objects defined by *-to-one relationships directly with

entity1.getEntity2()

Is it possible to get the foreign key instead of the object?

The current approach which I see is having addint to my mapping:

@JoinColumn(name="message_key")
@ManyToOne(targetEntity=Message.class,fetch=FetchType.LAZY)
private Message message;  //these lines currently exist

@Column(name="message_key")
private Long message_fk; //the idea is to add those 2 lines

Is there a better approach to get the foreign key, or is this the only one?


Solution 1:

Yes, you can do that. You just need to make it clear for hibernate which one is the mapping that it's supposed to maintain, like so:

@Column(name="message_key", updatable=false, insertable=false)
private Long message_fk;

Solution 2:

If you still want a reference to your entity but don't want to load it from the database just to get the foreign key, your approach is the correct one. Add insertable and updatabale = false to the Column attribute to prevent losing the correct reference to an entity.

@JoinColumn(name = "message_key")
@ManyToOne(targetEntity = Messages.class, fetch = FetchType.LAZY)
private Messages message;

@Column(name = "message_key", insertable = false, updatable = false)
private Long message_fk;

Solution 3:

Actually, it is default Hibernate behavior to only load the foreign key instead of the message object if the FetchType is LAZY. This is why there are proxies to the objects to be loaded when you specify LAZY FetchType.

The foreign key is not visible directly, but it is of course the key of the object at the "one" end of the OneToMany relationship.

However, with a field-based access type (e.g., in your case, where annotations are placed on fields), there is an unresolved hibernate issue: Hibernate loads the whole object behind the proxy from the database. ( http://blog.xebia.com/2009/06/13/jpa-implementation-patterns-field-access-vs-property-access/ )

My concrete suggestion would be (as, for example, the "right" answer did not work in my case):

  • Use the message object directly, as Hibernate will only load it if non-foreign-key data is needed. Do not specify an additional field for the foreign key.
  • Switch the class to use property access, i.e. define getters and setters, and put your annotations from the fields to the getters.

Solution 4:

Long fk = entity1.getEntity2().getId();

This should work. It would only not work if you have composite, primary keys being referenced as foreign keys but your solution wouldn't work either in that case. Considering my solution, even a composite key wouldn't look that ugly.

Long fkField1 = entity1.getEntity2().getCol1();
String fkField2 = entity1.getEntity2().getCol2();

Something like that will work.

EDIT: Thinking about your proposed solution more, it wouldn't work anyway because Hibernate already tries to automatically create a FK field for a Mapped relationship, so defining another @Column would simply try to bind to a second column with the same name.