有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Hibernate延迟加载不起作用

我上的是ModPm课程

    @Entity
    @Table(name="MOD_PM")
    public class ModPm extends WebPageObject implements Serializable, IDBNamedEntity {

        private static final long serialVersionUID = 1L;

        public final static String Q_GET_DEPENDENT_LIST_FOR_NEW_SCOPE = "ModPm.getDependentForNewScope";
        public final static String Q_GET_DEPENDENT_LIST_WITHOUT_STATUS_FOR_SCOPE = "ModPm.getDependentWithoutStatusForScope";
        public final static String Q_GET_STATUS_FOR_NEW_SCOPE = "ModPm.getStatusForNewScope";
        public final static String Q_GET_WITHOUT_STATUS_FOR_SCOPE = "ModPm.getWithoutStatusForScope";

        @Id
        @SequenceGenerator(name="MOD_PM_ID_GENERATOR", sequenceName="MOD_PM_SEQ", allocationSize=1)
        @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="MOD_PM_ID_GENERATOR")
        @Column(name="ID")
        private long id;

        @Column(name="COMMIT_NUMBER")
        private int commitNumber;


        @Lob()
        @Basic(fetch=FetchType.LAZY)
        @Column(name="LIST_ARCHIVE")
        private byte[] listArchive;

    ....

        @Lob
        @Basic(fetch=FetchType.LAZY)
        @Column(name="TEXT_MASTER")
        private String textMaster;

....

 @ManyToMany
    @JoinTable(
        name="MOD_PM_DEPENDENCE"
        , joinColumns={
            @JoinColumn(name="PRIMARY_PM_ID")
            }
        , inverseJoinColumns={
            @JoinColumn(name="DEPENDENT_PM_ID")
            }
        )
    private List<ModPm> modPms2;

和NamedQuery

@NamedQueries({
    @NamedQuery(name = ModPm.Q_GET_DEPENDENT_LIST_FOR_NEW_SCOPE, 
            query = "SELECT DISTINCT p FROM ModPm p " +
                    "WHERE p.modSystem.id=(SELECT s.modSystem.id FROM ModScopeType s WHERE s.id=?1) " +
                    "AND p.test=false AND p.rejected=false " +
                    "AND p.modPms2 IS NOT EMPTY"),

...
}

当我尝试执行查询时,hibernate递归加载所有数据库。从ModPm类开始(包括所有blob和集合)

我的坚持。xml文件:

  <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.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_2_0.xsd">
        <persistence-unit name="mod-db-jpa" transaction-type="JTA">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>jdbc/security</jta-data-source>
....
            <class>com.ecleasing.db.jpa.entity.ModPm</class>
....
            <exclude-unlisted-classes>true</exclude-unlisted-classes>
            <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
            <validation-mode>CALLBACK</validation-mode>
            <properties>
                <property name="hibernate.transaction.manager_lookup_class" 
                        value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup" />
                <property name="hibernate.transaction.factory_class"
                        value="org.hibernate.transaction.CMTTransactionFactory" />  
                <property name="hibernate.transaction.jta.platform" 
                        value="org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform" />  

                <property name="hibernate.dialect" value="com.ecleasing.db.hibernate.dialect.Oracle10gExtendedDialect" />
                <property name="hibernate.format_sql" value="true" />

                <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" />
                <property name="hibernate.cache.use_second_level_cache" value="true" />
                <property name="hibernate.cache.use_query_cache" value="false" />

                <property name="hibernate.max_fetch_depth" value="0" />

            </properties>
        </persistence-unit>
    </persistence>

我还尝试为每个集合编写带有“fetch=FetchType.LAZY”的注释,但没有帮助


共 (1) 个答案

  1. # 1 楼答案

    Thx至JB Nizet

    将maven任务添加到pom中。xml

    <plugins>
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.3</version>
                    <executions>
                        <execution>
                            <id>Hibernate bytecode optimization</id>
                            <phase>process-classes</phase>
                            <configuration>
                                <tasks>
                                    <taskdef name="instrument"  classname="org.hibernate.tool.instrument.javassist.InstrumentTask">
                                        <classpath>
                                            <path refid="maven.dependency.classpath" />
                                            <path refid="maven.plugin.classpath" />
                                        </classpath>
                                    </taskdef>
                                    <instrument verbose="true">
                                        <fileset dir="${project.build.outputDirectory}">
                                            <include name="**/*.class" />
                                            <exclude name="**/I*_.class,**/WebPageObject.class" />
                                        </fileset>
                                    </instrument>
                                </tasks>
                            </configuration>
    
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>