I am working on a tutorial to learn JPA and Hibernate. The aim is to create a table in DB "EMPLOYEE_DATA" and insert the values. I am not sure where I am doing it wrong but it is not creating the table. The tutorial I am following has a table created with values inserted in columns. Unfortunately, the code isn't provided by the instructor and
The project pom.xml is
<modelVersion>4.0.0</modelVersion>
<groupId>io.javabrains</groupId>
<artifactId>jpa-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.29.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>compile</scope>
</dependency>
</dependencies>
and persistence.xml file is,
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
<persistence-unit name="myApp" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost:8082/~test" />
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="sa" />
<property name="show_sql" value="true" />
<property name="format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" />
</properties>
</persistence-unit>
</persistence>
I have got an Employee class and
package io.javabrains;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "EMPLOYEE_DATA")
public class Employee {
@Id
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
and JPAStarter main as
package io.javabrains;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class JpaStarterMain {
public static void main(String[] args) {
Employee employee = new Employee();
employee.setId(1);
employee.setName("Foo Bar");
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("myApp");
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
entityManager.persist(employee);
transaction.commit();
}
}
and this is what I get when I run the project. I am not sure what am I doing wrong which is stopping to create the table.