org.hibernate.exception.SQLGrammarException:表'XXX'不存在

时间:2015-11-09 11:35:34

标签: java mysql hibernate jpa

我将JPA与hibernate实现一起使用。 我的项目看起来像这样:

enter image description here

我的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>

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>

问题是当我运行main方法时出现此错误:

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

我知道数据库中不存在该表,但是当我运行应用程序时,我希望在数据库中创建该表。

主要方法:

    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();
}

实体人:

@Entity

@Table(name =“Person”) public class Person实现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 ....

1 个答案:

答案 0 :(得分:3)

解决方案摘要。

要让Hibernatepersistence.xml初始化期间创建表格,必须定义属性hibernate.hbm2ddl.auto。 (见: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" />

对于MySQL&gt; = 5.x数据库,如果属性hibernate.dialect设置为

,则此方法无效
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" 

该属性必须设置为

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

Hibernate does not create tables automatically

获取的信息