Spring JPA EntityGraph获取所有懒惰的属性



我已经与Spring和Hibernate合作。现在查看使用JPA 2.2

的弹簧数据JPA(2.0.3(

AgencyTicketType

@Entity
@Table(name = "agency_ticket_type", catalog = "test")
public class AgencyTicketType implements java.io.Serializable {
   private Long id;
   private String name;
   private Agency agency;
   private Set<AgencyTicketCategory> agencyTicketCategories = new HashSet<AgencyTicketCategory>(0);
   @Id
   @GeneratedValue(strategy = IDENTITY)
   @Column(name = "id", unique = true, nullable = false)
   public Long getId() {
     return this.id;
   }
   public void setId(Long id) {
    this.id = id;
   }
   @Column(name = "name", nullable = false, length = 100)
   public String getName() {
    return this.name;
   }
   public void setName(String name) {
    this.name = name;
   }
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "agency_id", nullable = false)
   public Agency getAgency() {
     return this.agency;
   }
   public void setAgency(Agency agency) {
     this.agency = agency;
   }
   @OneToMany(fetch = FetchType.LAZY, mappedBy = "agencyTicketType")
   public Set<AgencyTicketCategory> getAgencyTicketCategories() {
      return this.agencyTicketCategories;
   }
   public void setAgencyTicketCategories(Set<AgencyTicketCategory> agencyTicketCategories) {
      this.agencyTicketCategories = agencyTicketCategories;
   }
}

AgencyTicketCategory

@Entity
@Table(name = "agency_ticket_category", catalog = "waytest")
public class AgencyTicketCategory implements java.io.Serializable {
    private Long id;
    private AgencyTicketType agencyTicketType;
    private String name;
    private BigDecimal price;
    private Set<TripTicket> tripTickets = new HashSet<TripTicket>(0);
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "agency_ticket_type_id", nullable = false)
    public AgencyTicketType getAgencyTicketType() {
        return this.agencyTicketType;
    }
    public void setAgencyTicketType(AgencyTicketType agencyTicketType) {
        this.agencyTicketType = agencyTicketType;
    }
    @Column(name = "name", nullable = false, length = 100)
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Column(name = "price", nullable = false, precision = 8)
    public BigDecimal getPrice() {
        return this.price;
    }
    public void setPrice(BigDecimal price) {
        this.price = price;
    }
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "agencyTicketCategory")
    public Set<TripTicket> getTripTickets() {
        return this.tripTickets;
    }
    public void setTripTickets(Set<TripTicket> tripTickets) {
        this.tripTickets = tripTickets;
    }
}

存储库

public interface TicketTypeRepository extends JpaRepository<AgencyTicketType, Long> {
  @EntityGraph(attributePaths={ "agencyTicketCategories" }, type=EntityGraphType.LOAD)
  @Query("select type from AgencyTicketType type where type.agency.code=?1")
  List<AgencyTicketType> findByAgency(String agencyCode);
}

服务

@Service
public class TicketServiceImpl implements TicketService {       
    @Autowired private TicketTypeRepository ticketType;
    @Transactional(readOnly=true)
    @Override
    public List<AgencyTicketType> findByName(String code) {
        return ticketType.findByAgency(code);
    }    
}

当询问服务时,查询似乎热切地获取所有懒惰的属性 - 代理,代理机构TicketCategories-和它们的所有内部懒惰加载的属性,这会导致 JSON SERILIADIAD错误

需要仅获取这些

AgencyTicketTypes [
                   {
                     id, name,
                     agencyTicketCategories [
                                              {id,name,price},....
                                            ]
                   },.....    
                 ]

我可以用@EntityGraph做到这一点吗?我缺少什么?

指定懒惰加载只是JPA提供商的提示。根据您使用的提供商(Hibernate,Eclipselink等(,它可能完全忽略,并且可能会急切地获取依赖项。

您需要做的是配置如何将类映射到JSON。假设您使用的是杰克逊,则可能需要使用@JsonIgnore@JsonView等注释。您还可以映射只有您需要的字段的课程。

您可以使用Jackson注释@JsonBackReference/@JsonManagedReference。他们解决了对象模型中双向链接的无限递归问题。据我所知,这是您的情况。

有关更多信息

要指出的一件事是,在交易打开时进行调试(触摸集合(也会导致它被加载,即使是实时的。正如@apokralipsa所提到的那样,懒惰的加载只是一个可以完全忽略的提示,永远不要依靠您使用的任何技术

相关内容

  • 没有找到相关文章

最新更新