开发者

What impact does the @embedded annotation mean?

开发者 https://www.devze.com 2023-04-11 12:59 出处:网络
How does the Embedded annotation affect the database? How will SQL queries need to change? What\'s the typical usecase for开发者_运维问答 using the annotation?

How does the Embedded annotation affect the database?

How will SQL queries need to change?

What's the typical usecase for开发者_运维问答 using the annotation?


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.


extending the answer of @Tomasz Nurkiewicz Embedded objects are useful to mapping a table's with a composite primary key whit help of the annotation @EmbenddedId


What's the typical usecase for using the annotation?

This is typically to represent a composite primary key as an embeddable class:

@Entity
public class Project {
    @EmbeddedId ProjectId id;
     :
}



@Embeddable
Class ProjectId {
    int departmentId;
    long projectId;
}

The primary key fields are defined in an embeddable class. The entity contains a single primary key field that is annotated with @EmbeddedId and contains an instance of that embeddable class. When using this form a separate ID class is not defined because the embeddable class itself can represent complete primary key values.

How does the Embedded annotation affect the database?

It does not. Use this annotation to represent a composite primary key.

How will SQL queries need to change?

They won't.


Like Tomasz said - that's the one goal - the other - you can "snaphot" the state of other related entity inside your table. F.e.

@Embeddable public class Company {
    String name;
    String streetName;
    String city;
}



@Entity public class Invoice {

    @Embedded 
    @AttributeOverrides({
        @AttributeOverride(name="name", column=@Column(name="name")),
        @AttributeOverride(name="streetName", column=@Column(name="streetName")),
        @AttributeOverride(name="city", column=@Column(name="city")),
    })            
    Company seller;

    @Embedded 
    @AttributeOverrides({
        @AttributeOverride(name="name", column=@Column(name="name")),
        @AttributeOverride(name="streetName", column=@Column(name="streetName")),
        @AttributeOverride(name="city", column=@Column(name="city")),
    })        
    Company customer;
}

in this example - without embedded and @AttributeOverrides any change in future of Company customer will change the data in Invoice - which is a bug - the invoice was generated for the company with old data.

It's good explained here: :) Java - JPA @Basic and @Embedded annotations


It doesn't always have to be the ID of the class. In Domain Driven Design, you can create a component out of some of the properties of an object, e.g. in this example http://yaarunmulle.com/hibernate/hibernate-example/hibernate-mapping-component-using-annotations-1.html a student has an address component.

The Address property of Student is annotated with @Embedded to point to the Address class component.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号