Normally, a many-to-one column is mapped to a Java object of the mapped class, for example:
Code:
@ManyToOne(targetEntity = Reason.class)
@JoinColumn(name = "reason_code", nullable = false)
private Reason reason;
Is there a way to map this column to a simple integer/long Java property containing only the ID of the mapped row?
(The reason for this 'strange' question is that most of the our Java code only cares about the integer code, and we prefer to manipulate these codes directly rather than always play around with Reason objects. We need the FK just to make sure values are valid)
I could avoid @ManyToOne altogether, and use a simple @Column instead, but then I lose the foreign key definition in the database (since I use hibernate ant tasks to create the tables...)
I tried changing the member to int (and Integer) type in the example above and got a runtime exception (PropertyAccessException) when trying to save() an object.
Any ideas?
Thanks.