Can the @uniqueConstraint annotation be specified on subclasses in a table-per-class hierarchy relationship?
For example, I have a Class Device and ManagedDevice is a subclass of Device
Code:
@Entity
@Inheritence(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="ObjectType")
@DiscriminatorValue("Device")
@Table(uniqueConstraints =
@UniqueConstraint(name="primaryIp_U", columnNames={"primaryIpAddress"})
)
public class Device {
@Id private int instanceId;
...
@Column private String primaryIpAddress;
}
@Entity
@DiscriminatorValue("ManagedDevice")
@Table(uniqueConstraints =
@UniqueConstraint(name="managementIp_U", columnNames={"managementIpAddress"})
)
public class ManagedDevice extends Device {
...
@Column private String managementIpAddress;
...
}
The generated DDL looks like this (I am using Postgres dialect)
Code:
create table device (
instanceId int4 not null,
primaryIpAddress varchar(255),
managementIpAddress varchar(255),
...
primary key(instanceId),
unique (primaryIpAddress)
);
The unique constraint for managementIpAddress is not generated. It is generated only when the @UniqueConstraint annotation for managementIpAddress is specified for Device class. Also, the constraint name specified in the annotation is ignored.
I do not like to specify the annotation for the attribute in a subclass in a super class as this creates dependency on the subclass.
Has anybody else run into this issue? I am using Hibernate 3.6.0.
The only workaround for me is to not specify this annotation at all, and instead execute handcrafted DDLs after the tables are created. However, the UniqueConstraint annotation is not useful in this case. For example:
Code:
alter table device add constraint primaryIp_U unique (primaryIpAddress);
alter table device add constraint managementIP_U unique (managementIpAddress);