本质:
package com.igorgorbunov3333.core.entities.domain;
import javax.persistence.*;
import java.util.Date;
/**
* Created by Игорь on 03.04.2016.
*/
@Entity
@Table(name = "cases")
@NamedQueries({
@NamedQuery(name = "findAllCases", query = "SELECT c FROM Case c JOIN c.client JOIN c.firstInstance " +
"JOIN c.secondInstance JOIN c.thirdInstance JOIN c.category"),
})
public class Case {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "case_number")
private String caseNumber;
@ManyToOne
@JoinColumn(name = "client_id")
private Client client;
@OneToOne
@JoinColumn(name = "first_instance_id")
private FirstInstance firstInstance;
@OneToOne
@JoinColumn(name = "second_instance_id")
private SecondInstance secondInstance;
@OneToOne
@JoinColumn(name = "third_instance_id")
private ThirdInstance thirdInstance;
@Temporal(TemporalType.DATE)
@Column(name = "closing_date")
private Date closingDate;
@ManyToOne
@JoinColumn(name = "category_id")
private Category category;
private float price;
.....
如您所见,有一个命名查询findAllCases
。我得到所有实体 ( Case
) 是这样的:
public List<Case> findAll() {
Query query = entityManager.createNamedQuery("findAllCases");
return query.getResultList();
}
但是该方法返回字段secondInstance
不为thirdInstance
null的所有实体。如何使具有空字段的实体secondInstance
被返回thirdInstance
?
有
SQL
不同类型的表连接(JOIN
-s),在您的情况下,其中两个很有趣INNER JOIN
: 如果附加表中没有记录,则主表中的记录不包含在结果选择中LEFT JOIN
: 主表中的记录包含在结果选择中,无论附加表中是否存在记录在语言中,默认
JPQL
运算符作为,因此在您的情况下,记录被排除在没有在和中找到记录的选择之外。为了将空条目包含在实体选择中,您必须明确指定.JOIN
INNER JOIN
secondInstance
thirdInstance
Case
secondInstance
thirdInstance
LEFT JOIN