Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

jpa - orm.xml does not override annotations

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.

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

<?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 (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>

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...