When to use @Embedded and @Embeddable?

Is it possible to annotate a class as @Embeddable or a property as @Embedded ?

Sample code:

 @Embeddable class A{ ... } class B{ ... } @Entity class Foo { A a; @Embedded B b; } 

When to prefer @Embedded and @Embeddable ?

+7
source share
5 answers

As you know, for @Embedded / @Embeddable there are two main uses:

First and foremost: the separation of large classes of entities. In the database world, a large table (one with many columns) is in order. Violation of such a table can even worsen the situation and interfere with the principles of database design. In Java (or object-oriented languages), on the other hand, a large class is code smell . Here we would like to divide the classes, including entity classes, into smaller units. @Embedded / @Embeddable allows us to easily do this without breaking the database table.

Secondly, it allows you to reuse common mappings between objects. Let's say each table has simple versioning, with two columns containing the username of the person who changed the row, and the time it happened. You can then create an @Embeddable object that spans these lines, and then reuse it for all objects by inserting it (instead of repeating the variables corresponding to these columns in each object.)

+7
source

If we have Person and Address, which are two POJOs, you will not want to create another table for the address, but you will want to insert the address into the person table. Thus, the address adds the value to the Person object, but does not make any sense individually. In this case, we can go:

 @Embeddable public class Address{ } @Entity public class Person { @Embedded private Address address; } 
+2
source

You will use @Embeddable and @Embedded together. You mark your class as @Embeddable, which indicates that this class will not exist in the database as a separate table. Now that you use @Embedded in the field itself.

The word embeddable and embedded gives you a big key.

Embeddable = This class can be embedded in the class Embedded = This class will now be embedded in your class as a field.

+2
source

These are both used in sleep mode with annotation:

@Embeddable for combined primary key.

@ A mortgaged entity is merged with the parent object and processed as if they were declared there all the time. Sometimes you have a huge table with several columns (especially with legacy databases). However, some columns are logically related to each other (for example, street, city and phone number in the CUSTOMER table). If you do not want to create an object with all fields, you create an inline address object. This way you logically group the address columns into an object instead of having the same number of POJOs with a flat list of fields.

0
source

I think if you annotate a class like @Embeddable you don't need to annotate a field like @Embedded . Also, if you annotate a class as @Embeddable and want to use it as a primary key, you can only use @Id , but if it is not annotated as @Embeddable , you must use @EmbeddedId for the field to work as the primary key.

0
source

Source: https://habr.com/ru/post/1242187/


All Articles