0

I don't know how to make it clear, and i couldn't think of any good title for this.

Basically, i have a database table which looks like this

ID name another_column ref_id
1 name1 asdasd same_ref1
2 name2 zzzzzz same_ref1
3 name3 aadas same_ref2
4 name4 ppppp same_ref2

I have an entity which maps to this table, lets say, and i can use JpaRepository methods :

public class TestObject {
   private int id;
   private String name;
   private String anotherColumn;
   private String refId;
}

Is it possible for me to have another object, lets say ParentObject that is mapped based on ref_id column

public class ParentObject {
  private String refId;
  private List<TestObject> child;
}

That way when I use a JpaRepository<ParentObject> for example

parentRepository.findById("same_ref2");

it returns a ParentObject with two children (lines 3 and 4).

Don't know if I made that clear.

retArdos
  • 135
  • 2
  • 4
  • 17
  • Do you want a projection for query result mapping? – Turing85 Oct 25 '22 at 18:34
  • Mmm can’t you make directly a query on TestObject on refId that return a List? Why do you need ParentObject? – Pp88 Oct 25 '22 at 18:36
  • What if you use a findByRefId and then a groupingBy with the stream collectors ? – luisenricke Oct 25 '22 at 18:52
  • Well, let's say TestObject is the basic object that lives for sometimes. Once n TestObjects are associated with the REF_ID column, they are not really used anymore. The ParentObject becomes the one used. And ParentObject should be queryable via classic JpaRepository. In my example case, a parentRepository.findAll() should return a list of two ParentObjects. – retArdos Oct 25 '22 at 22:15

0 Answers0