EclipseLink/MySQL/Glassfish - Table 未创建,即使连接正常
EclipseLink/MySQL/Glassfish - Table is not created, even though connection works fine
我正在尝试设置一个基本的 JPA 应用程序,它在 mySQL 中创建一个简单的 table。
我使用 glassfish 4.1,我已经创建连接 pool/resource 并从管理面板成功地 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 + " ]";
}
}
我也试过把persistence.xml移到/WEB-INF下,还是没办法。
Glassfish 日志中没有错误。
我只是想让 eclipseLink 工作并自动生成 table.
正如 Dark 所提到的,我创建了 EntityManager,启动了一个事务并将一行保存到 table:
@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 才能创建 table。
我认为 EM 只与持久性上下文有关,与数据库相关的实体的生命周期,这意味着对象代表 table 的记录,而不是 table 本身,对吗?
我正在尝试设置一个基本的 JPA 应用程序,它在 mySQL 中创建一个简单的 table。
我使用 glassfish 4.1,我已经创建连接 pool/resource 并从管理面板成功地 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 + " ]";
}
}
我也试过把persistence.xml移到/WEB-INF下,还是没办法。
Glassfish 日志中没有错误。 我只是想让 eclipseLink 工作并自动生成 table.
正如 Dark 所提到的,我创建了 EntityManager,启动了一个事务并将一行保存到 table:
@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 才能创建 table。 我认为 EM 只与持久性上下文有关,与数据库相关的实体的生命周期,这意味着对象代表 table 的记录,而不是 table 本身,对吗?