I'm using JBoss (4.0.4.GA-Patch1) and EJB3 and have found a small inconvinence. Searching the forums reveals similar problems, but I'm unable to see if they are related or even the same thing.
I have one entity bean ("ConsentDecision") reference another ("Sample") through a many-to-one relation. I want to put a unique constraint on ConsentDecision to make sure they are unique in relation to one Sample (I'm not using the real id-field of ConsentDecision but that is unrelated).
So, I add:
@Entity
Code:
@Table(uniqueConstraints =
{ @UniqueConstraint(columnNames = { "storeId", "sample" }) })
public class ConsentDecision implements java.io.Serializable { ... }
to my bean, and this wont work since the field "sample" is defined as:
Code:
@ManyToOne
public Sample getSample() { ... }
This generates an exception:
Quote:
org.hibernate.MappingException: Unable to find column with logical name: ConsentDecision.sample
However, if alter the definition of the relation slighly:
Code:
@ManyToOne
@JoinColumn(name = "sample_pseudosampleid")
public Sample getSample() { ...}
and then the entity definition:
Code:
@Entity
@Table(uniqueConstraints =
{ @UniqueConstraint(columnNames = { "storeId", "sample_pseudosampleid" }) })
public class ConsentDecision implements java.io.Serializable { ...}
This works fine, generating the following SQL (Postgres):
Quote:
...
CONSTRAINT consentdecision_storeid_key UNIQUE (storeid, sample_pseudosampleid)
...
So, my question is: shouldn't it be possible to derive the name from the association field, just like an ordinary field? The name "sample_pseudosampleid" is what is generated by Hibernate on ordinary deployment and should be available somewhere, somehow.
Anyway, hope this helps someone else.