Java – add JPA to the right

NewRequest. java

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(nullable = false)
private Long id;

@Size(max = 2147483647)
@Column(length = 2147483647)
private String data;

@JoinColumn(name = "buyer_id",referencedColumnName = "id")
@ManyToOne(fetch = FetchType.LAZY)
private Buyer buyer_id;

@OneToMany(mappedBy = "newrequest_id",fetch = FetchType.LAZY)
private Collection<Mapping> mappingCollection;

Mapping. java

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(nullable = false)
private Long id;

@JoinColumn(name = "seller_id",referencedColumnName = "id")
@ManyToOne(fetch = FetchType.LAZY)
private Seller seller_id;

@JoinColumn(name = "newrequest_id",referencedColumnName = "id")
@ManyToOne(fetch = FetchType.LAZY)
private NewRequest newrequest_id;

It can be said that I have such a record

new_ request:

id(PK) |  data  | buyer_id(FK)
------------------------------
1      |  data1 |   1
2      |  data2 |   1
3      |  data3 |   2

Mapping:

id(PK) | newrequest_id(FK) | seller_id(FK)
------------------------------------------
1      |        1          |    1

Note: consider that there are two different tables between the buyer and the seller

I want all new mapped to the seller_ request. Therefore, expected outputs:

id   | data  | buyer_id | seller_id
------------------------------------
1    | data1 |  1       |    1
2    | data2 |  1       |
3    | data3 |  2       |

I tried the following query in JPA:

select n.id,n.data,n.buyer_id,m.seller_id from Mapping m left join m.newrequest_id n

It provides only one data (output behavior 1)

I used the native query to get my results through the right connection But I want to implement it with JPA queries

Solution

You only need to switch entity classes in the query (in most cases, the right outer connection is just the inversion of the left outer connection):

select n.id,m.seller_id 
  from NewRequest n left join n.mappingCollection m

As I suggested in the comments above: you should remove from the attribute_ ID suffix, because it can be read and understood better

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>