Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JPA, how to get from multiple table in a single entity class

Entity : Details
@OneToMany(mappedBy = "refId")
private Collection<PersonDetail> PersonDetailCollection;
@Id
@Column(name = "REFERENCE_ID")
private String referenceId;
@Column(name = "PRICE_ID")
private String priceID;
@Column(name = "NAME")
private String name;
Getter and Setter of all variable

Entity: PersonDetail 
@JoinColumn(name = "REF_ID", referencedColumnName="REFERENCE_ID")
@ManyToOne
private Details refId;
Getter and Setter of all variable

Entity: PriceDetail
@Id
@Column(name = "PRICE_ID")
private String priceID;
@Column(name = "PRICE")
private String price;
Getter and Setter of all variable

Here I have three table from which table Details and PersonDetail have a join. How to get data from these three tables in a single entity without a join on PriceDetail table?

like image 289
rsi Avatar asked Jan 17 '26 19:01

rsi


2 Answers

I think what you want is selecting into new object instance using its constructor.

You will have to create a new class, e.g. DetailsResult which would have all the properties you need to retrieve. Then you can use the JPQL query to fetch data:

"select new your.package.DetailsResult(
<fill here the required constructor arguments, 
that you will get from the query results, like 'd.referenceId' or 'p.priceID'>)
 from Details d, PriceDetail p where d.priceID = p.priceID"

I hope this will put you on the right track.

like image 140
rapasoft Avatar answered Jan 20 '26 09:01

rapasoft


try writing NamedNativeQuery in the parent class itself. I think this works.!!

like image 38
thecoot_11 Avatar answered Jan 20 '26 11:01

thecoot_11



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!