У меня есть два объекта A, B. В этом A является родительским и имеет структуру таблицы ниже.
Таблица-А
A1 --> Колонка
Таблица-Б
B1-->A1 B2-->A1
Мне нужно получить строку A1 и связанные с ней строки в таблице B (B1, B2) на основе условия where для обеих таблиц A и B.
Когда я использую левое внешнее соединение, JPA возвращает две строки с одинаковым значением в таблице b (B1-A1 или B2-A1).
Даже я пытался использовать обычное соединение. Пожалуйста, дайте мне знать, что мне не хватает.
Я использую @Query для указания запроса. SELECT a From TableA a LEFT OUTER JOIN FETCH a.TablesBCollection p где a.TableAColum =?1 и p.TableBColumn‹>p.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")