If you declare a CompositeUserType that maps a type to 2 columns, you have to declare 2 columns in @Columns besides the @Type annotation. Example:
public class BType implements CompositeUserType {
@Override
public String[] getPropertyNames() {
return new String[] { "stockId", "stockCode" };
}
// ...
}
This CompositeUserType maps to 2 separate columns, therefore 2 separate @Column annotations have to be declared:
@Type(type="com.xyz.BType")
@Columns(columns = {
@Column(name="STOCK_ID"),
@Column(name="STOCK_CODE")
})
public B b;
}
However, what if you are using a custom NamingStrategy? You should be able to define the column name there, and not be forced to define it in the annotation. In fact, I have a project where ALL column names are defined in a custom NamingStrategy, except for this case. It's ugly.
If I remove the @Column annotation I get:
org.hibernate.MappingException: property mapping has wrong number of columns.
Please note Hibernate already knows it must use two columns in this case, since this is already specified by the CompositeUserType declaration of BType. So it should be able to find out the column names by using the NamingStrategy.
But it gets worse. Suppose the following structure:
@Entity A {
@Embedded
B b1;
@Embedded
B b2;
}
Since B is used twice, B's column names would have to be defined in A's definition, so that each pair of column names for b1 and b2 can differ. I don't even know if this is possible (using AttributeOverride), but even if it is, it's messy.
To sum up, Custom NamingStrategy should be able to define unique column names for CompositeUserType's with 2 columns or more.
Any.
How could we make the NamingStrategy do it by itself ? It is able to do it for type declared as Embeddable, why can’t it do it for CompositeUserType ?
Â