加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 19:26:28 所属栏目:Java 来源:网络整理
导读:您好,我无法从Java模型@Entity类自动生成数据库表.我已经尝试了几乎所有东西. 我首先在Intellij Idea中创建了maven项目,我下载了我正在使用的JavaEE 8 api实现. 我正在使用Glassfish 5.0服务器作为java ee 8实现提供程序和MySQL数据库. 我的项目结构: 我的p

您好,我无法从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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读