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

查看:86
本文介绍了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;
}

persistence.xml

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中)

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中的实体类或基本名称属性,则会抱怨它无效/未找到.因此,这告诉我它正在读取它,但没有覆盖为文件指定的注释.甚至没有使用默认架构.

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天全站免登陆