How to use @UniqueConstraint with single table inheritance (JPA)?

♀尐吖头ヾ 提交于 2019-12-02 00:26:41

You cannot override the base class @Table declaration, that's why the sub-class uniqueConstraints directive is ignored.

With JPA you can override annotations with xml declarations. So you need to add an orm.xml file in your class-pat and add the unique constraints there:

<entity-mappings 
  xmlns="http://java.sun.com/xml/ns/persistence/orm"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
  version="2.0">

    <package>...</package>
    <entity class="Document" access="PROPERTY" metadata-complete="false">
        <table name="document">
            <unique-constraint>
                <column-name>first_column</column-name>
                <column-name>second_column</column-name>
            </unique-constraint>
        </table>            
</entity-mappings>

This way you might not even need the MyDocument sub-class, if you only used it to override the DDL schema.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!