Java EE – define @ uniqueconstraint in @ mappedsuperclass
I have a requirement where many of my entities need long values and @ manytoone relationships with other entities Using mappedsuperclass, you can easily achieve this requirement in the following ways: @ h_ 419_ 2@@MappedSuperclass public class BaseEntity { @Column(name = "value",nullable = false) private Long value; @JoinColumn(name = "some_entity_id",nullable = false) @ManyToOne(fetch = FetchType.EAGER) private SomeEntity some;
The problem is that long value and entity combinations must be unique Can I define an index in a superclass? If I define an index in the entity's @ table, the code works as expected
The setback is that constraints must be copied along all subclasses, and then all changes need to be copied, making inheritance almost useless (definitely not so elegant)
To summarize, the real question is: can you define composite unique constraints from @ mappedsuperclass? If the answer is no, what would you do?
PS: I know that this is relevant only when the form generation takes effect. It is the whole content of our coding and best practice politics
Solution
I can enter this, or I can give you a link to the solution:)
How to use @UniqueConstraint with single table inheritance (JPA)?
In essence, you cannot override the @ table annotation in an extension class, but you can do so in orm Set unique constraints in the XML file (referenced from persistence. XML)
This means that you can add a unique constraint that will inherit from all classes that extend the base class