Get value from JPA subentities
Clash Royale CLAN TAG#URR8PPP
Get value from JPA subentities
How can I retrieve the value from childofchild entity using Spring-data-jpa?
eg. Person Entity ->Account Entity ->Debit_Account Entity
Person Entity ->Account Entity ->Debit_Account Entity
Debit Account Entity contains value dbtAmount that I want to retrieve.
I am new in spring and spring-data-jpa repository using find method I cannot get details from childofchild entity.
You can do this via Inheritance relation betweeen AccountEntity and DebitAccount subentity.
– Gaurav Srivastav
2 hours ago
1 Answer
1
We have to implement the relationships between the entities properly. Such as,
If you have configured this properly you should be able to get person entity, from that get the Account entity using getAccounts method and from that get the DebitAccount using the getDebitAccounts methods.
JPA Relationship Mapping Concept
https://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example/
Assuming the relationship are one to many, i have added more explanation,
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
@Entity
public class Person
@Column
private String username;
@Column
private String email;
@OneToMany(mappedBy="user")
private Set<Account> accounts;
public String getUsername()
return username;
public void setUsername(String username)
this.username = username;
public String getEmail()
return email;
public void setEmail(String email)
this.email = email;
public Set<Account> getAccounts()
return accounts;
public void setAccounts(Set<Account> accounts)
this.accounts = accounts;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
@Entity
public class Account
@Column
String name;
@OneToMany(mappedBy="account")
private Set<DebitAccount> debitAccounts;
public String getName()
return name;
public void setName(String name)
this.name = name;
public Set<DebitAccount> getDebitAccounts()
return debitAccounts;
public void setDebitAccounts(Set<DebitAccount> debitAccounts)
this.debitAccounts = debitAccounts;
import javax.persistence.Column;
import javax.persistence.Entity;
@Entity
public class DebitAccount
@Column
String name;
public String getName()
return name;
public void setName(String name)
this.name = name;
Hope this helps.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Please be careful, if you start a line with 4 (or more) spaces, the editor assumes it is code. When you create a new question, or edit it, you will see editor help on the right of the screen. You should take the tour, read How to Ask and Minimal, Complete, and Verifiable example.
– Nic3500
2 hours ago