Hi,
I'm wondering if it's possible to use a ManyToOne annotation to 'find or create' on the reference table.
Code:
class Foo {
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "reference_table_id")
ReferenceTable referenceTable;
...
}
class ReferenceTable {
@Id
@GeneratedValue
@Column(name = "id")
Integer id;
@Column(name = "description")
String description;
...
}
ReferenceTable can be null. A Foo object is created without any problems, but when I go to add a ReferenceTable to it later, it always creates a new entry. I'd like to look up by 'description' first and use that if it exists, otherwise create a new one.
I tried implementing this in code, but I get a TransientObjectException when I try to do the find. I think it smells that I have to do the find manually, so I probably don't understand how best to use the many-to-one relationship in hibernate.
Any help would be greatly appreciated.