Veritable requete d'un javax.persistence.Query
Suposant ces deux classes :
Entité TVA
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@Entity
@Table(name = "tva")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Tva.findAll", query = "SELECT t FROM Tva t ORDER BY t.tva ASC"),
@NamedQuery(name = "Tva.findByCodTva", query = "SELECT t FROM Tva t WHERE t.codTva = :codTva"),
@NamedQuery(name = "Tva.findByTva", query = "SELECT t FROM Tva t WHERE t.tva = :tva"),
@NamedQuery(name = "Tva.findByCodeComptable", query = "SELECT t FROM Tva t WHERE t.codeComptable = :codeComptable")})
public class Tva implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 20)
@Column(name = "COD_TVA")
private String codTva;
// @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Basic(optional = false)
@NotNull
@Column(name = "TVA")
private BigDecimal tva;
@Size(max = 20)
@Column(name = "CODE_COMPTABLE")
private String codeComptable;
@Transient
private BigDecimal transTva;
public Tva() {
}
public Tva(String codTva) {
this.codTva = codTva;
}
public Tva(String codTva, BigDecimal tva) {
this.codTva = codTva;
this.tva = tva;
}
public String getCodTva() {
return codTva;
}
public void setCodTva(String codTva) {
this.codTva = codTva;
}
public BigDecimal getTva() {
BigDecimal ret = BigDecimal.ZERO;
try{
ret = tva.setScale(2, RoundingMode.HALF_EVEN);
}catch(Exception ex){
ret = tva;
}
return ret;
}
public void setTva(BigDecimal tva) {
this.tva = tva;
}
public String getCodeComptable() {
return codeComptable;
}
public void setCodeComptable(String codeComptable) {
this.codeComptable = codeComptable;
}
@Override
public int hashCode() {
int hash = 0;
hash += (codTva != null ? codTva.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 Tva)) {
return false;
}
Tva other = (Tva) object;
if ((this.codTva == null && other.codTva != null) || (this.codTva != null && !this.codTva.equals(other.codTva))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.relaistoulousain.domain.Tva[ codTva=" + codTva + " ]";
}
/**
* @return the transTva
*/
public BigDecimal getTransTva() {
transTva = tva.setScale(2, RoundingMode.HALF_EVEN);
return transTva;
}
/**
* @param transTva the transTva to set
*/
public void setTransTva(BigDecimal transTva) {
this.transTva = transTva;
}
} |
Et son EJB
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
import com.relaistoulousain.domain.Clients;
import com.relaistoulousain.domain.Tva;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless
public class TvaFacade extends AbstractFacade<Tva> implements TvaFacadeLocal {
@PersistenceContext(unitName = "RelaisToulousain-ejbPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public TvaFacade() {
super(Tva.class);
}
@Override
public Tva obtenirTvaParTaux(BigDecimal Taux) {
try {
Query requete = em.createNamedQuery("Tva.findByTva");
requete.setParameter("tva", Taux);
return (Tva) requete.getSingleResult();
} catch (NoResultException ex) {
return null;
}
}
@Override
public Tva obtenirTvaParCode(String codeTva) {
try {
Query requete = em.createNamedQuery("Tva.findByCodTva");
requete.setParameter("codTva", codeTva);
return (Tva) requete.getSingleResult();
} catch (NoResultException ex) {
return null;
}
}
@Override
public List<Tva> obtenirLazyListeTva(int debut, int longeurPage) {
List<Tva> tvas = new ArrayList<Tva>();
try{
Query requete = em.createNamedQuery("Tva.findAll");
requete.setMaxResults(longeurPage - debut);
requete.setFirstResult(debut);
tvas = requete.getResultList();
} catch (NoResultException ex) {
tvas = null;
} finally {
return tvas;
}
}
public Integer obtenirNombreListeTva(){
Integer nbr = 0;
try{
Query requete = em.createQuery("SELECT COUNT(t) FROM Tva t");
Long nbrL = (Long) requete.getSingleResult();
nbr = nbrL.intValue();
} catch (NoResultException ex) {
nbr = 0;
} finally {
return nbr;
}
}
@Override
public List<Tva> obtenirLazyListeTva(int debut, int longeurPage,Map<String, String> filters) {
List<Tva> tvas = new ArrayList<Tva>();
try{
String queryString = "SELECT t FROM Tva t WHERE t.codTva IS NOT NULL";
if (!filters.isEmpty()) {
for(String key:filters.keySet()){
queryString += (" AND t."+key+" LIKE '%"+filters.get(key)+"%'");
}
}
Query requete = em.createQuery(queryString);
requete.setMaxResults(longeurPage - debut);
requete.setFirstResult(debut);
tvas = requete.getResultList();
} catch (NoResultException ex) {
tvas = null;
} finally {
return tvas;
}
}
public Integer obtenirNombreListeTva(Map<String, String> filters){
Integer nbr = 0;
try{
String queryString = "SELECT COUNT(t) FROM Tva t WHERE t.codTva IS NOT NULL";
if (!filters.isEmpty()) {
for(String key:filters.keySet()){
queryString += (" AND t."+key+" LIKE '%"+filters.get(key)+"%'");
}
}
Query requete = em.createQuery(queryString);
Long nbrL = (Long) requete.getSingleResult();
nbr = nbrL.intValue();
} catch (NoResultException ex) {
nbr = 0;
} finally {
return nbr;
}
}
} |
Comment puis-je connaitre le vrai requete SQL executé à partir d'un objet du type javax.persistence.Query?
Exemple à la ligne 84-89 de mon EJB :
Code:
1 2 3 4 5 6 7
|
String queryString = "SELECT t FROM Tva t WHERE t.codTva IS NOT NULL";
if (!filters.isEmpty()) {
for(String key:filters.keySet()){
queryString += (" AND t."+key+" LIKE '%"+filters.get(key)+"%'");
}
Query requete = em.createQuery(queryString); |
Je veux un fonction qui retourne quelque chose comme celà :
SELECT t FROM tva t WHERE t.COD_TVA IS NOT NULL AND ....
Je ne sais pas si vous comprenez!
C'es le vrai requete SQL que je veut obtenir à partir du requete type Query!
Merci de votre aide!