@Entity
@Table(name="tbl_product")
public class ProductEntity
{
@Id
@Column(name="product_id")
private Integer productId;
@Column(name="product_nm")
private String productName;
private Integer quantity;
private Double unitPrice;
@CreationTimestamp
@Column(name="created_on")
private LocalDateTime createdOn;
@Column(name="updated_on")
@UpdateTimestamp
private LocalDateTime updatedOn;
public interface ProductDao
{
ProductEntity saveProduct(ProductEntity product);
ProductEntity loadProductById(Integer productId);
ProductEntity updateProductById(Integer productId, Double newUnit_Price);
ProductEntity deleteProductById(Integer productId);
}
public class ProductDaoImpl implements ProductDao
{
private EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
@Override
public ProductEntity saveProduct(ProductEntity product) {
EntityManager entityManager=factory.createEntityManager();
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
try {
entityManager.persist(product);
tx.commit();
System.out.println("Product is inserted into database");
}
catch (Exception e){
tx.rollback();
System.out.println("Error in inserting product");
e.printStackTrace();
}
finally {
entityManager.close();
}
return product;
}
@Override
public ProductEntity loadProductById(Integer productId) {
EntityManager entityManager=factory.createEntityManager();
ProductEntity p = entityManager.find(ProductEntity.class,productId);
entityManager.close();
return p;
}
@Override
public ProductEntity updateProductById(Integer productId, Double newUnitPrice)
{
EntityManager entityManager = factory.createEntityManager();
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
try {
ProductEntity product = entityManager.find(ProductEntity.class, productId);
if (product != null) {
product.setUnitPrice(newUnitPrice);
entityManager.merge(product);
tx.commit();
return product;
} else {
System.out.println("Product not found for update");
}
} catch (Exception e) {
tx.rollback();
System.out.println("Error updating product");
e.printStackTrace();
} finally {
entityManager.close();
}
return null;
}
@Override
public ProductEntity deleteProductById(Integer productId) {
EntityManager entityManager = factory.createEntityManager();
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
try {
ProductEntity product = entityManager.find(ProductEntity.class, productId);
if (product != null) {
entityManager.remove(product);
tx.commit();
return product;
} else {
System.out.println("Product not found for deletion");
}
} catch (Exception e) {
tx.rollback();
System.out.println("Error deleting product");
e.printStackTrace();
} finally {
entityManager.close();
}
return null;
}
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.durgasoft.app01.beans.ProductEntity</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/durgadb" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
public class Test {
public static void main(String[] args) {
ProductDao productDao = new ProductDaoImpl();
// Create a product
ProductEntity product = new ProductEntity();
product.setProductName("Table");
product.setQuantity(10);
product.setUnitPrice(25.5);
// Save the product
productDao.saveProduct(product);
// Load the product by ID
ProductEntity loadedProduct = productDao.loadProductById(product.getProductId());
System.out.println(loadedProduct);
// Update the product
loadedProduct.setUnitPrice(30.0);
productDao.updateProductById(loadedProduct.getProductId(), 30.0);
// Load the product again to verify update
loadedProduct = productDao.loadProductById(product.getProductId());
System.out.println(loadedProduct);
// Delete the product
productDao.deleteProductById(product.getProductId());
// Try to load the deleted product (should be null)
loadedProduct = productDao.loadProductById(product.getProductId());
System.out.println(loadedProduct);
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>app01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>app01</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.6.15.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-core -->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.6.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
</project>
Still getting error
jakarta.persistence.spi.PersistenceProviderResolverHolder$DefaultPersistenceProviderResolver log
WARNING: jakarta.persistence.spi::No valid providers found.
Exception in thread “main” jakarta.persistence.PersistenceException: No Persistence provider for EntityManager named test
at jakarta.persistence.Persistence.createEntityManagerFactory(Persistence.java:86)
at jakarta.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at com.durgasoft.app01.dao.ProductDaoImpl.(ProductDaoImpl.java:8)
at com.durgasoft.app01.test.Test.main(Test.java:9)