Java jpa entity annotation

Java Guides

In this blog post, we will explore the purpose and usage of JPA @Entity and @Table annotations, understanding how they facilitate the persistence of Java entities.

Jakarta Persistence API (JPA) is a powerful technology that simplifies the interaction between Java applications and relational databases. At the heart of JPA lies the @Entity and @Table annotations, which enable developers to seamlessly map Java objects to database tables.

@Entity Annotation: Defining Persistent Entities

The @Entity annotation is a fundamental building block of JPA, indicating that a Java class is a persistent entity, representing a database table. Each instance of an @Entity class corresponds to a row in the associated database table, and the class’s fields are mapped to the table columns.

The below diagram shows the mapping between the Student persistent class and the student database table:

@Entity public class Product < @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private double price; // getters and setters >

In this example, the Product class is marked as an @Entity, representing the product table in the database. If we don’t use @Table annotation to customize the table mapping then JPA gives a name to the table as a class.

@Table Annotation: Customizing Table Mapping

The @Table annotation provides options to customize the mapping of an entity to a database table. It allows you to specify the name of the table, the schema, and other attributes related to the database table.

For instance: In the below code snippet, we are specifying the students table name in the database.

Читайте также:  Critical path css это

@Entity @Table(name = «student«) public class Student

@Entity @Table(name="student", schema="college") public class Student < // fields, getters and setters >

If we do not use the @Table annotation, the name of the entity will be considered the name of the table.

We can also define the unique constraints using uniqueConstraints attribute of @Table annotation:

import jakarta.persistence.*; @Entity @Table( name = "posts", uniqueConstraints = <@UniqueConstraint(columnNames = <"title">)> ) public class Post

Conclusion

The @Entity and @Table annotations are fundamental to JPA, enabling Java developers to seamlessly map Java objects to database tables and establish relationships between entities. By mastering these annotations and understanding their various attributes, developers can create sophisticated and efficient database-driven applications using JPA.

Leveraging the power of JPA simplifies the data access layer and ensures smooth interaction between the Java application and the underlying database.

References

Источник

Java Guides

In this tutorial, we’ll learn how to define a basic JPA entity with an example.

What is JPA Entity?

Entities in JPA are nothing but POJOs representing data that can be persisted in the database. An entity represents a table stored in a database. Every instance of an entity represents a row in the table.

@Entity — JPA Annotation Example

Creating the JPA Entity Class(Persistent class)

import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "student") public class Student < @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private int id; @Column(name = "first_name") private String firstName; @Column(name = "last_name") private String lastName; @Column(name = "email") private String email; public Student() < >public Student(String firstName, String lastName, String email) < this.firstName = firstName; this.lastName = lastName; this.email = email; > public int getId() < return id; > public void setId(int id) < this.id = id; > public String getFirstName() < return firstName; > public void setFirstName(String firstName) < this.firstName = firstName; > public String getLastName() < return lastName; > public void setLastName(String lastName) < this.lastName = lastName; > public String getEmail() < return email; > public void setEmail(String email) < this.email = email; > @Override public String toString() < return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]"; > >

@Entity Annotation

@Entity public class Student

The entity name defaults to the name of the class. We can change its name using the name element.

@Entity(name="student") public class Student < // fields, getters and setters >

@Table

@Entity @Table(name = «student«) public class Student

We can also mention the schema using the schema element:

@Entity @Table(name="student", schema="college") public class Student < // fields, getters and setters >

Schema name helps to distinguish one set of tables from another.

If we do not use the @Table annotation, the name of the entity will be considered the name of the table.

@Id

@Entity @Table(name = "student") public class Student < @Id private int id; >

Each JPA entity must have a primary key that uniquely identifies it. The @Id annotation defines the primary key.

@GeneratedValue

@Entity @Table(name = "student") public class Student < @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; >

We can generate the identifiers in different ways which are specified by the @GeneratedValue annotation.

We can choose from four id generation strategies with the strategy element. The value can be AUTO, TABLE, SEQUENCE, or IDENTITY.

@Column

The @Column annotation is used to specify the mapping between a basic entity attribute and the database table column.:

@Entity @Table(name = "student") public class Student < @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private int id; @Column(name = "first_name", length=50, nullable=false, unique=false) private String firstName; // getters and setters >

The @Column annotation has many elements such as name, length, nullable, and unique.

The nullable element specifies whether the column is nullable or not, and the unique element specifies whether the column is unique.

If we don’t specify this annotation, the name of the field will be considered the name of the column in the table.

Rules or R equirements to define Entity Class

The Entity Class of the JPA 2.1 specification defines its requirements for an entity class. Applications that wish to remain portable across JPA providers should adhere to these requirements.

  • The entity class must be annotated with the javax.persistence.Entity annotation (or be denoted as such in XML mapping)
  • The entity class must have a public or protected no-argument constructor. It may define additional constructors as well.
  • The entity class must be a top-level class.
  • An enum or interface may not be designated as an entity.
  • The entity class must not be final. No methods or persistent instance variables of the entity class may be final.
  • If an entity instance is to be used remotely as a detached object, the entity class must implement the Serializable interface.
  • Both abstract and concrete classes can be entities. Entities may extend non-entity classes as well as entity classes, and non-entity classes may extend entity classes.
  • The persistent state of an entity is represented by instance variables, which may correspond to JavaBean-style properties. An instance variable must be directly accessed only from within the methods of the entity by the entity instance itself. The state of the entity is available to clients only through the entity’s accessor methods (getter/setter methods) or other business methods.

Источник

Оцените статью