Hibernate Proxy getter返回null值

时间:2017-05-05 14:28:56

标签: hibernate lazy-evaluation many-to-one

我正在处理所有@XXXToOne关联被急切获取的现有源(默认),由于急切提取的性能问题,我设法将这些关联设置为LAZY。但是我面临一个奇怪的问题(之前从未得到过这个问题),在@ManyToOne注释字段上调用getter会返回带有enmpty值的空对象。 会话未关闭(spring transaction和jpa log debug on), 这个案例不系统,有些@ManyToOne正确加载。 调查我拿了所有的实体模型项目代码,并把它放在一个新的spring boot jpa项目中,使用相同的hibernate / jpa版本(4.3.1 / 2.1)并创建了一个获取相同实体的服务,以及延迟加载的通过调用getter正常工作,相同的代码,在同一个数据库上,(可能不是相同的libs),setter / getter方法被标记为final,但这适用于独立的spring boot项目,删除修饰符原始实体无法解决问题。 谢谢您的帮助。

       @Entity
       @DiscriminatorColumn(name = "BL_DELETE", discriminatorType = DiscriminatorType.STRING, columnDefinition = "char")
       @DiscriminatorValue("0")
       @DiscriminatorOptions(force = true)
       @SQLDelete(sql = "UPDATE OM_STEP SET BL_DELETE=1, TS_DELETE=SYSDATE WHERE ID_STEP=? and VERSION=?")
       @Table(name = "OM_STEP")
       public class OmStepEntity implements Serializable {
          @Id
          @Column(name = "ID_STEP")
          @SequenceGenerator(name = "STEP_ID_GENERATOR", sequenceName = "SEQ_STEP", allocationSize = 1)
          @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "STEP_ID_GENERATOR")
          private Long stepId;

        // primitives fields 
         .....
        //associations

         @ManyToOne(fetch = FetchType.LAZY)
          @JoinColumn(name = "ID_STEP_MODEL")
          private OrStepModelEntity stepModel;

          @ManyToOne(fetch = FetchType.LAZY)
          @JoinColumn(name = "ID_WORK")
          private OmWorkEntity work;

         public final void setStepModel(final OrStepModelEntity stepModel) {
           this.stepModel= stepModel;
         }
         public final OrStepModelEntity getStepModel() {
           return stepModel;
         }
         public final OmWorkEntity  getWork() {
           return work;
         }
         public final void setWork(final OmWorkEntity work) {
           this.work= work;
         }


        }
     @Entity
     @Table(name = "OR_STEP_MODEL")
     public class OrStepModelEntity implements Serializable {
        @Id
        @Column(name = "ID_STEP_MODEL")
        @SequenceGenerator(name = "ID_STEP_MODEL_GENERATOR", sequenceName = "SEQ_STEP_MODEL", allocationSize = 1)
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator =  "ID_STEP_MODEL_GENERATOR")
        private Long stepModelId;  

        @OneToMany(mappedBy = "stepModel")
        private List < OmStepEntity > stepList;
         public List < OmStepEntity > getStepList() {
           return stepList;
         }
       public void setStepList(final List < OmStepEntity > stepList) {
         this.stepList= stepList;
       }

     }
     @Entity
     @DiscriminatorColumn(name = "BL_DELETE", discriminatorType = DiscriminatorType.STRING, columnDefinition = "char")
     @DiscriminatorValue("0")
     @DiscriminatorOptions(force = true)
     @SQLDelete(sql = "UPDATE OM_WORK SET BL_DELETE=1, TS_DELETE=SYSDATE WHERE ID_WORK=? and VERSION=?")
     @Table(name = "OM_WORK")
     public class OmWorkEntity implements Serializable {
        @Id
        @Column(name = "ID_WORK")
        @SequenceGenerator(name = "ID_WORK_GENERATOR", sequenceName = "SEQ_WORK", allocationSize = 1)
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID_WORK_GENERATOR")
        private Long workId;

        @ManyToOne
        @JoinColumn(name = "ID_WORK_MODEL")
        private OrWorkModelEntity workModel;

        @OneToMany(mappedBy = "work")
        private List < OmStepEntity > stepList;

     public final OrWorkModelEntity getWorkModel() {
        return workModel;
      }

     public final void setWorkModel(final OrWorkModelEntity workModel) {
        this.workModel= workModel;
      }
     public final List < OmStepEntity > getStepList() {
        return stepList;
      }

     public final void setStepList(final List < OmStepEntity > stepList) {
        this.stepList= stepList;
     }
    }

public class StepPersistenceImpl implements IStepPersistence {
 ....
 public final OmStepVo rechercherEtapeParId(final Long stepId) {
    final OmStepEntity result= getEntityManager().find(OmStepEntity.class, stepId);
    return OmStepEntityUtils.toValueObject(result);

  }
}

OmStepEntityUtils.toValueObject从实体创建一个Value Object(一个DTO),在调用getStepModel()时我可以看到为stepModel实体触发的sql查询,但调用getWork()不会强制执行惰性关联初始化并且不会触发sql查询 我在复制实用程序中添加了((HibernateProxy)entite).getHibernateLazyInitializer()。isUninitialized()并返回true。我不明白为什么休眠会忽略这种关联。

0 个答案:

没有答案