I created sample java project and try to save data with JPA. able to connect with the database as well as able to create the related database table(named laptop
) as well as. But when i try to save data(Laptop
object). Those data not in the database table(in laptop
table)
persistence.xml file
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="laptop_persist">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/device_store"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="19940306"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.CharSet" value="utf8"/>
<property name="hibernate.connection.characterEncoding" value="utf8"/>
<property name="hibernate.connection.useUnicode" value="true"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
Laptop.java 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;
import java.util.logging.Logger;
@Entity
@Table(name = "laptop")
public class Laptop implements Device {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long lapId;
@Column(name = "brand_name")
private String brandName;
@Column(name = "model_name")
private String modelName;
@Column(name = "serial_number")
private String serialNumber;
public Laptop(String brandName, String modelName, String serialNumber) {
this.brandName = brandName;
this.modelName = modelName;
this.serialNumber = serialNumber;
}
public Laptop() {
}
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
public String getModelName() {
return modelName;
}
public void setModelName(String modelName) {
this.modelName = modelName;
}
public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}
public Long getLapId() {
return lapId;
}
public void start() {
System.out.println("Starting the" + serialNumber + " laptop...");
}
public void restart() {
System.out.println("restarting the laptop...");
}
public void shutdown() {
System.out.println("Shutting down the laptop...");
}
}
Main.java class
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class Main {
public static void main(String[] args) {
Laptop laptop = new Laptop("aaa","bbb","cccc");
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("laptop_persist");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.persist(laptop);
entityManager.close();
entityManagerFactory.close();
}
}
pom.xml file
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>pc-hi</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Archetype - pc-hi</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.5.7.Final</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
As I mentioned in above, the laptop
table has created. But unable to save the data
As you turn off auto-commit (i.e hibernate.connection.autocommit=false
) which is a good practise for changing the data , you have to explicitly declare when to start and commit the transaction.
Change to the following should fix the issue :
entityManager.getTransaction().begin();
entityManager.persist(laptop);
entityManager.getTransaction().commit();