使用oneToMany关系加入两个JPA实体

时间:2017-04-03 05:57:47

标签: hibernate jpa spring-data-jpa

我有两个参与者A,B。这个A是父母,并且具有以下表格结构。

表-A

A1 - >将aColumn

表-B

B1 - > A1 B2 - > A1

我需要根据表A和B中的where条件获取表B(B1,B2)中的A1行及其相关行。

当我使用Left outer join fetch时,JPA返回表-b(B1-A1或B2-A1)中具有相同值的两行。

即使我尝试过使用普通的join.Pls让我知道我错过了什么。

我正在使用@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")

1 个答案:

答案 0 :(得分:0)

为什么不通过将IDE连接到数据库来生成数据库表作为POJO。 您可以在IDE中create a JPA project并连接到您的数据库it will ask for the relationship between columns, you can mention it there,它将为您创建所需的POJO。