【发布时间】:2017-04-03 05:57:47
【问题描述】:
我有两个实体 A,B。在这个 A 中是父级并且具有下表结构。
表-A
A1-->栏目
表-B
B1-->A1 B2-->A1
我需要根据表 A 和 B 上的 where 条件获取表 B(B1,B2) 中的 A1 行及其关联行。
当我使用左外连接获取时,JPA 将返回 Table-b(B1-A1 或 B2-A1)中具有相同值的两行。
即使我尝试过使用普通连接。请让我知道我缺少什么。
我正在使用@Query 来指定查询。 SELECT a From TableA a LEFT OUTER JOIN FETCH a.TablesBCollection p where a.TableAColum =?1 and p.TableBColumnp.TableBColumn.
@Entity
@Table(name = "USER", schema = "USER_PROFILE")
public class User implements java.io.Serializable {
private BigDecimal userId;
private String firstNm;
private String lastNm;
private String userIndex;
private Set<UserPhone> userPhones = new HashSet<UserPhone>(0);
public User(){
}
public User(BigDecimal userId,String firstNm,String lastNm){
this.userId=userId;
this.firstNm=firstNm;
this.lastNm=lastNm;
}
public User(BigDecimal userId,String firstNm,String lastNm,Set<UserPhone> userPhones){
this.userId=userId;
this.firstNm=firstNm;
this.lastNm=lastNm;
this.userPhones=userPhones;
}
@Id
@Column(name = "USER_ID", unique = true, nullable = false, precision = 22, scale = 0)
public BigDecimal userId() {
return this.userId;
}
public void setuserId(BigDecimal userId) {
this.userId = userId;
}
@Column(name = "FIRST_NM", nullable = false, length=50)
public String firstNm() {
return this.firstNm;
}
public void setfirstNm(String firstNm) {
this.firstNm = firstNm;
}
@Column(name = "LAST_NM", nullable = false, length=50 )
public String lastNm() {
return this.lastNm;
}
public void setlastNm(String lastNm) {
this.lastNm = lastNm;
}
@Column(name = "USER_INDEX", nullable = false, length=50 )
public String userIndex() {
return this.userIndex;
}
public void setUserIndex(String userIndex) {
this.userIndex = userIndex;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
public Set<UserPhone> getuserPhones() {
return this.userPhones;
}
public void setUserPhones(Set<UserPhone> userPhones) {
this.userPhones = userPhones;
}
}
@Entity
@Table(name = "USER_PHONE", schema = "USER_PROFILE")
public class UserPhone implements java.io.Serializable {
private BigDecimal userPhoneId;
private String city;
private String State;
private User user;
public UserPhone(){
}
public UserPhone(BigDecimal userPhoneId){
this.userPhoneId = userPhoneId;
}
public UserPhone(BigDecimal userPhoneId,String city,String State,User user){
this.userPhoneId = userPhoneId;
this.city = city;
this.State = State;
this.user = user;
}
@Id
@Column(name = "USER_PHONE_ID", unique = true, nullable = false, precision = 22, scale = 0)
public BigDecimal userPhoneId() {
return this.userPhoneId;
}
public void setUserPhoneId(BigDecimal userPhoneId) {
this.userPhoneId = userPhoneId;
}
@Column(name = "CITY", nullable = false, length=50 )
public String city() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
@Column(name = "STATE", nullable = false, length=50 )
public String state() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "USER_ID")
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
}
@QUERY("Select a from User a LEFT OUTER JOIN FETCH a.userPhones p where a.userIndex =?1 and p.city<>p.state")
【问题讨论】:
-
JPA 围绕类而不是表进行操作。发布实际实体
标签: hibernate jpa spring-data-jpa