java-无法使用MySQL从GlassFish 5服务器中的模型@Entity类生成数据库架构

前端之家收集整理的这篇文章主要介绍了java-无法使用MySQL从GlassFish 5服务器中的模型@Entity类生成数据库架构 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

您好,我无法从Java模型@Entity类自动生成数据库表.我已经尝试了几乎所有东西.

我首先在Intellij Idea中创建了maven项目,我下载了我正在使用的JavaEE 8 api实现.

我正在使用Glassfish 5.0服务器作为java ee 8实现提供程序和MySQL数据库.

我的项目结构:

enter image description here

我的pom.xml看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.zerg.web</groupId>
<artifactId>PlanYourTime</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
    <!-- https://mvnrepository.com/artifact/javax/javaee-api -->
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>8.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

我的模型@Entity类希望在MysqL数据库生成表.

package model;

import javax.persistence.*;
@Entity
@TableGenerator(name = "USER")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USERID")
private int userId;
@Column(name = "USERNAME")
private String username;
@Column(name = "PASSWORD")
private String password;
@Column(name = "EMAIL")
private String email;

public User(String username,String password,String email) {
    this.username = username;
    this.password = password;
    this.email = email;
}

public User() {
}

public int getUserId() {
    return userId;
}

public void setUserId(int userId) {
    this.userId = userId;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}
}

我的persistence.xml文件src / main / java / Meta-INF /如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
         version="2.2">

<persistence-unit name="myPu" transaction-type="JTA">
    <jta-data-source>jdbc/PlanYourTime</jta-data-source>
    <properties>
        <property name="javax.persistence.schema-generation.database.action" value="create"/>
        <property name="javax.persistence.schema-generation.create-source" value="Metadata"/>
        <property name="javax.persistence.schema-generation.drop-source" value="Metadata"/>
        <property name="javax.persistence.sql-load-script-source" value="Meta-INF/load-script.sql"/>
        <property name="eclipselink.logging.level" value="FINE"/>
        <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
    </properties>
</persistence-unit>

并且在我的glassfish服务器管理页面中,我还添加
Jdbc资源和jdbc连接池如下所示:

enter image description here

enter image description here

我的战争神器结构看起来像我部署的IntellijIdea
到glassfish 5.0服务器:

enter image description here

当我检查server.log中的glassfish时,我没有任何错误.

我将非常感谢您的每一次帮助.

最佳答案
您创建/使用过实体经理吗?如果不是,则持久性模块未启动.使用实体管理器进行插入,或尝试:

Use eclipselink.deploy-on-startup to configure deployment on startup
(at the creation of the EntityManagerFactory) instead of occurring the
first time an EntityManager is created.

https://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/p_deploy_on_startup.htm#delayonstartup

猜你在找的Java相关文章