1

I use JPA with a hibernate implementation. My project looks like this:

enter image description here

The dependency in my pom.xml

<dependencies>
    <!-- JUNIT -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>

    <!-- MYSQL CONNECTOR -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.9</version>
    </dependency>

    <!-- HIBERNATE -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.1.6.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.1.6.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.5.5-Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.common</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>4.0.4.Final</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.7</version>
    </dependency>
</dependencies>

The persistence.xml

<persistence-unit name="manager" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>ma.mahmoud.jpa.Person</class>
    <properties>
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/testjpa" />
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
        <property name="hibernate.connection.username" value="root" />
        <property name="hibernate.connection.password" value="root" />
        <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        <property name="hibernate.connection.charSet" value="UTF-8" />
        <property name="hibernate.id.new_generator_mappings" value="false" />
        <property name="hibernate.show_sql" value="true" />
    </properties>
</persistence-unit>

The problem is when I run the main method I get this error:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'testjpa.person' doesn't exist

I know the table does not exist in the database, but when I run the app I would like the table to be created in the database.

The main method:

    public static void main(String[] argv) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager");
    EntityManager em = emf.createEntityManager();

    EntityTransaction transac = em.getTransaction();

    transac.begin();
    em.persist(new Person("mahmoud", "lotfi", "morroco"));
    transac.commit();

    em.close();
    emf.close();
}

The entity Person:

@Entity

@Table(name = "Person") public class Person implements Serializable {

private static final long serialVersionUID = 4717398914745522714L;

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

@Column(name = "last_name", length = 50, nullable = false, unique = false, updatable = true)
private String lastname;

@Column(name = "first_name", length = 50, nullable = false, unique = false, updatable = true)
private String firstname;

@Column(name = "country", length = 50, nullable = true, unique = false, updatable = true)
private String country;

public Person(String lastname, String firstname, String country) {
    super();
    this.lastname = lastname;
    this.firstname = firstname;
    this.country = country;
}

public Person() {
}

// GETTERS AND SETTERS ....
Cœur
  • 37,241
  • 25
  • 195
  • 267
Mahmoud
  • 325
  • 10
  • 25

1 Answers1

3

Summary of the solution.

To let Hibernate create the tables during initialisation in the persistence.xml the property hibernate.hbm2ddl.auto must be defined. (see: https://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch03.html#configuration-transaction-properties)

<property name="hibernate.hbm2ddl.auto" value="create-drop" />

For a MySQL >= 5.x database this doesn't work if the property hibernate.dialect is set as

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" 

the property must be set as

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" 

Information taken from Hibernate does not create tables automatically

Community
  • 1
  • 1
SubOptimal
  • 22,518
  • 3
  • 53
  • 69