Spring JPA Repository忽略FetchType.LAZY

时间:2015-06-18 09:05:14

标签: java spring hibernate spring-mvc jpa

我的实体类定义了OneToMany映射,并通过扩展spring的JpaRepository接口创建了Spring JPA存储库接口。

我已在FetchType.LAZY注释中明确地将关联设置为OneToMany

我面临的问题是,当我使用存储库获取特定记录时,Spring将获取使用OneToMany注释及其后续关联实体对象定义的关联实体对象,依此类推,忽略其获取在Spring REST Controller中使用它时,我正在向网页接收大量数据。几次,它遇到循环绑定时也会导致StackOverflow异常。

我在stackoverflow.com上看到以下帖子,但答案似乎与目前的情况无关。

Spring JPA loads all associated properties despite marking them as FetchType.LAZY in entity class

  

更新

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AbstractEntity implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -2700585394392789902L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @CreatedDate
    private Date createdDate = new Date();

    @LastModifiedDate
    private Date lastModifiedDate = new Date();

    @CreatedBy
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn
    private User createdBy;

    @LastModifiedBy
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn
    private User lastModifiedBy;

    @javax.persistence.Version
    private long version;

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public Date getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(Date lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

    @JsonIgnore
    public User getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(User createdBy) {
        this.createdBy = createdBy;
    }

    public void setLastModifiedBy(User lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    public long getVersion() {
        return version;
    }

    public void setVersion(long version) {
        this.version = version;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

}



@Entity
@Table(name = "currencies")
public class Currency extends AbstractMasterEntity {

    private static final long serialVersionUID = 1L;

    @Override
    public String toString() {
        return "Currency [code=" + code + ", name=" + name + "]";
    }
}

0 个答案:

没有答案