Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 映射的超类中的Eager fetch属性_Spring_Hibernate_Hql - Fatal编程技术网

Spring 映射的超类中的Eager fetch属性

Spring 映射的超类中的Eager fetch属性,spring,hibernate,hql,Spring,Hibernate,Hql,我们映射的超类具有createdBy列,该列定义为延迟加载 @MappedSuperclass @EntityListeners(AuditingEntityListener.class) @XmlAccessorType(XmlAccessType.FIELD) public abstract class AbstractAuditingEntity implements Serializable { @CreatedBy @ManyToOne(fetch = FetchTyp

我们映射的超类具有createdBy列,该列定义为延迟加载

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class AbstractAuditingEntity implements Serializable {
    @CreatedBy
    @ManyToOne(fetch = FetchType.LAZY)
    @XmlTransient
    @JoinColumn(name = "created_by", updatable = false, columnDefinition = "bigint")
    protected User createdBy;

   public User getCreatedBy() {
      return createdBy;
   }

   public void setCreatedBy(User createdBy) {
      this.createdBy = createdBy;
   }
我需要在继承上述类的子类中加载这个属性

@Override
@XmlElement(name = "createdBy")
@JsonProperty("createdBy")
public User getCreatedBy() {
    return super.getCreatedBy();
}
我该怎么做

我尝试了以下方法(使用NamedEntityGraph和HQL),但是,它们都没有从定义为lazy的MappedSuperClass返回createdBy

//defined at the top of Model
@NamedEntityGraphs({
        // eagerly fetches created by and program names when used
        @NamedEntityGraph(
                name = "graphWithCreatedBy",
                attributeNodes = {
                        @NamedAttributeNode("createdBy")
                }
        )
})

//Repository method
@EntityGraph(value = "Program.createdBy", type = EntityGraph.EntityGraphType.FETCH) //tried both LOAD and FETCH
    Program findOne(Specification<Program> specification);
这两种方法都返回程序,但不返回createdBy用户
我做错了什么

好问题。是否可以添加一个在实例化时执行super.getCreatedBy()的默认构造函数,从而强制加载字段?有点长。我对这个问题的第一反应是,我不认为你能做到这一点,也不太清楚为什么你想在超级类中延迟加载?是因为您的扩展不应该急于加载性能吗?这也许值得一试?有兴趣了解您是如何获得/解决此问题的。@DaisyDay现有代码和每个表都扩展了该超类。所以,我不想改变现有的功能。此外,由于每个类都扩展了超类,所以即使用户也扩展了该超类,并且在获取程序的createdBy时,在获取用户的createdBy时可能会导致无限循环。似乎有点限制性的设计。当您说您需要急切地加载它时,您的意思是您希望子类的实例化能够正确地急切地加载它,对吗?您可以尝试“隐藏”字段并在子类中使用EAGER?抓救命稻草!是的,我想在加载子类时急切地加载该属性。你是怎么做到的?你能用eg解释一下吗?隐藏在子类中只是重新命名变量。老实说,我不知道这是否有效。我不知道注释是否能按预期运行等。请参阅字段隐藏:
//Repository Implementation
private static final String PROGRAM_USER_QUERY = "SELECT " +
            " sp FROM Program sp " +
            " LEFT JOIN FETCH sp.createdBy where sp.id = :id";

Query query = entityManager.createQuery(PROGRAM_USER_QUERY ).
                setParameter("id", id);

query.getSingleResult();