orm.xml 不会覆盖注释 [英] orm.xml does not override annotations

查看:20
本文介绍了orm.xml 不会覆盖注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I ran into an issue where JPA on Derby defaults the BLOB size to 64KB. I can resolve this by setting the columnDefinition="BLOB(128M)". However, this fails during integration testing against another RDBMS like MS SQL. What I'd like to do is set the columnDefinition using the orm.xml. However, my attempts have been futile and am unable to get this to work. It doesn't appear that the values set in orm.xml are overriding the annotations as I would expect.

我使用的是 JPA 1.0,Toplink Essentials 2.1.60.

I am using JPA 1.0, Toplink Essentials 2.1.60.

我对以下实体进行了注释:

I have the following entity annotated as such:

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;
}

持久性.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)

orm.xml (located in 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 指定的注释.甚至没有使用默认架构.

It IS interesting to note that if I change the entity class or basic name attribute in the orm.xml it complains that it's invalid/not found. So that tells me it is reading it but it's not overriding the annotation specified for file. Even the default schema isn't being used.

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

Isn't the orm.xml suppose to override the annotations? Shouldn't this be simple?

推荐答案

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

Figured out my issue. I didn't realize the project also had the same persistence unit name defined in a persistence.xml in the test/resources. So when I added

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

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

to that persistence.xml file, it worked. So, yes, there does appear to be a bug with toplink as it should pick that up automatically but doesn't. I can't believe I didn't see that earlier. sigh...

这篇关于orm.xml 不会覆盖注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆