Hibernate version: 3.2.6.GA
Name and version of the database you are using: Oracle 10g
Using JPA annotations in Seam with schema generation (hbm-ddl) and a custom naming Strategy.
Code:
public class MyNamingStrategy extends ImprovedNamingStrategy {
@Override
public String foreignKeyColumnName(String propertyName, String propertyEntityName,
String propertyTableName, String referencedColumnName) {
return columnName(propertyName) + "_id";
}
@Override
public String classToTableName(String className) {
return pluralize(super.classToTableName(className));
}
private String pluralize(String name) {
StringBuilder p = new StringBuilder(name);
if (name.endsWith("y")) {
p.deleteCharAt(p.length() - 1);
p.append("ies");
} else {
p.append('s');
}
return p.toString();
}
}
Here is my entity
Code:
@Entity
@Table( uniqueConstraints={@UniqueConstraint(columnNames={"bar_id", "time_period"})})
public class Foo extends BaseEntity {
Fof fof;
private Date timePeriod;
// others
@ManyToOne
@JoinColumn
public Fof getFof() {
return fof;
}
public Date getTimePeriod() {
return timePeriod;
}
//others
}
I get an exception,
Code:
org.hibernate.AnnotationException: Unable to create unique key constraint (bar_id, time_period) on table foos: time_period not found
at org.hibernate.cfg.AnnotationConfiguration.buildUniqueKeyFromColumnNames(AnnotationConfiguration.java:604)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:343)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1121)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1225)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:159)
This is really strange, because, the generated table DOES have time_period column. However if I change it to
@Table( uniqueConstraints={@UniqueConstraint(columnNames={"bar_id", "timePeriod"})})
This is incorrect usage but surprisingly working. How can the mapping mix and match the column names and entity property names in one annotation?
Something is seriously wrong in here.
Note that, the entire mapping is perfectly fine as it's a working code for the past three months. We've had no problems with CRUD operations or queries and the generated DDL.