Hibernate 中Lazy 属性字段需要在类编译后用Instrument对字节处理 才能生效

 1. 一种是利用cglib

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>Instrument domain classes</id>
            <configuration>
                <tasks>
                    <taskdef name="instrument"
                             classname="org.hibernate.tool.instrument.cglib.InstrumentTask">
                        <classpath>
                            <path refid="maven.dependency.classpath"/>
                            <path refid="maven.plugin.classpath"/>
                        </classpath>
                    </taskdef>
                    <instrument verbose="true">
                        <fileset dir="${project.build.outputDirectory}">
                            <include name="**/model/*.class"/>
                        </fileset>
                    </instrument>
                </tasks>
            </configuration>
            <phase>process-classes</phase>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate</artifactId>
            <version>3.2.6.ga</version>
        </dependency>
    </dependencies>
</plugin>

2. 利用javasist

      <!-- For hibernate mapping lazy columns -->
 		  <plugin>
	          <artifactId>maven-antrun-plugin</artifactId>
			  <executions> 
			    <execution> 
			     <phase>process-classes</phase>  
		          <configuration>
		          <tasks>
		          	<echo>Byte instrument: ${project.build.outputDirectory}</echo>
		            <taskdef name="instrument" classname="org.hibernate.tool.instrument.javassist.InstrumentTask">
		              <classpath>
		                 <path refid="maven.dependency.classpath"/>
		              </classpath>
		            </taskdef>
		            
		            <instrument verbose="true">
		              <fileset dir="${project.build.outputDirectory}">
		                <include name="jp/co/yamaha_motor/dashboard/common/entity/*.class" />
		              </fileset>
		            </instrument>
		          </tasks>
		          </configuration>
			      <goals> 
			        <goal>run</goal> 
			      </goals> 
			    </execution> 
			  </executions> 		          
          </plugin>


参考:hibernate byte instrument

你可能感兴趣的:(Hibernate 中Lazy 属性字段需要在类编译后用Instrument对字节处理 才能生效)