How to build a connection with such a task and is it even possible to do this

Table1 contains a list of goods that I own

user_id | product_id | status 

Table2 contains information about these products.

 product_id | title | description | size 

In one query, you can get everything from Table2 (a list of all products) and add a status column to all? if this product is tied to user_id, then the status is taken from Table1 otherwise 0

Table1

 user_id | product_id | status 1 1 5 1 2 3 

Table2

 product_id | title | description | size 1 Title1 Description1 2 2 Title2 Description2 31 3 Title3 Description3 3 4 Title4 Description4 12 

I must return

 product_id | title | description | size | status 1 Title1 Description1 2 5 2 Title2 Description2 31 3 3 Title3 Description3 3 0 4 Title4 Description4 12 0 

In Java, I need a class in User, for example.

 private Set<Product> products; 
  • Here lay the result of the sample, what is the solution?
  • one
    As an option, I can make a database with a view with such data (product_id | title | description | size | status), which are easily displayed at the CSL level and then the entity should be built according to the view rather than by the tables. - aleshka-batman
  • Hibernate allows you to automatically throw views into the database? You can give a link where to read, please. - user_21
  • No, hibernate does not do view. I suggest in the database to make the view itself. - aleshka-batman 1:09 pm

1 answer 1

You can consider this option.

 @Entity @Table(name="USER") public class User { @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id", unique = true, nullable = false) private Long id; //... @OneToMany(mappedBy="user") private Set<ItemEntry> items; //... } @Entity @Table(name="PRODUCT") public class Product{ //... @ManyToOne @JoinColumn(name="user_id", nullable=false) private User user; // getters and setters } 

More detailed example