如何编写用于休眠构建时字节码增强的Ant任务 [英] How to write Ant task for hibernate build-time bytecode enhancement

查看:62
本文介绍了如何编写用于休眠构建时字节码增强的Ant任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图懒惰地获取一对一关联中的子实体与父实体.我知道我必须激活休眠构建时字节码增强功能.

I am trying to lazily fetch a child entity which is in a One to One association with the parent entity. I understood i have to activate hibernate build-time bytecode enhancement.

我的项目建立在Ant之上,我相信现在已经支持Ant任务路易斯·巴雷罗(Luis Barreiro)提供.我的挑战是我暂时不精通写作蚂蚁脚本.我已经尝试了很多运气不佳的事情.子实体尽管每次我尝试总是懒洋洋地取回仅检索父实体.

My project is built on Ant and i believe there is now support for Ant task contributed by Luis Barreiro. My challenge is that i am for now not proficient in writing ant script. I have tried the much i can without luck. Child entity although set to be lazily fetched is always eagerly fetched each time i try to retrieve just the Parent entity.

这是我的实体类

@Entity()
@Table(name = "parent")
public class Parent implements Serializable {

    //Basic properties excluded for brevity

    @OneToOne(mappedBy = "parent", cascade = CascadeType.PERSIST, orphanRemoval = true, fetch = FetchType.LAZY)
    @LazyToOne(LazyToOneOption.NO_PROXY)
    private Child child;

    //getters and setters excluded for brevity

}

@Entity()
@Table(name = "child")
public class Child implements Serializable {

    //Basic properties excluded for brevity

    @OneToOne(fetch = FetchType.LAZY)
    @MapsId
    @JoinColumn(name = "parent_id")
    private Parent parent;

    //getters and setters excluded for brevity

}

然后是我的build.xml

Then my build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="default" basedir=".">
    <description>Builds, tests, and runs the project MyProject.</description>
    <import file="nbproject/build-impl.xml"/>

    <property name="lib.dir" value="./lib" />
    <property name="classes.dir" value="./classes" />

    <path id="lib.class.path">
        <fileset dir="${lib.dir}">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <target name="enhance" depends="compile">
        <taskdef name="enhance" classname="org.hibernate.tool.enhance.EnhancementTask">
            <classpath path="${classes.dir}"/>
            <classpath refid="lib.class.path"/>
        </taskdef>
        <enhance base="${classes.dir}" dir="${classes.dir}" 
             failOnError="true" 
             enableLazyInitialization="true" 
             enableDirtyTracking="true" 
             enableAssociationManagement="true" 
             enableExtendedEnhancement="true" />
    </target>

</project>

请我需要有关如何正确编写ant任务以使其正常工作的帮助.除了蚂蚁任务外,还应指出其他任何观察结果.

Please i need help on how to properly write the ant task to get this to work. Any other observation aside the ant task should as well be pointed out.

注意:因为子实体为空,所以我不能使用 optional = false .

NOTE: I can't use optional = false because Child entity is nullable.

还要注意,我正在使用hibernate-core-5.2.12.Final.jar

Also note that i am using hibernate-core-5.2.12.Final.jar

谢谢

推荐答案

您可以在

As you can see in this integration test in the High-Performance Java Persistence GitHub repository, the client-side does not use @MapsId because it interferes with the bytecode lazy fetching.

此外,正式不支持And任务,仅Maven和Gradle.

Also, the And task is not supported officially, only Maven and Gradle.

如果您使用的是Hibernate 5.2,也许您也应该从Ant迁移.

If you are using Hibernate 5.2, maybe you should migrate from Ant as well.

这篇关于如何编写用于休眠构建时字节码增强的Ant任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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