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?
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;