-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: @UniqueConstraint annotation in Table-per-class hierarchy
PostPosted: Wed Apr 13, 2011 4:41 pm 
Newbie

Joined: Wed Apr 13, 2011 3:59 pm
Posts: 2
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);


Top
 Profile  
 
 Post subject: Re: @UniqueConstraint annotation in Table-per-class hierarchy
PostPosted: Thu Apr 14, 2011 9:55 am 
Newbie

Joined: Tue Sep 14, 2010 4:29 pm
Posts: 16
Hi balar,
you use the single table with discriminator for inheritance. The @UniqueConstraint does not work on the object level but on the table level. I think that is the reason why it does not work on the subclass. As a replacement you could use the "unique" parameter for the @Column-Annotation like this:
Code:
@Column(unique=true) private String managementIpAddress;
And
Quote:
Also, the constraint name specified in the annotation is ignored.
It is indeed a pitty that constraint name is ignored in the generated DDL. Unfortunately there is no solution for this.
Quote:
However, the UniqueConstraint annotation is not useful in this case.
In your case the unique=true-approach is sufficient. The @UniqueConstraint-Annotation comes into play, if you want to span a unique constraint over more than one columns.

_________________
http://www.winfonet.eu


Top
 Profile  
 
 Post subject: Re: @UniqueConstraint annotation in Table-per-class hierarchy
PostPosted: Fri Apr 15, 2011 9:12 pm 
Newbie

Joined: Wed Apr 13, 2011 3:59 pm
Posts: 2
Thanks for the reply.

your idea of unique="true" will work at the column level as long as the object property refers to a single column, and I will use this in future requirements.

However, the real code that I was dealing with uses an embedded object. The IPAddress property is actually an embeded Object with two columns: ipAddressType and ipAddressString as shown below.

I would like to set the unique attribute on the mangementIpAddress property in the subclass. However, it is not possible to do so. I will have to settle for creating the unique constaint with an "alter table xxx add constraint yyy ..." sql statement that will be run after the schema is created from Hibernate generated DDLs.

Code:
public class Device {
      @Id private int instanceId;
      ...
      @Embedded
      @AttributeOverrides({
          @AttributeOverride(name="ipAddressString", column=@Column(name="primaryIpAddressString"))
          @AttributeOverride(name="ipAddressType", column=@Column(name="primaryIpAddressType"))
        })
        private IPAddress primaryIpAddress;
}

@Entity
@DiscriminatorValue("ManagedDevice")
@Table(uniqueConstraints =
                @UniqueConstraint(name="managementIp_U", columnNames={"managementIpAddress"})
)
public class ManagedDevice extends Device {
      ...
      @Embedded
      @AttributeOverrides({
           @AttributeOverride(name="ipAddressString", column=@Column(name="managementIpAddressString"))
           @AttributeOverride(name="ipAddressType", column=@Column(name="managementIpAddressType"))
       })
      @Column
      private IPAddress managementIpAddress;
      ...
}

@Embeddable
public class IPAddress {
     @Column
     private String ipAddressString;
     @Column
     private String ipAddressType;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.