0

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.

enter image description here

MWiesner
  • 8,868
  • 11
  • 36
  • 70
Gambit
  • 11
  • 1
  • 4
  • You aren't using an in-memory database. So why should it be created? You are using a tcp connection instead of in-memory. According to your JDBC url. – M. Deinum Jan 06 '22 at 15:23
  • Sorry, my bad. It is used in server mode, thats why tcp is there. – Gambit Jan 06 '22 at 15:39
  • And why do you think the tables aren't generated? – M. Deinum Jan 06 '22 at 18:52
  • I checked in dB for the table "EMPLOYEE_DATA" and it was not created. – Gambit Jan 07 '22 at 07:00
  • 2
    You checked when? What do you think `create-drop` does? It will drop de tables after the application stops. So if you check **after** your application stopped there will be nothing there. If there really wouldn't be a table your application would crash on the `persist` call. – M. Deinum Jan 07 '22 at 08:04
  • Does this answer your question? [How does spring.jpa.hibernate.ddl-auto property exactly work in Spring?](https://stackoverflow.com/questions/42135114/how-does-spring-jpa-hibernate-ddl-auto-property-exactly-work-in-spring) – MWiesner Jan 09 '22 at 09:32

0 Answers0