0% found this document useful (0 votes)
2 views

JPA

The document provides an overview of various JPA annotations, including their purposes and examples, categorized into sections such as JPA Annotations, Relationship Annotations, Query Annotations, Lifecycle Callbacks, Advanced Mapping, and Caching Annotations. Each annotation is described with its function and a code snippet demonstrating its usage. This serves as a reference for developers working with JPA in Java applications.

Uploaded by

Debasis Bhuyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JPA

The document provides an overview of various JPA annotations, including their purposes and examples, categorized into sections such as JPA Annotations, Relationship Annotations, Query Annotations, Lifecycle Callbacks, Advanced Mapping, and Caching Annotations. Each annotation is described with its function and a code snippet demonstrating its usage. This serves as a reference for developers working with JPA in Java applications.

Uploaded by

Debasis Bhuyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

JPA Annotations
Annotation Purpose Example

@Entity Marks a class as a JPA entity java @Entity public class User { @Id private

(mapped to a database table). Long id; private String name; }

@Table Specifies the table name and java @Entity @Table(name = "users") public

other metadata for the entity. class User { @Id private Long id; }

@Id Marks a field as the primary key java @Entity public class User { @Id private

of the entity. Long id; @Column private String name; }

@GeneratedValue Specifies the generation strategy java @Id @GeneratedValue(strategy =

for the primary key. GenerationType.IDENTITY) private Long id;

@Column Specifies the column name for a java @Column(name = "username", nullable =

field and additional column false) private String username;

attributes.

@Basic Marks a field as a basic mapping java @Basic private String name;

(default).

@Transient Excludes a field from being java @Transient private String temporaryData;

persisted to the database.

@Version Implements optimistic locking java @Version private Integer version;

using a version field.

2. JPA Relationship Annotations


Annotation Purpose Example

@OneToOne Defines a one-to-one java @OneToOne private Address address;

relationship between
two entities.

@OneToMany Defines a one-to-many java @OneToMany(mappedBy = "user") private List<Order>

relationship between orders;

entities.
Annotation Purpose Example

@ManyToOne Defines a many-to-one java @ManyToOne @JoinColumn(name = "user_id") private User

relationship between user;

entities.

@ManyToMany Defines a many-to- java @ManyToMany private List<Role> roles;

many relationship
between entities.

@JoinColumn Specifies the foreign java @ManyToOne @JoinColumn(name = "user_id") private User

key column for a user;

relationship.

@JoinTable Defines a join table for java @ManyToMany @JoinTable(name = "user_role",

many-to-many joinColumns = @JoinColumn(name = "user_id"),


inverseJoinColumns = @JoinColumn(name = "role_id")) private
relationships.
List<Role> roles;

3. JPA Query Annotations


Annotation Purpose Example

@NamedQuery Defines a static JPQL java @NamedQuery(name = "User.findByUsername", query =

query for an entity. "SELECT u FROM User u WHERE u.username = :username")


public class User { }

@NamedNativeQuery Defines a static SQL java @NamedNativeQuery(name = "User.findByEmail", query

query for an entity. = "SELECT * FROM users WHERE email = :email",


resultClass = User.class) public class User { }

@Query Defines a custom java @Query("SELECT u FROM User u WHERE u.username =

query for a :username") User findByUsername(@Param("username")


String username);
repository method
(Spring Data JPA).

4. JPA Lifecycle Callbacks


Annotation Purpose Example

@PrePersist Marks a method to execute java @PrePersist public void prePersist() {

before an entity is persisted. this.createdDate = new Date(); }

@PostPersist Marks a method to execute java @PostPersist public void postPersist() {

after an entity is persisted. System.out.println("Entity persisted"); }

@PreUpdate Marks a method to execute java @PreUpdate public void preUpdate() {

before an entity is updated. this.updatedDate = new Date(); }

@PostUpdate Marks a method to execute java @PostUpdate public void postUpdate() {

after an entity is updated. System.out.println("Entity updated"); }

@PreRemove Marks a method to execute java @PreRemove public void preRemove() {

before an entity is removed. System.out.println("Entity about to be removed");


}

@PostRemove Marks a method to execute java @PostRemove public void postRemove() {

after an entity is removed. System.out.println("Entity removed"); }

@PostLoad Marks a method to execute java @PostLoad public void postLoad() {

after an entity is loaded from System.out.println("Entity loaded"); }

the database.

5. JPA Advanced Mapping


Annotation Purpose Example

@Embeddable Marks a class as embeddable, to be java @Embeddable public class Address {

used within an entity as a component. private String street; private String


city; }

@Embedded Embeds an @Embeddable object java @Entity public class User {

inside an entity. @Embedded private Address address; }

@ElementCollection Maps a collection of basic or java @ElementCollection private

embeddable types (e.g., List, Set). Set<String> phoneNumbers;

@MapKey Specifies a key for a Map collection. java @OneToMany private Map<String,
Order> orders; @MapKey(name =
"orderNumber")

@Lob Marks a field as a large object (BLOB java @Lob private String description;

or CLOB).
Annotation Purpose Example

@Enumerated Maps an enum type to a database java @Enumerated(EnumType.STRING)

column. private Status status;

@Temporal Specifies the temporal mapping java @Temporal(TemporalType.DATE)

for Date or Calendar fields. private Date startDate;

@Formula Maps a field to a computed SQL java @Formula("YEAR(CURRENT_DATE) -

expression. YEAR(birth_date)") private Integer age;

6. JPA Caching Annotations


Annotation Purpose Example

@Cacheable Enables caching for an java @Cacheable public class Product { @Id private Long

entity. id; @Column private String name; }

@Cache Specifies Hibernate- java @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

specific caching public class Product { }

behavior.

You might also like