问题描述
我正在尝试使用JPA和hibernate实现连接到MysqL数据库,但是它没有在数据库中创建表,即使在日志中似乎是在创建它。 这是我的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>academy.learnprogrammin</groupId>
<artifactId>hello-javaee8</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-ejb-client-bom</artifactId>
<type>pom</type>
<version>9.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.5.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/MysqL/mysql-connector-java -->
<dependency>
<groupId>MysqL</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.23</version>
</dependency>
</dependencies>
<build>
<finalName>hello-javaee8</finalName>
<plugins>
<!-- The WildFly plug-in deploys the WAR to a local JBoss EAP container -->
<!-- To use,run: mvn package wildfly:deploy -->
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.0.2.Final</version>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
和persistence.xml
<?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="studentsApp" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:MysqL://localhost:3306/students" />
<property name="javax.persistence.jdbc.user" value="students" />
<property name="javax.persistence.jdbc.password" value="admissssn" />
<property name="javax.persistence.jdbc.driver" value="com.MysqL.jdbc.Driver" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MysqL5InnoDBDialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
和学生实体类:
package com.learning.entity;
import javax.annotation.Generated;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "students_data")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
public Student() {
}
public Student(long id,String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
这是我的项目结构:
这是它记录创建students_data
表的服务器控制台:
12:02:13,074 INFO [stdout] (ServerService Thread Pool -- 205) Hibernate: create table students_data (id bigint not null auto_increment,name varchar(255),primary key (id)) engine=InnoDB
我正在使用wildfly mvn插件部署到JBoss EAP 7.0。
还有另一种奇怪的行为,我将数据库凭据更改为错误的名称(错误的用户名,密码),该错误不会给出错误,并且日志中仍然显示该表已创建。
解决方法
根据我在不同时间的经验提出的建议,
- 不能完全确定内部结构,但是有时取决于配置的设置方式,属性标记的长格式和短格式会造成一些混乱。
来自
<property name="hibernate.hbm2ddl.auto" value="create"/>
收件人:
<property name="hibernate.hbm2ddl.auto">create</property>
-
具有此属性,有时会使其工作
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
将值用作
a)创建:提供者将在应用程序部署时创建数据库工件。重新部署应用程序后,工件将保持不变。
b)拖放并创建:将删除数据库中的任何工件,并且提供程序将在部署时创建数据库工件。
更多详细信息:https://docs.oracle.com/javaee/7/tutorial/persistence-intro005.htm