【问题标题】:orm.xml does not override annotationsorm.xml 不会覆盖注释
【发布时间】:2012-02-03 18:22:51
【问题描述】:

我遇到了一个问题,即 Derby 上的 JPA 将 BLOB 大小默认为 64KB。我可以通过设置 columnDefinition="BLOB(128M)" 来解决这个问题。但是,在针对 MS SQL 等其他 RDBMS 的集成测试期间,这会失败。我想做的是使用 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 中的实体类或基本名称属性,它会抱怨它无效/未找到。所以这告诉我它正在阅读它,但它没有覆盖为 file 指定的注释。甚至没有使用默认架构。

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

【问题讨论】:

  • 很确定您可以使用 DataNucleus JPA 覆盖此类信息,因为我们积极鼓励人们这样做。也许它是 TopLink 中的一个错误?

标签: jpa orm


【解决方案1】:

解决了我的问题。我没有意识到该项目在 test/resources 中的 persistence.xml 中也定义了相同的持久性单元名称。所以当我添加

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

到该 persistence.xml 文件,它起作用了。所以,是的,toplink 似乎确实存在一个错误,因为它应该自动拾取它,但没有。我不敢相信我之前没有看到。叹息……

【讨论】:

    猜你喜欢
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    相关资源
    最近更新 更多