java - 实体表不是使用 JPA 2.1 创建的

标签 java jpa netbeans ejb-3.0 jpa-2.1

我在 Netbeans 中使用 JPA 2.1 来创建我的实体。如果我的数据库没有表,那么它应该从实体创建表。

当我部署和运行我的企业应用程序时,userEntity 表没有出现在我的 mySQL 数据库中。

这里有什么帮助吗? :)

下面是我的代码。

持久性.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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_1.xsd">
  <persistence-unit name="CommonInfrastructure-ejbPU" transaction-type="JTA">
    <jta-data-source>jdbc/commonInfraDatasource</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
    </properties>
  </persistence-unit>
</persistence>

用户实体.java

package entity;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class userEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long systemUserId;
    private String userName;
    private String password;
    private String email;
    private int activateStatus;
    private String accessGroup;
    private int lockOutStatus;



    @Override
    public int hashCode() {
        int hash = 0;
        hash += (getSystemUserId() != null ? getSystemUserId().hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the systemUserId fields are not set
        if (!(object instanceof userEntity)) {
            return false;
        }
        userEntity other = (userEntity) object;
        if ((this.getSystemUserId() == null && other.getSystemUserId() != null) || (this.getSystemUserId() != null && !this.systemUserId.equals(other.systemUserId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.userEntity[id=" + getSystemUserId() + "]";
    }

    /**
     * @return the systemUserId
     */
    public Long getSystemUserId() {
        return systemUserId;
    }

    /**
     * @param systemUserId the systemUserId to set
     */
    public void setSystemUserId(Long systemUserId) {
        this.systemUserId = systemUserId;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the activateStatus
     */
    public int getActivateStatus() {
        return activateStatus;
    }

    /**
     * @param activateStatus the activateStatus to set
     */
    public void setActivateStatus(int activateStatus) {
        this.activateStatus = activateStatus;
    }

    /**
     * @return the accessGroup
     */
    public String getAccessGroup() {
        return accessGroup;
    }

    /**
     * @param accessGroup the accessGroup to set
     */
    public void setAccessGroup(String accessGroup) {
        this.accessGroup = accessGroup;
    }

    /**
     * @return the lockOutStatus
     */
    public int getLockOutStatus() {
        return lockOutStatus;
    }

    /**
     * @param lockOutStatus the lockOutStatus to set
     */
    public void setLockOutStatus(int lockOutStatus) {
        this.lockOutStatus = lockOutStatus;
    }
}

太阳资源.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Resource Definitions //EN" "http://www.sun.com/software/appserver/dtds/sun-resources_1_3.dtd">
<resources>
  <jdbc-resource enabled="true" jndi-name="jdbc/commonInfraDatasource" object-type="user" pool-name="CommonInfraConnectionPool">
    <description/>
  </jdbc-resource>
  <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="CommonInfraConnectionPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
    <property name="URL" value="jdbc:XXXXXXXXXX"/>
    <property name="User" value="XXXXXXXXX"/>
    <property name="Password" value="XXXXXXXXX"/>
  </jdbc-connection-pool>
</resources>

附加信息 我启动我的 Glassfish Server 并右键单击并部署我的企业应用程序。 这应该在我的数据库中创建 userEntity 表吧? 然而,它没有。 enter image description here

最佳答案

要强制 EclipseLink 在部署期间创建表,请添加:

<property name="eclipselink.deploy-on-startup" value="true" />

到您的 persistence.xml。默认情况下,在需要时创建表,通常是在第一次从应用程序访问 EMF 时创建。 此行为在 JPA 2.1 规范的第 9.4 节中定义。

关于java - 实体表不是使用 JPA 2.1 创建的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25489359/

相关文章:

java - Play/Ebean 未获取 ManyToOne 相关对象的所有属性

java - JPA OneToMany 集合返回为空

java - Liferay 不捕获 portlet 中的表单,也不进行处理

java - 原始类型类的用途/目的是什么?

java - Android:将 View 添加到网格 RecyclerView

java - thymeleaf (th :action) not working when rendering long html

java - 将分离的 JPA 实体与 @version 字段合并

java - 仅当记录因数据修改而更新时才更新日期

java - Netbeans 与 JDeveloper 中的 Swing 开发

java - 我在导入最近制作的类(class)时遇到问题