I use JPA with a hibernate implementation. My project looks like this:
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 ....