aggiunto mapping relazione, entity manager da container, listener, callbacks, query
This commit is contained in:
@@ -22,7 +22,9 @@ import javax.validation.executable.ExecutableValidator;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -50,7 +52,71 @@ public class Servlettest3 extends HttpServlet {
|
||||
out.println("<h1>Servlet xg at " + request.getContextPath() + "</h1>");
|
||||
|
||||
|
||||
s.store_class(new test_class() );
|
||||
s.store_class(new test_class());
|
||||
out.println(s.find(1L).getTitle());
|
||||
//s.fake_remove(1L);
|
||||
s.remove_and_persist(1L);
|
||||
|
||||
test_class t = s.find(1L);
|
||||
t.setTitle("alibaa");
|
||||
out.println(s.find(1L).getTitle()); // database isn't updated
|
||||
out.println(s.contains(s.find(1L))); // it's false
|
||||
s.merge(t);
|
||||
out.println(s.find(1L).getTitle());// database is updated! All OK!
|
||||
|
||||
//test_class q = (test_class) s.namedquery_single("findtest_class") ;
|
||||
//out.println(q.getTitle());
|
||||
|
||||
/*
|
||||
List<Object> q = s.namedquery_multiple("findtest_class") ;
|
||||
Iterator<Object> it = q.iterator();
|
||||
while (it.hasNext()){
|
||||
out.println(((test_class) it.next()).getTitle());
|
||||
}
|
||||
|
||||
*/
|
||||
out.println("<br>");
|
||||
|
||||
List<Object> q2 = s.dynquery_multiple("SELECT t FROM test_class t") ;
|
||||
Iterator<Object> it = q2.iterator();
|
||||
while (it.hasNext()){
|
||||
out.println(((test_class) it.next()).getTitle());
|
||||
}
|
||||
|
||||
out.println("<br>");
|
||||
|
||||
HashMap<String,String> h = new HashMap<String,String>();
|
||||
h.put("fname", "alibaa");
|
||||
// h.put(...);
|
||||
List<Object> q3 = s.dynquery_multiple_string("SELECT t FROM test_class t where t.title = :fname", h) ;
|
||||
Iterator<Object> it2 = q3.iterator();
|
||||
while (it2.hasNext()){
|
||||
out.println(((test_class) it2.next()).getTitle());
|
||||
}
|
||||
|
||||
out.println("<br>");
|
||||
|
||||
HashMap<Integer,String> h2 = new HashMap<Integer,String>();
|
||||
h2.put(1,"alibaa");
|
||||
// h2.put(...);
|
||||
List<Object> q4 = s.dynquery_multiple_integer("SELECT t FROM test_class t where t.title = ?1", h2) ;
|
||||
Iterator<Object> it3 = q4.iterator();
|
||||
while (it3.hasNext()){
|
||||
out.println(((test_class) it3.next()).getTitle());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
30
test3_persistence/Persistent_Listener1.java
Normal file
30
test3_persistence/Persistent_Listener1.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package test3_persistence;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.PostLoad;
|
||||
import javax.persistence.PostPersist;
|
||||
import javax.persistence.PostUpdate;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 10/05/2015.
|
||||
*/
|
||||
public class Persistent_Listener1 {
|
||||
|
||||
// @Inject not working
|
||||
Logger l = Logger.getLogger("xg");
|
||||
|
||||
|
||||
@PostLoad
|
||||
@PostPersist
|
||||
@PostUpdate
|
||||
|
||||
public void method1 (Object entity) {
|
||||
|
||||
l.info("persistence listener : postload, postpersist,postupdate");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
27
test3_persistence/Persistent_Listener2.java
Normal file
27
test3_persistence/Persistent_Listener2.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package test3_persistence;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.*;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 10/05/2015.
|
||||
*/
|
||||
public class Persistent_Listener2 {
|
||||
|
||||
// @Inject not working
|
||||
Logger l = Logger.getLogger("xg");
|
||||
|
||||
|
||||
@PrePersist
|
||||
@PreUpdate
|
||||
|
||||
public void method1 (Object entity) {
|
||||
|
||||
l.info("persistence listener 2 : PrePersist, PreUpdate");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -2,8 +2,14 @@ package test3_persistence;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.Query;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 04/05/2015.
|
||||
@@ -12,6 +18,9 @@ import javax.persistence.PersistenceContext;
|
||||
@Stateless
|
||||
public class store_class {
|
||||
|
||||
@Inject
|
||||
Logger l;
|
||||
|
||||
@PersistenceContext(unitName = "test3")
|
||||
private EntityManager em;
|
||||
|
||||
@@ -22,6 +31,80 @@ public class store_class {
|
||||
|
||||
}
|
||||
|
||||
public test_class find (Long n){
|
||||
//return em.find(test_class.class,n); // null if fail
|
||||
//or
|
||||
return em.getReference(test_class.class, n); // EntityNotFoundException if fail
|
||||
}
|
||||
|
||||
|
||||
public void fake_remove (Long n){
|
||||
|
||||
test_class t = find(n);
|
||||
t.setXlist(null); // works because orphanremoval is true
|
||||
|
||||
}
|
||||
|
||||
public void remove_and_persist (Long n){
|
||||
test_class t = find(n);
|
||||
em.remove(t); // delete t object and connected objects because cascading is set to CascadeType.ALL
|
||||
em.persist(t); // persist work, objects are in memory, all is safe again
|
||||
}
|
||||
|
||||
public boolean contains (test_class t){
|
||||
return em.contains(t);
|
||||
}
|
||||
|
||||
public void merge (test_class t){
|
||||
//em.persist(t); error, object already present in database
|
||||
em.merge(t);
|
||||
}
|
||||
|
||||
|
||||
public Object namedquery_single (String q){
|
||||
Query Q = em.createNamedQuery(q);
|
||||
return Q.getSingleResult(); // if no result NoResultException
|
||||
// if more NonUniqueResultException
|
||||
}
|
||||
|
||||
public List<Object> namedquery_multiple (String q){
|
||||
Query Q = em.createNamedQuery(q);
|
||||
return Q.getResultList(); // if no result NoResultException
|
||||
|
||||
}
|
||||
|
||||
public List<Object> dynquery_multiple (String q){
|
||||
Query Q = em.createQuery(q);
|
||||
return Q.getResultList(); // if no result NoResultException
|
||||
|
||||
}
|
||||
|
||||
public List<Object> dynquery_multiple_string (String q, HashMap<String,String> h){
|
||||
Query Q = em.createQuery(q);
|
||||
Iterator<String> it = h.keySet().iterator();
|
||||
while (it.hasNext()){
|
||||
String next = it.next();
|
||||
l.info(next);
|
||||
l.info(h.get(next));
|
||||
Q.setParameter(next,h.get(next));
|
||||
}
|
||||
return Q.getResultList(); // if no result NoResultException
|
||||
|
||||
}
|
||||
|
||||
|
||||
public List<Object> dynquery_multiple_integer (String q, HashMap<Integer,String> h){
|
||||
Query Q = em.createQuery(q);
|
||||
Iterator<Integer> it = h.keySet().iterator();
|
||||
while (it.hasNext()){
|
||||
Integer next = it.next();
|
||||
l.info(next.toString());
|
||||
l.info(h.get(next));
|
||||
Q.setParameter(next,h.get(next));
|
||||
}
|
||||
return Q.getResultList(); // if no result NoResultException
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,28 +1,46 @@
|
||||
package test3_persistence;
|
||||
|
||||
|
||||
import test1_cdi.GeneralInterceptorA;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.NamedQuery;
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@NamedQuery(name = "findxg", query = "SELECT b FROM test_class b WHERE b.title ='xg'")
|
||||
@NamedQueries({
|
||||
@NamedQuery(name = "findtest_class", query = "SELECT b FROM test_class b WHERE b.title ='test_class'"),
|
||||
@NamedQuery(name = "findxgio", query = "SELECT b FROM test_class b WHERE b.title ='xgio'"),
|
||||
@NamedQuery(name = "findxg", query = "SELECT b FROM test_class b WHERE b.title ='xg'")
|
||||
})
|
||||
@EntityListeners({Persistent_Listener1.class,Persistent_Listener2.class})
|
||||
|
||||
public class test_class {
|
||||
|
||||
@Id @GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@PostConstruct
|
||||
public void ps (){
|
||||
title = "xg";
|
||||
|
||||
public test_class (){
|
||||
title = "test_class";
|
||||
xlist = new ArrayList<test_class_2>();
|
||||
xlist.add(new test_class_2());
|
||||
xlist.get(0).setT(this);
|
||||
xlist.add(new test_class_2());
|
||||
xlist.get(1).setT(this);
|
||||
|
||||
}
|
||||
|
||||
private String title;
|
||||
|
||||
//@NotNull
|
||||
private Double price;
|
||||
|
||||
@OneToMany (mappedBy = "t",fetch = FetchType.LAZY, cascade = {CascadeType.ALL},orphanRemoval=true)
|
||||
private List<test_class_2> xlist;
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
@@ -38,4 +56,12 @@ public class test_class {
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public List<test_class_2> getXlist() {
|
||||
return xlist;
|
||||
}
|
||||
|
||||
public void setXlist(List<test_class_2> xlist) {
|
||||
this.xlist = xlist;
|
||||
}
|
||||
}
|
||||
|
||||
54
test3_persistence/test_class_2.java
Normal file
54
test3_persistence/test_class_2.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package test3_persistence;
|
||||
|
||||
|
||||
import test1_cdi.GeneralInterceptorA;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Entity
|
||||
@GeneralInterceptorA
|
||||
@NamedQuery(name = "findxg2", query = "SELECT b FROM test_class b WHERE b.title ='xg2'")
|
||||
public class test_class_2 {
|
||||
|
||||
@Id @GeneratedValue
|
||||
private Long id;
|
||||
|
||||
|
||||
public test_class_2 (){
|
||||
title = "test_class_2";
|
||||
}
|
||||
|
||||
private String title;
|
||||
|
||||
//@NotNull
|
||||
private Double price;
|
||||
|
||||
@ManyToOne (fetch = FetchType.LAZY)
|
||||
private test_class t;
|
||||
|
||||
public test_class getT() {
|
||||
return t;
|
||||
}
|
||||
|
||||
public void setT(test_class t) {
|
||||
this.t = t;
|
||||
}
|
||||
|
||||
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