Is Javax Persistence Deprecated in JPA?

Is Javax Persistence Deprecated in JPA?

What Is Java Persistence API(JPA)

Java persistence APIs are specialized sets of Java interfaces that are bound together with selected annotations to interact with databases. JPA represents how objects are persisted in the databases with the aid of object-relational mapping(ORM). An example of ORM is Hibernate

The way this technology work can be simplified by saying JPA is the mediator between objects created from a Java class(Entity) and breaking it down into values acceptable in the database (relationships, tables, and column).

We can't discuss JPA in Java without involving the use of Entity class. An entity class becomes an entity class with the usage of the @Entity annotation.

See below an example of an Entity class

@Entity
@Table(name="Users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(length = 128, nullable = false, unique = true)
    private String email;

    @Column(length = 64, nullable = false)
    private String password;

    @Column(name = "first_name", length= 45, nullable = false)
    private String firstName;

    @Column(name = "last_name", length= 45, nullable = false)
    private String lastName;

    //You can create getters and setters 

})

This code snippet shows how entity annotation is used to distinguish a normal class from an entity class. After creating an entity class, the object-relational mapping i.e. Hibernate takes hold of the entity class and identifies all the annotations i.e. @Entity,@table,@Id, and @Column e.t.c and maps it all to the database schema.

Note: We need to add both JPA dependency and MySQL dependency in our pom. xml file before these methods can work.

   <dependencies>
     <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.32</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

</dependencies>

Also, we need to add hibernate auto update to the application.properties file. this enables hibernate to make changes to the database table that was created. It carries out CRUD operations without the need of writing boilerplate codes.

server.port=8080
server.servlet.context-path=/testingEntity

spring.datasource.url=jdbc:mysql://localhost:3306/testingEntity
spring.datasource.username=username
spring.datasource.password=password

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

Java persistence APIs are specialized sets of Java interfaces that are bound together with selected annotations to interact with databases. They work hand in hand with object-oriented mapping like hibernate to establish a session between a database schema and entity class.

Why Did the Package Name Change to Jakarta Persistence?

Jakarta EE is an upgraded version of the Java version Enterprise editions(Java EE). Jakarta EE is previously known as Java Platforms Enterprise Edition (Java EE). While Java EE is an upgraded version of the J2EE version of Java which was owned and managed by Sun microSystem.

Sun Microsystem owned and managed the initial specification release. The J2EE started with version 1.2 in the year 1999. Some group of developers manage the platform for 6 years until it was taken over by Oracle at version 1.4. upon taking over the platform, Oracle renamed and managed the platform for years. The platform evolved into higher versions starting from Java EE 5 to Java EE 8. The platform was managed for 12 years before handing over to the ECLIPSE Foundation in the year 2017.

After this migration, the namespace changed from Java EE to Jakarta EE under Eclipse Foundation. And most of the package's names changed also. The changes reflect that some of the technology used previously in Java EE will no longer work well with the new technology that comes with Jakarta EE. Hence, that is why we have a change in package names like Javax. persistence changed into Jakarta. persistence in our projects.

Errors Importing Javax.Persistence in the Entity Class of the Spring-Boot Project [Fixed]

The error we have in the entity class whenever we import javax. persistence is because of the javax. persistence is not compatible with the version of JPA version.

so we have this error below.

Jpa 3.0 introduced the name change from javax. persistence to Jakarta. persistence, therefore in other to eliminate such error, one has to do the right import which is Jakarta. persistence. See the importation of Jakarta. persistence below and the error is gone.

Conclusion

The reason why we have an error in our project package is simply that we are using an older version of JPA or Hibernate version as our dependency. it is advised to add the compatible version of either of the dependency to eliminate such errors. In the image above, I was having an error until I changed my version of JPA from 2.0xxx to 3.0xxx.