Junior — Middle
Can you explain the concept of Embedded entities within the Spring Framework?
sobes.tech AI
Answer from AI
In Spring Framework Embedded entities are objects that do not have their own identifier and exist as part of another entity. They are used to group multiple fields into a separate class, which is then embedded into the main entity.
For example, if you have an entity User, and it has an address consisting of street, city, and postal code, you can create an Address class as @Embeddable, and include it in User as @Embedded:
@Embeddable
public class Address {
private String street;
private String city;
private String zipCode;
// getters and setters
}
@Entity
public class User {
@Id
private Long id;
@Embedded
private Address address;
// other fields, getters, and setters
}
The advantage of this approach is logical grouping of related fields and ease of reuse, while the Address data is stored in the same table as User.