简体   繁体   中英

Hibernate annotation mapping taking more time than hbm.xml mapping

I know its weird but I am having that problem. I have one simple pojo class and using struts+hibernate, I am updating a oracle table through JPA. the only tricky thing I am using is oracle sequence which called before every insert query.

But when I use hbm.xml file it's giving result very fast compare to annotation mapping. I want to switch to annotation, can anyone have any idea what can be the cause.

hbm.xml file is something like that

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>
    <class  name="com.myproject.VersionSequence" 
            table="Version_Sequence"
            dynamic-update="true">

        <meta attribute="sync-DAO">false</meta>

        <id name="id" column="ID" type="java.lang.Long">
            <generator class="sequence">
                <param name="sequence">Oracle_Sequence</param>
            </generator>
        </id>

        <property name="version" column="VERSION" type="java.lang.Long" not-null="true" />
        .
        .
        .
        .

    </class>    
</hibernate-mapping>

Annotation correspond file for above xml file

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

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "Oracle_Sequence")
    @GenericGenerator(name = "Oracle_Sequence",
        strategy = "sequence-identity",
        parameters = { @Parameter(name = "sequence", value = "Oracle_Sequence") })
    @Column(name = "ID")
    private Long id;


    @Column(name = "VERSION")
    private Long version;
    .
    .
    .

    // getter/seeters
}

First we need to understand the working of annotation.

Example -

The Definition of @GenericGenerator

http://grepcode.com/file/repo1.maven.org/maven2/org.hibernate/hibernate-core/4.3.2.Final/org/hibernate/annotations/GenericGenerator.java

The applied annotation is @Retention(RUNTIME) which means annotation should be available for reflection at run time.

Annotation uses reflection and reflection is slow.

Why reflection is slow ?

Every step is validated when you take in reflection.

For example, when you invoke a method, it needs to check whether the target is actually an instance of the declarer of the method, whether you've got the right number of arguments, whether each argument is of the right type, etc.

Annotations are based on reflection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM