prima implementazione di persistence usando eclipselink e database integrato in glassfish.
This commit is contained in:
17
META-INF/persistence.xml
Normal file
17
META-INF/persistence.xml
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence 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"
|
||||
version="2.1">
|
||||
|
||||
|
||||
<persistence-unit name="test3" transaction-type="JTA">
|
||||
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
|
||||
<jta-data-source>jdbc/__default</jta-data-source>
|
||||
<properties>
|
||||
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
|
||||
<property name="eclipselink.logging.level" value="FINE" />
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
</persistence>
|
||||
73
servlet/Servlettest3.java
Normal file
73
servlet/Servlettest3.java
Normal file
@@ -0,0 +1,73 @@
|
||||
package servlet;
|
||||
|
||||
import test2_constraints_validation.*;
|
||||
import test3_persistence.store_class;
|
||||
import test3_persistence.test_class;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.transaction.*;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
import javax.validation.executable.ExecutableValidator;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 25/04/2015.
|
||||
*/
|
||||
|
||||
@WebServlet(name = "Servlettest3")
|
||||
public class Servlettest3 extends HttpServlet {
|
||||
|
||||
|
||||
@Inject
|
||||
store_class s;
|
||||
|
||||
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. */
|
||||
|
||||
out.println("<!DOCTYPE html>");
|
||||
out.println("<html>");
|
||||
out.println("<head>");
|
||||
out.println("<title>Servlet xg</title>");
|
||||
out.println("</head>");
|
||||
out.println("<body>");
|
||||
out.println("<h1>Servlet xg at " + request.getContextPath() + "</h1>");
|
||||
|
||||
|
||||
s.store_class(new test_class() );
|
||||
|
||||
|
||||
|
||||
out.println("</body>");
|
||||
out.println("</html>");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
processRequest(request, response);
|
||||
|
||||
}
|
||||
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
processRequest(request, response);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -5,14 +5,14 @@ package test1_cdi;
|
||||
*/
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Priority;
|
||||
//import javax.annotation.Priority;
|
||||
import javax.inject.Inject;
|
||||
import javax.interceptor.*;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Interceptor
|
||||
@GeneralInterceptorA
|
||||
@Priority(200)
|
||||
//@Priority(200)
|
||||
|
||||
public class GeneralInterceptor {
|
||||
|
||||
|
||||
28
test3_persistence/store_class.java
Normal file
28
test3_persistence/store_class.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package test3_persistence;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 04/05/2015.
|
||||
*/
|
||||
|
||||
@Stateless
|
||||
public class store_class {
|
||||
|
||||
@PersistenceContext(unitName = "test3")
|
||||
private EntityManager em;
|
||||
|
||||
|
||||
public void store_class(test_class t){
|
||||
|
||||
em.persist(t);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
41
test3_persistence/test_class.java
Normal file
41
test3_persistence/test_class.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package test3_persistence;
|
||||
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.NamedQuery;
|
||||
|
||||
@Entity
|
||||
@NamedQuery(name = "findxg", query = "SELECT b FROM test_class b WHERE b.title ='xg'")
|
||||
public class test_class {
|
||||
|
||||
@Id @GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@PostConstruct
|
||||
public void ps (){
|
||||
title = "xg";
|
||||
}
|
||||
|
||||
private String title;
|
||||
|
||||
private Double price;
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user