gpt4 book ai didi

java - 组织.hibernate.exception.SQLGrammarException : Table 'XXX' doesn't exist

转载 作者:行者123 更新时间:2023-11-29 05:15:24 25 4
gpt4 key购买 nike

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

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

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 = "人")公共(public)类 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 ....

最佳答案

解决方案总结。

要让 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 >= 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

关于java - 组织.hibernate.exception.SQLGrammarException : Table 'XXX' doesn't exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33608237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com