2

i have a superclass that defines a name column:

@MappedSuperclass
public class SuperClass {
   @Basic
   private String name;
}

there are plenty of concrete @Entity subclasses that extend it, but for one of them i'd like to add a unique constraint on the name column. i cannot just add @Column(unique=true) on the superclass, since thats not correct for all of its subclasses.

how do i redefine the name column in a subclass to be unique? (note: my model maps fields, not getter methods)

radai
  • 23,949
  • 10
  • 71
  • 115

1 Answers1

2

try @AttributeOverride( name="name", column = @Column(name="new_name", unique=true) on your subclass. And then you have to change your column 'name' to 'new_name' in db for the table on which subclass is mapped

birya
  • 340
  • 3
  • 6
  • thanks. i tried a `@Table(uniqueConstraints=...)` approach earlier, which didnt work. `@AttributeOverride` worked, and i didnt have to change the column name: `@AttributeOverride(name = "name", column = @Column(name = "name", unique = true))` – radai Jul 31 '14 at 06:11
  • yes, I also though so but I was not sure. Good for you – birya Jul 31 '14 at 06:28
  • Thanks; I had the *exact* same issue, and I just solved it using your suggestion; +1. – IntelliData Dec 08 '15 at 22:04