LEFT JOIN FETCH不起作用

时间:2018-05-23 00:35:48

标签: java jpa eclipselink

如何在JPA / Eclipselink 2.6.5中使用三个级别的“LEFT JOIN”获取Table3(关系OneToOne)?

此命令无法获取它:(错误)

> require(R6)

> data(iris)

> dataframe_factory <- R6Class(
+   "dataframe_factory",
+   portable = FALSE,
+   lock_objects = FALSE,
+   private = list(
+     ..iris_data = iris
 .... [TRUNCATED] 

> # Create the DataFrame
> my_Dataframe <- dataframe_factory$new()

> # Retrieve the alias
> my_Dataframe$Sepal.Length
  [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1 5.7 5.1
 [21] 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.0 5.5 4.9 4.4 5.1
 [41] 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0 7.0 6.4 6.9 5.5 6.5 5.7 6.3 4.9 6.6 5.2
 [61] 5.0 5.9 6.0 6.1 5.6 6.7 5.6 5.8 6.2 5.6 5.9 6.1 6.3 6.1 6.4 6.6 6.8 6.7 6.0 5.7
 [81] 5.5 5.5 5.8 6.0 5.4 6.0 6.7 6.3 5.6 5.5 5.5 6.1 5.8 5.0 5.6 5.7 5.7 6.2 5.1 5.7
[101] 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2 6.5 6.4 6.8 5.7 5.8 6.4 6.5 7.7 7.7 6.0
[121] 6.9 5.6 7.7 6.3 6.7 7.2 6.2 6.1 6.4 7.2 7.4 7.9 6.4 6.3 6.1 7.7 6.3 6.4 6.0 6.9
[141] 6.7 6.9 5.8 6.8 6.7 6.7 6.3 6.5 6.2 5.9

> my_Dataframe$another.Sepal.Length
  [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1 5.7 5.1
 [21] 5.4 5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epal.Length)
[1] TRUE

> identical(my_Dataframe$Sepal.Length[1], my_Dataframe$another.Sepal.Length[1])
[1] TRUE

> identical(my_Dataframe$Sepal.Length[1:2], my_Dataframe$another.Sepal.Length[1:2])
[1] TRUE

此命令获取它,但不要先保持“LEFT”:(错误)

SELECT t1 FROM Table1 t1 LEFT JOIN FETCH t1.table2 t2
                         LEFT JOIN FETCH t2.table3

这个获取它,但它只是两个级别:(不需要的)

SELECT t1 FROM Table1 t1 LEFT JOIN FETCH t1.table2
                         LEFT JOIN FETCH t1.table2.table3

表1:

SELECT t2 FROM Table2 t2 LEFT JOIN FETCH t2.table3

表2:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "Id")
private Integer id;

@JoinColumn(name = "Table2", referencedColumnName = "Id")
@ManyToOne(fetch = FetchType.LAZY)
private Table2 table2;

表3:

@Id
@Basic(optional = false)
@NotNull
@Column(name = "Id")
private Integer id;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "table2")
private Collection<Table1> table1Collection;

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "table2")
private Table3 table3;

谢谢!

1 个答案:

答案 0 :(得分:0)

JPA / JPQL不支持嵌套的提取连接。要解决此问题,您可以在查询中添加query hint,以使用左外连接告诉它您想要加入的内容。 fetch-join查询提示有一个更好的示例,说明如何使用它。

Query query = em.createQuery("SELECT t1 FROM Table1 t1", Table1.class);
query.setHint("eclipselink.LEFT_FETCH", "t1.table2");
query.setHint("eclipselink.LEFT_FETCH", "t1.table2.table3");