Reputation:
I am trying to setup a basic JPA application that creates a simple table in mySQL.
I use glassfish 4.1, I have created connection pool/resource and pinged successfully the database from administration panel.
I created a web application project which contains just one simple entity and a persistence.xml under src/META-INF folder.
<?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>
And the entity file,
@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 + " ]";
}
}
I also tried moving persistence.xml under /WEB-INF, still no cure.
There is no error in Glassfish logs. I am just trying to get eclipseLink working and automatically generate the table.
Upvotes: 1
Views: 173
Reputation:
As Dark mentioned, I created EntityManager, started a transaction and persisted a row to the 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();
}
}
But would like to know why EntityManager is needed in order for the table to be created. I thought that EM has to do only with Persistence Context, the lifecycle of Entities related with the database, which means objects representing records to a table, not the table itself, right?
Upvotes: 1