Junior — Middle
What is the significance of the @Embeddable annotation in the context of JPA and how is it used?
sobes.tech AI
Answer from AI
The @Embeddable annotation in JPA is used to designate a class whose objects can be embedded into other entities as part of their state. Such a class does not have its own identifier and does not exist separately in the database.
This is convenient for grouping related fields into a separate class, which is then included in an entity using the @Embedded annotation.
Example:
@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
}
Thus, the Address fields will be stored in the User table as columns, not in a separate table.