java - 连接到 mySQL、JPA、Java EE 和 JBoss
问题描述
我正在尝试使用 JPA 和休眠实现连接到 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) drop-and-create:数据库中的任何工件都将被删除,并且提供者将在部署时创建数据库工件。
更多细节:https ://docs.oracle.com/javaee/7/tutorial/persistence-intro005.htm
推荐阅读
- python - 如何在python烧瓶中获取具有本地时区的模板文件的修改日期
- swiftui - SwiftUI - 如何正确重新加载 @ObservableObject 对象
- spacy - 在多个 GPU 上运行 spacy 以预测 ner
- apache - 获取 405 - 通过 BlueImp FileUpload 上传图像时某些请求上不允许的方法
- jwt - Auth0 中的过期 JWT
- bash - 捕获嵌入在 bash 函数中的对话框调用的结果
- amazon-web-services - 访问 lambda (Python) 代码中的 AWS 阶跃函数错误
- java - 无效的标头签名;读取 0x7061632C65746144,预期 0xE11AB1A1E011CEG0 - 您的文件似乎不是有效的 OLE2 文档]]
- css - 在 Framer Motion 退出动画完成之前,在路径更改时移除 CSS 模块
- javascript - 保留 MomentJS 日期的原始格式