I am learning JPA from this tutorial.
I have some confusions in understanding the following annotations:
@Basic@EmbeddedFields of an embeddable type default to persistent, as if annotated with @Embedded.
If the fields of embeddable types default to persistent, then why would we need the @Embedded annotation
We can use the @Basic annotation to mark a basic type property: @Entity public class Course { @Basic @Id private int id; @Basic private String name; ... } In other words, the @Basic annotation on a field or a property signifies that it's a basic type and Hibernate should use the standard mapping for its persistence.
4. @Embedded. The JPA annotation @Embedded is used to embed a type into another entity.
For the @Basic annotation, this writes: The simplest type of mapping to a database column. The Basic annotation can be applied to a persistent property or instance variable of any of the following types: Java primitive types, wrappers of the primitive types, String, java. math.
The @Embedded annotation is used to specify a persistent field or property of an entity whose value is an instance of an embeddable class.
The @Embeddable annotation allows to specify a class whose instances are stored as intrinsic part of the owning entity. This annotation has no attributes.
@Embeddable public class EmploymentPeriod { java.util.Date startDate; java.util.Date endDate; ... } The @Embedded annotation is used to specify a persistent field or property of an entity whose value is an instance of an embeddable class. By default, column definitions specified in the @Embeddable class apply to the table of the owning entity but you can override them using@AttributeOverride:
@Embedded @AttributeOverrides({ @AttributeOverride(name="startDate", column=@Column(name="EMP_START")), @AttributeOverride(name="endDate", column=@Column(name="EMP_END")) }) public EmploymentPeriod getEmploymentPeriod() { ... } Regarding the optional @Basic annotation, you may use it to configure the fetch type to LAZY and to configure the mapping to forbid null values (for non primitive types) with the optional attribute.
@Basic(fetch=LAZY) protected String getName() { return name; } You can also place it on a field or property to explicitly mark it as persistent (for documentation purpose).
In ORM mapping, the granularity of your object model can be finer than that of your database.
For example, you can have a Person record in your database which can be further decomposed to contain a reference to an Address object in your model. That's where the @Embedded and @Embeddable annotations come in. They simply state a relationship where one Entity can be stored as part of another.
As for the @Basic annotation, it's the simplest form of mapping which is applied by default to primitive types such as int and float and their wrappers as well as enums. More information can be had here: http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-mapping-property
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With