W
W
Wan-Derer2021-04-28 19:42:26
Java
Wan-Derer, 2021-04-28 19:42:26

Hibernate how to get two objects from same table?

The database looks something like this:

объект    поле_а    поле_б    поле_ц
------------------------------------
объект1   а1        б1        ц1
объект1   а1        б1        ц2
объект1   а1        б1        ц3

объект2   а2        б2        ц4
объект2   а2        б2        ц5
.................


Those. one object of the subject area corresponds to several records (rows) of the database. Some of the fields are the same from line to line, some are different. To describe this situation, I made the following Java classes:

public class ObjectC {
  private int fieldC;
}

public class MyObject{
  private String objName;
  private int fieldA;
  private int fieldB;

  private List<ObjectC> fieldC;
}


Getters, setters, empty constructors available.

I can get these objects by "manually" querying the database.
But I can't figure out how to describe such a data structure in terms of Hibernate. All examples which I saw, are under construction by the principle the table -> object. And I need to pull out two types of objects from one table, and even "embed" one into the other.

Prompt, it is possible with Hibernate and how such things become.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-04-28
Hasanly @azerphoenix

I suspect you need to look towards
JPA / Hibernate Projections
https://www.baeldung.com/jpa-hibernate-projections

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question