java Spring JPA存储库忽略了FetchType。懒惰的
我定义了带有OneToMany
映射的实体类,并通过扩展Spring的JpaRepository
接口创建了Spring JPA存储库接口
我已经在OneToMany
注释中的关联上显式地将fetch设置为FetchType.LAZY
我面临的问题是,当我使用存储库获取特定记录时,Spring会获取关联的实体对象,这些实体对象是使用OneToMany
注释定义的,以及它们随后的关联实体对象,等等,忽略它们的获取类型。在Spring REST控制器中使用网页时,我会收到大量数据。有时,当遇到循环绑定时,它也会导致StackOverflow异常
我在stackoverflow上看到了以下帖子。但答案似乎与目前的情况无关
Spring JPA loads all associated properties despite marking them as FetchType.LAZY in entity class
Update
@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) 个答案