I have a question regarding an ArrayList of Integers, or primitive types in general. Suppose I’m designing a POS program and each product may have several prices.
Let’s assume I can represent a price value with ints and in the Product class I have the field ArrayList<Integer> prices. What’s the best way to map this with Hibernate?
I could map it to a product_prices table with a field containing the price value and a field for the foreign key referencing the product in question, but this seems overkill.
On the other hand, I could concatenate all prices in a String and store it as a field in the products table, with the prices separated by semicolons, for instance. This way I’m saving one table and a future select, but it doesn’t seem quite OO.
What is the best to do here?
No, it’s not relational. That breaks the rules for the first normal form.
I don’t see why you’re worried about saving a table and a SELECT. This is premature optimization at its worst.
A product may have several prices, but there will also be criteria that tell you when one applies (e.g., effective date, discount conditions, etc.) You should add those into your schema as well.
I’d recommend a Product table, without any pricing or discount information in it.
It sounds like there’d be a many-to-many relationship between products and prices if Price has an effective date in it, so you’d have a Product_Price JOIN table as well.