Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - JPA @Basic and @Embedded annotations

Tags:

java

jpa

I am learning JPA from this tutorial.

I have some confusions in understanding the following annotations:

  • @Basic
  • @Embedded

Fields 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

like image 597
Yatendra Avatar asked Apr 05 '10 13:04

Yatendra


People also ask

What is @basic annotation in JPA?

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.

What is @embedded annotation?

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

What is @basic annotation in Java?

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.

What is @embeded in JPA?

The @Embedded annotation is used to specify a persistent field or property of an entity whose value is an instance of an embeddable class.


2 Answers

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).

like image 150
Pascal Thivent Avatar answered Sep 22 '22 11:09

Pascal Thivent


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

like image 39
James P. Avatar answered Sep 22 '22 11:09

James P.