Java – Hibernate – Foreign keys instead of Entities


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


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

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

private Message message;  //these lines currently exist

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?

Best Solution

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;
Related Question