3

I have 3 classes that are:

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public abstract class Tag {
    @Id
    private String id;
    private String name;
}

@Entity
@Table(uniqueConstraints=
        @UniqueConstraint(columnNames={"name"}))
public class SystemTag extends Tag {

}

@Entity
@Table(uniqueConstraints=
        @UniqueConstraint(columnNames = {"name", "user"}))
public class CustomTag extends Tag{
    @ManyToOne
    private User user;
}

so I want to use unique name for system tags, and unique name-user pair for custom tags (multiple users can create same tags) But I get two warnings as below:

<timestamp> WARN  AnnotationBinder:601 - HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: <domain>.CustomTag
<timestamp> WARN  AnnotationBinder:601 - HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: <domain>.SystemTag

And it allows me to create two system tags with same name and two custom tags with same name for same user.

How can I handle this?

bdogru
  • 812
  • 1
  • 11
  • 19

1 Answers1

6

If you are using a single table that is obviously not going to work.

Switch to using JOINED strategy

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Tag {
    @Id
    private String id;
    private String name;
}

and you will then have a table for CustomTag and table for SystemTag with unique constraints as expected.

Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • in project design team leader decided to use it as single table but i guess i should use it as joined or table per class. thanks anyway :) – bdogru Apr 05 '14 at 09:36
  • well you may be able to use 1 table with discriminator. Depends on your unique key requirements. If you create the key on name/user and forget about the name unique that might work depending on your db. e.g for records 1/alan/null, 2/alan/abc, 3/alan/xyz should be no issues: http://stackoverflow.com/a/767702/1356423 – Alan Hay Apr 05 '14 at 13:17