实体表未使用JPA 2.1创建

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

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

有什么帮助吗? 🙂

以下是我的代码。

persistence.xml中

   jdbc/commonInfraDatasource false      

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

太阳resources.xml中

             

其他信息我启动Glassfish服务器并右键单击并部署我的企业应用程序。 这应该在我的数据库中创建userEntity表吗? 但事实并非如此。 在此处输入图像描述

无论如何,我设法解决了这个问题。

在创建表之前,您需要使用JPA执行某些操作。

例如…

 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package sessionBean; import javax.ejb.Stateless; import entity.userEntity; import javax.persistence.EntityManager; import javax.persistence.NoResultException; import javax.persistence.PersistenceContext; import javax.persistence.Query; /** * * @author Lawrence */ @Stateless public class userSessionBean implements userSessionBeanLocal { @PersistenceContext private EntityManager entityManager; @Override public userEntity getUser(String userName) { Query query = entityManager.createQuery("SELECT u FROM userEntity u WHERE u.userName = :inUserName"); query.setParameter("inUserName", userName); userEntity systemUser = null; try { systemUser = (userEntity) query.getSingleResult(); } catch (NoResultException ex) { ex.printStackTrace(); } return systemUser; } } 

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

  

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

persistence.xml文件中添加下一个属性。

  

万一有人感兴趣:似乎从JPA 2.1 / Glassfish 4.1开始,你需要在创建表之前的某个地方使用你的PU。 这两行应该足够了,即在EJB中:

 @PersistenceContext private EntityManager em; 

要么

 @PersistenceContext(unitName = "CommonInfrastructure-ejbPU") private EntityManager em; 

另请参阅我的答案: 如何在JavaDB上使用JPA和Java EE 7,Glassfish 4.1和Maven

对我来说,在持久性单元中添加一行就可以了。