A
A
Anton Boyarshinov2016-12-05 13:02:03
MySQL
Anton Boyarshinov, 2016-12-05 13:02:03

How to correctly display the contents of the database as objects?

There are signs like this.
d7dbce3e015946af9d649d9bdca98740.jpg
solditems table - a list of sold items in 1 receipt. Since the main information about the product is stored in products, records from solditems refer to the product_id , and also store the number of items indicated in the receipt and the price at the time of sale.
It is necessary for me that I could look at the object (suppose Check) in the form:
name | id | price | at the time of sale | some other fields.
Hibernate has generated an entity for each table and there is no problem viewing data from checks and solditems. The question is how to "attach" data from products.
It only comes to mind to create a wrapper class over several entities that will store everything like this:

public class Check {
  private CheckEntity checkEntity; //информация из checks
  private List<Entry> entries = new ArrayList<Entry>();
  class Entry {
       SoldItemEntity soldItemEntity ; //информация из solditems 
       ProductEntity productEntity; // product entity из которой уже и брать имя и тд
  } 
}

And already collect this wrapper somewhere... in checkDAO for example.
But I am sure that there is some easier option without bicycles, and I hope you will tell me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor Potapov, 2016-12-15
@Zhyki

What is your problem? you spelled it all out for yourself.
There is a many-to-one relationship between soliditems and products. Hibernate knows how to organize all this perfectly.
You will get the product simply and elegantly ala soliditem.getProduct(). I would write you another mapping for classes, but I'm afraid I don't quite understand your domain model.
In addition, I would advise you not to remember the price at the time of sale, but to make the prices of products historical. those. connection with the product, when the product is changed, a copy is created, and the old version with the old price is marked as deleted\notActual. this is how you maintain the integrity of the data.
You can read about one-to-many here or here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question