简体   繁体   English

连接到 mySQL、JPA、Java EE 和 JBoss

[英]Connect to mySQL, JPA, Java EE & JBoss

I'm trying to connect to mySQL database using JPA and hibernate implementation but it doesn't create the tables in the database even though in the logs it appears it's creating it.我正在尝试使用 JPA 和 hibernate 实现连接到 mySQL 数据库,但它不会在数据库中创建表,即使在日志中它似乎正在创建它。 Here is my pom.xml这是我的 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>

And persistence.xml和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>

And Student entity class:和 Student 实体类:

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

And this is my project structure:这是我的项目结构:

在此处输入图片说明

Here's the server console where it logs creating the students_data table:这是记录创建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

I'm deploying to JBoss EAP 7.0 with wildfly mvn plugin.我正在使用 wildfly mvn 插件部署到 JBoss EAP 7.0。

There is another strange behaviour that I changed the database credentials to something wrong (wrong username, password) it doesn't give error and logs still saying the table is created.还有另一种奇怪的行为,我将数据库凭据更改为错误(错误的用户名、密码),它没有给出错误,并且日志仍然说表已创建。

Couple of suggestions from my experience at different times,我在不同时期的经验的一些建议,

  1. Not completely sure of the internals, but at times depending on how the configuration is set, the long form and the short form of the property tag makes some mess.不完全确定内部结构,但有时取决于配置的设置方式,属性标签的长格式和短格式会造成一些混乱。

From

    <property name="hibernate.hbm2ddl.auto" value="create"/>

To:到:

     <property name="hibernate.hbm2ddl.auto">create</property>
  1. Having this property, makes it working at times,拥有这个属性,让它有时工作,

     <property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />

Use values as使用值作为

a) create: The provider will create the database artifacts on application deployment. a) 创建:提供者将在应用程序部署时创建数据库工件。 The artifacts will remain unchanged after application redeployment.应用程序重新部署后,工件将保持不变。

b) drop-and-create: Any artifacts in the database will be deleted, and the provider will create the database artifacts on deployment. b) drop-and-create:数据库中的任何工件都将被删除,提供者将在部署时创建数据库工件。

More details: https://docs.oracle.com/javaee/7/tutorial/persistence-intro005.htm更多详情: https : //docs.oracle.com/javaee/7/tutorial/persistence-intro005.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM