Home > database >  @Embeddable or @Entity which one should we choose
@Embeddable or @Entity which one should we choose

Time:12-08

Let's say I have an entity Offer, that has many prices.

@Entity
@Table(name = "offer")
data class Offer(
   val prices: List<Price>,
   @Id
   val id: Long? = null
)

Does it make sense to make Price an Entity or it's better to mark it as Embeddable? What in case there will be an offer with the same price? Perhaps we could reuse the price, or shouldn't we?

I'm looking for a good explanation of when we should use Entity and when Embeddable. Perhaps we could use it as an Entity but set the orphanRemoval = true, so the child in this case Price will be always removed together with an Offer.

CodePudding user response:

I wouldn't definitely try to reuse prices between Offers. The reason is that you might change that price for whatever reason and it will inadvertently change for all Offers. To me this is just asking for troubles and bugs to appear. Unless there is a very good reason in terms of your business case that really makes it mandatory for doing so, I wouldn't do it.

Having written this, with the information that you gave I would definitely go with @Embeddable because it does seem to be. very beneficial to have a separate entity and table for the Price.

  • Related