jpa – orm.xml不会覆盖注释

我遇到了一个问题,Derby上的JPA默认BLOB大小为64KB.我可以通过设置columnDefinition =“BLOB(128M)”来解决这个问题.但是,在针对另一个RDBMS(如MS SQL)进行集成测试期间,这会失败.我想要做的是使用orm.xml设置columnDefinition.然而,我的尝试是徒劳的,我无法让这个工作.似乎orm.xml中设置的值不会像我期望的那样覆盖注释.

我正在使用JPA 1.0,Toplink Essentials 2.1.60.

我有以下实体注释:

package foo;

@Entity
@Table(name = "Attachment")
public class Attachment implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Version
    @Column(name = "VERSION")
    private Integer version;
    @Column(name = "FILE_NAME", nullable = false)
    private String name;
    @Lob
    @Basic
    @Column(name = "ATTACHED_FILE", nullable = false)
    private byte[] file;
}

persistence.xml中

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"         
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="LAS-OOC" transaction-type="RESOURCE_LOCAL">
        <provider>${jpa.provider}</provider>
        <!-- this isn't required, i know, but I'm trying everything I can think of -->
        <mapping-file>META-INF/orm.xml</mapping-file>
        <class>foo.Attachment</class>
        <properties>
        ...
        </properties>
    </persistence-unit>
</persistence>

orm.xml(位于META-INF)

<?xml version="1.0" encoding="UTF-8" ?>
<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 
      http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" version="1.0">

    <persistence-unit-metadata>
        <persistence-unit-defaults>
            <schema>TEST</schema>
        </persistence-unit-defaults>
    </persistence-unit-metadata>

    <entity class="foo.Attachment" access="FIELD" >
        <attributes>
            <basic name="file">
                <!-- 
                     I'm using maven profiles to set the 'jpa.blob.definition' property.  I even tried changing the column name.
                 --> 
                <column nullable="true" name="ATTACHED_FILE_MODIFIED" column-definition="${jpa.blob.definition}" />
            </basic>
        </attributes>
    </entity>
</entity-mappings>

有趣的是,如果我在orm.xml中更改实体类或基本名称属性,则会抱怨它无效/未找到.所以这告诉我它正在读它但它没有覆盖为文件指定的注释.甚至没有使用默认架构.

orm.xml是不是要覆盖注释?这不应该简单吗?

最佳答案 想出了我的问题.我没有意识到该项目也具有在test / resources中的persistence.xml中定义的相同持久性单元名称.所以当我补充说

<mapping-file>META-INF/orm.xml</mapping-file>

到那个persistence.xml文件,它工作.所以,是的,toplink似乎确实存在一个错误,因为它应该自动选择,但不会.我不敢相信我之前没有看到.叹…

点赞