简体   繁体   English

字节码增强无法正常工作

[英]Bytecode enhancement not working

I am using below link as a reference to implement lazy loading of an image from PostgreSQL DB: URL 我使用以下链接作为参考,以实现从PostgreSQL DB延迟加载图像: URL

In my User entity I declared byte array field: 在我的用户实体中,我声明了字节数组字段:

@Lob
@Basic(fetch = FetchType.LAZY)
private byte[] avatar;

In pom.xml file I included hiberante enhancement plugin: 在pom.xml文件中,我包含了hiberante增强插件:

<plugin>
<groupId>org.hibernate.orm.tooling</groupId>
<artifactId>hibernate-enhance-maven-plugin</artifactId>
<version>${hibernate.version}</version>
<executions>
    <execution>
        <configuration>
            <failOnError>true</failOnError>
            <enableLazyInitialization>true</enableLazyInitialization>
        </configuration>
        <goals>
            <goal>enhance</goal>
        </goals>
    </execution>
</executions>

The issue is that when I fetch User entity from the DB, avatar byte array is also loaded, which I don't want. 问题是,当我从数据库中获取用户实体时,头像字节数组也被加载了,这是我不想要的。

I understand that hibernate-enhance-maven-plugin is supposed to enhance/alter User.class file, this didn't happen. 我了解到hibernate-enhance-maven-plugin应该可以增强/更改User.class文件,但这没有发生。

Am I missing something here? 我在这里想念什么吗?

UPDATE: 更新:

I execute enhance goal: 我执行增强目标:
org.hibernate.orm.tooling:hibernate-enhance-maven-plugin:enhance org.hibernate.orm.tooling:hibernate-enhance-maven-plugin:enhance
In consol I got message: 在consol我收到消息:
"Skipping Hibernate bytecode enhancement plugin execution since no feature is enabled" “由于未启用任何功能,因此跳过了Hibernate字节码增强插件的执行”
I checked plugin jar file hibernate-enhance-maven-plugin-5.3.1.Final.jar and i see bellow code: 我检查了插件jar文件hibernate-enhance-maven-plugin-5.3.1.Final.jar,我看到下面的代码:

@Mojo(name="enhance", defaultPhase=LifecyclePhase.COMPILE, 
requiresDependencyResolution=ResolutionScope.COMPILE_PLUS_RUNTIME)
public class MavenEnhancePlugin
extends AbstractMojo
{
private List<File> sourceSet = new ArrayList();
@Component
private BuildContext buildContext;
@Parameter(property="base", defaultValue="${project.build.outputDirectory}")
private String base;
@Parameter(property="dir", defaultValue="${project.build.outputDirectory}")
private String dir;
@Parameter(property="failOnError", defaultValue="true")
private boolean failOnError = true;
@Parameter(property="enableLazyInitialization", defaultValue="false")
private boolean enableLazyInitialization;
@Parameter(property="enableDirtyTracking", defaultValue="false")
private boolean enableDirtyTracking;
@Parameter(property="enableAssociationManagement", defaultValue="false")
private boolean enableAssociationManagement;
@Parameter(property="enableExtendedEnhancement", defaultValue="false")
private boolean enableExtendedEnhancement;

private boolean shouldApply()
{
    return (this.enableLazyInitialization) || (this.enableDirtyTracking) || 
    (this.enableAssociationManagement) || (this.enableExtendedEnhancement);
}

public void execute()
throws MojoExecutionException, MojoFailureException
{
if (!shouldApply())
{
  getLog().warn("Skipping Hibernate bytecode enhancement plugin execution since no feature is enabled");
  return;
}
.
.
.
}

Looks like shouldApply() method returns false, not sure why since I set properties(enableLazyInitialization) in pom file to be true. 看起来shouldApply()方法返回false,不知道为什么,因为我将pom文件中的properties(enableLazyInitialization)设置为true。

If you don't intend to use the plugin to participate in some phases of the build lifecycle, then you should not use executions. 如果您不打算使用该插件来参与构建生命周期的某些阶段,则不应使用执行。 Try this configuration in your pom.xml: 在pom.xml中尝试以下配置:

        <plugin>
            <groupId>org.hibernate.orm.tooling</groupId>
            <artifactId>hibernate-enhance-maven-plugin</artifactId>
            <version>5.2.13.Final</version>
            <configuration>
                <failOnError>true</failOnError>
                <enableLazyInitialization>true</enableLazyInitialization>
                <enableDirtyTracking>false</enableDirtyTracking>
                <enableAssociationManagement>false</enableAssociationManagement>
            </configuration>
            <goals>
                <goal>enhance</goal>
            </goals>
        </plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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