select n.incoming, n.outgoing, c.claim_id, c.claim_label, c.claim_date, c.claim_status
from CLAIM c
left join PERSON p on p.person_id = c.person_id
left join NMI_VIEW n on n.claim_id = c.claim_id
and n.person_identifier = p.person_identifier
;
基本上,PERSON
表在此查询中充当中间表,但不保证在c.person_id
列上具有匹配项。我只想从NMI_VIEW
视图中获取一行,如果它与claim_id
和person_identifier
匹配。因此NMI_VIEW
联接基于2个不同表中的2列。
所以我的问题是,是否有可能(以及如何)让JPA投影/实体产生这种类型的查询?我将列出我在下面的当前课程的基本部分。
NmiViewPK
public class NmiViewPK implements Serializable {
private Long claimId;
private String personIdentifier;
}
NmiViewProjection:
@Data
@Immutable
@Entity
@IdClass(NmiViewPK.class)
@Table(name = "NMI_VIEW")
public class NmiViewEntity {
@Id
@Column(name = "CLAIM_ID")
private Long claimId;
@Id
@Column(name="PERSON_IDENTIFIER")
private String personIdentifier;
// ??? This is either a 1 or 0; can I make it a Boolean instead of Integer??
@Column(name = "OUTGOING")
private Integer outgoing;
// ??? This is either a 1 or 0; can I make it a Boolean instead of Integer??
@Column(name = "INCOMING")
private Integer incoming;
@Column(name = "SINGLE_STATUS")
private Integer singleStatus;
}
PersonProjection:
@Data
@Entity
@Immutable
@EqualsAndHashCode(of = "personId")
@ToString(of = "personId")
@Table(name = "PERSON")
public class PersonProjection {
public PersonProjection() {
// JPA
}
@Id
@Column(name = "PERSON_ID")
private Long personId;
@Column(name = "PERSON_IDENTIFIER")
private String personIdentifier;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "MIDDLE_NAME")
private String middleName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "SSN")
private String ssn;
@Column(name = "DATE_OF_BIRTH")
private Date dateOfBirth;
}
ClaimProjection:
@NamedEntityGraph(name = "ClaimProjection.graph",
attributeNodes = {
@NamedAttributeNode(value = "personProjection"),
@NamedAttributeNode(value = "nmiViewEntity")})
@Data
@Entity
@Immutable
@EqualsAndHashCode(of = "claimId")
@ToString(of = "claimId")
@Table(name = "CLAIM")
public class ClaimProjection {
public ClaimProjection() {
// JPA
}
@Id
@Column(name = "CLAIM_ID")
private Long claimId;
@Column(name = "CLAIM_LABEL")
private String claimLabel;
@Column(name = "CLAIM_DATE")
@Temporal(TemporalType.TIMESTAMP)
private Date claimDate;
@Column(name = "CLAIM_STATUS")
private String claimStatus;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PERSON_ID")
@NotFound(action = NotFoundAction.IGNORE)
private PersonProjection personProjection;
// TODO Do not know how to properly join NMI_VIEW here!!
@OneToOne(fetch = FetchType.Lazy)
@LazyToOne(LazyToOneOption.NO_PROXY)
@JoinColumns({
@JoinColumn(name = "CLAIM_ID", insertable = false, updatable = false),
@JoinColumn(name = "PersonProjection.PERSON_IDENTIFIER", insertable = false, updatable = false)
})
@NotFound(action = NotFoundAction.IGNORE)
private NmiViewEntity nmiViewEntity;
}
我继承了这段代码,并在ClaimProjection.nmiViewEntity上添加了一些注释作为如何执行连接的最佳猜测,但我感觉它们无法正常工作。如果任何人对如何正确连接有任何想法,也许对此处的任何其他更正,我真的很感激!提前谢谢!
编辑1:我按原样测试了这些类,但它目前无效,部分原因是NmiViewEntity
上没有合适的复合ID。 (不确定我是否应该在这里使用复合ID类或嵌入式ID类?)
编辑2:为NmiViewEntity添加了PK类。 添加复合ID类后,我收到此错误:
2017-07-05 16:06:57.316 ERROR 12279 --- [main]
o.h.engine.jdbc.spi.SqlExceptionHelper : ORA-00904: "CLAIMPR0_"."PERSON_PROJECTION_PERSON_IDENTIFIER": invalid identifier