相关疑难解决方法(0)

Spring Data JPA和NamedEntityGraphs

目前我正在努力只能获取我需要的数据.findAll()方法需要根据其被调用的位置获取数据.我不想最终为每个实体图编写不同的方法.此外,我会避免调用实体管理员并自己形成(重复)查询.基本上我想在findAll方法中使用构建,但是使用我喜欢的实体图.任何机会?

@Entity
@Table(name="complaints")
@NamedEntityGraphs({
    @NamedEntityGraph(name="allJoinsButMessages", attributeNodes = {
            @NamedAttributeNode("customer"),
            @NamedAttributeNode("handling_employee"),
            @NamedAttributeNode("genre")
    }),
    @NamedEntityGraph(name="allJoins", attributeNodes = {
            @NamedAttributeNode("customer"),
            @NamedAttributeNode("handling_employee"),
            @NamedAttributeNode("genre"),
            @NamedAttributeNode("complaintMessages")
    }),
    @NamedEntityGraph(name="noJoins", attributeNodes = {

    })
})
public class Complaint implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private long id;

    private Timestamp date;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "customer")
    private User customer;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "handling_employee")
    private User handling_employee;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="genre")
    private Genre genre;

    private boolean closed;

    @OneToMany(mappedBy = "complaint", fetch = …
Run Code Online (Sandbox Code Playgroud)

jpa fetch spring-data-jpa entitygraph

47
推荐指数
4
解决办法
3万
查看次数

加入三个深度表时“MultipleBagFetchException:无法同时获取多个袋子”

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.loader.MultipleBagFetchException: 不能同时取多个包: [Order.items, OrderItem.options];

以上是我加入如下三个表时遇到的一个例外。

订单项选项.java

@Entity
public class OrderItemOption {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "item_option_id")
  private Long id;

  @Column(name = "item_id", nullable = false)
  private Long itemId;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(
      name = "item_id",
      referencedColumnName = "item_id",
      insertable = false,
      updatable = false
  )
  private OrderItem orderItem;
}
Run Code Online (Sandbox Code Playgroud)

订单项.java

@Entity
public class OrderItem {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "item_id")
  private Long id;

  @Column(name = "order_id", nullable = false)
  private Long orderId;

  @ManyToOne(fetch = FetchType.LAZY) …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate jpa querydsl

6
推荐指数
1
解决办法
9063
查看次数

标签 统计

jpa ×2

entitygraph ×1

fetch ×1

hibernate ×1

java ×1

querydsl ×1

spring ×1

spring-data-jpa ×1