2017-07-01 31 views
0

我想設置一個基本的JPA應用程序,它在mySQL中創建一個簡單的表。EclipseLink/MySQL/Glassfish - 即使連接工作正常,表格也不會創建

我使用glassfish 4.1,我創建了連接池/資源併成功從管理面板中ping數據庫。

我創建了一個web應用程序項目,其中包含一個簡單的實體和src/META-INF文件夾下的persistence.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="DataPersistencePU" transaction-type="JTA"> 
<jta-data-source>mysqlresource</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> 

與實體文件,

@Entity 
public class DataProvider implements Serializable { 

private static final long serialVersionUID = 1L; 
@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
private Long id; 

public Long getId() { 
    return id; 
} 

public void setId(Long id) { 
    this.id = id; 
} 

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

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

@Override 
public String toString() { 
    return "Entity.DataProvider[ id=" + id + " ]"; 
} 

}

我也試過在/ WEB-INF移動persistence.xml中,仍無法治癒。

Glassfish日誌中沒有錯誤。 我只是想讓eclipseLink工作並自動生成表格。

+2

好的問這裏明顯。你是否在應用程序的某個地方創建了「EntityManager」? – Ranjeet

回答

0

暗提到的,我創建一個EntityManager,開始了交易,並堅持一排表:

@WebServlet(name = "TestUsertServlet", urlPatterns = {"/TestUsertServlet"}) 
@PersistenceContext(
    unitName = "DataPersistencePU") 
@TransactionAttribute(REQUIRED) 
public class TestUsertServlet extends HttpServlet { 

@Resource 
private javax.transaction.UserTransaction utx; 

@PersistenceContext 
EntityManager em; 

protected void processRequest(HttpServletRequest request,  HttpServletResponse response) 
     throws ServletException, IOException { 
    response.setContentType("text/html;charset=UTF-8"); 
    try (PrintWriter out = response.getWriter()) { 
     /* TODO output your page here. You may use following sample code. */ 
     out.println("<!DOCTYPE html>"); 
     out.println("<html>"); 
     out.println("<head>"); 
     out.println("<title>Servlet TestUsertServlet</title>"); 
     out.println("</head>"); 
     out.println("<body>"); 
     out.println("<h1>Servlet TestUsertServlet at " + request.getContextPath() + "</h1>"); 
     out.println("</body>"); 
     out.println("</html>"); 
    } 
} 

@Override 

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    response.setContentType("text/html;charset=UTF-8"); 
    try { 
     utx.begin(); 
     TestUser testUser = new TestUser(); 
     em.persist(testUser); 
     utx.commit(); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

但想知道爲什麼需要EntityManager的爲了要創建的表。 我認爲EM只能使用持久性上下文,實體與數據庫相關的生命週期,這意味着對象表示記錄到表,而不是表本身,對嗎?

+1

EclipseLink懶惰地部署持久性單元以避免在類路徑中找到許多可能會被忽略的開銷。如果要將其更改爲預先部署,請參見https://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/p_deploy_on_startup.htm#delayonstartup。 – Chris

相關問題