Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What impact does the @embedded annotation mean?

How does the Embedded annotation affect the database?

How will SQL queries need to change?

What's the typical usecase for using the annotation?

like image 502
user48545 Avatar asked Oct 05 '11 16:10

user48545


People also ask

What is @embedded annotation?

4. @Embedded. The JPA annotation @Embedded is used to embed a type into another entity.

What is @embeddable used for?

@Embedded / @Embeddable allows us to easily do this without having to split the database table. Second, it allows for reuse of common mappings between entities. Say each table has a simple revision tracking, with two columns containing the username of the person who changed the row, and the time it happened.

What is the role of annotation in JPA?

JPA annotations are used in mapping java objects to the database tables, columns etc. Hibernate is the most popular implement of JPA specification and provides some additional annotations.

Which annotations are mandatory in an entity class?

In most cases, you only need to annotate your entity class with @Entity and your primary key attribute with @Id and @GeneratedValue. If the names of your entity class or one of its attributes don't match the table or column names, you can adjust the mapping using a @Table or @Column annotation.


1 Answers

How does Embedded annotation affect the database?

It does not affect it at all. On ORM provider layer all fields from embedded entity are merged with parent entity and treated the same as if they were declared there all the time. In other words it works as if you would literally copy all the fields, getters and setters into the entity that contains embedded object.

How will SQL queries need to change?

They won't. You don't need to change anything. See above.

What's the typical case for using the annotation annotation?

Sometimes you have a huge table with several columns (especially with legacy databases). However some columns are logically tied to each other (like street, city and phone number in CUSTOMER table). When you don't want to create an object with all the fields, you create an embedded Address object. This way you logically group address columns into an object instead of having equally huge POJO with a flat list of fields.

Using embedded objects is considered a good practice, especially when strong 1-1 relationship is discovered.

like image 195
Tomasz Nurkiewicz Avatar answered Oct 28 '22 07:10

Tomasz Nurkiewicz