Bonjour !
Il y à peu je me suis lancé sur le JSF/JSP.. et là je bloque un peu avec le JPA.
je me suis aidé de ce tuto: http://wwwdi.supelec.fr.....jpa.php
Je suis à la fin!
mais mon problème et que je sais récupérer ce qui est sur mes tables dans ma base de données, par contre pour écrire dessus c'est plus compliqué..
Mon code est bien écrit car netbean ne me renvois aucune erreur par contre sur ma page j'ai javax.ejb.EJBException: Transaction aborted
et dans mon Stack Trace j'ai une ligne qui attire particulièrement mon attention:Je vous mets mon code au cas où ça pourrait aider quelqu'un qui est aussi sur ce tuto.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLSyntaxErrorException: Tentative de modification d'une colonne d'identité 'ID'. Error Code: -1 Call: INSERT INTO PRODUCT (ID, NAME, SELLING_PRICE) VALUES (?, ?, ?) bind => [3 parameters bound] Query: InsertObjectQuery(Model.Product@7488e66)
Mon entity
Mon ManagedBean
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package Model; import java.io.Serializable; import javax.persistence.*; /** * * @author Kayri */ @Table(name ="PRODUCT") @Entity public class Product implements Serializable { @Id @GeneratedValue(strategy= GenerationType.SEQUENCE) @Column( name = "ID") private int ID; @Column( name = "NAME") private String Nom ; @Column(name = "SELLING_PRICE") private Double Prix; public Product() { } public int getID(){ return ID; } public void setID(final int identifiant){ ID = identifiant; } public String getNom(){ return Nom; } public void setNom(final String name){ Nom = name; } public double getPrix(){ return Prix; } public void setPrix(final double PRIX){ Prix = PRIX; } }
Mon productfacade
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package logic; import Model.Product; import facade.ProductFacade; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.annotation.PostConstruct; import javax.ejb.EJB; import javax.faces.bean.ApplicationScoped; import javax.faces.bean.ManagedBean; /** * * @author Kayri */ @ManagedBean @ApplicationScoped public class CatalogManager implements Serializable { @EJB private ProductFacade productfacade; private List<Product> liste; private ArrayList<Product> Catalog; private Product bean = new Product(); private int fieldID ; private String fieldNAME ; private double fieldPRIX ; public CatalogManager() { this.Catalog = new ArrayList<Product>(); this.liste = new ArrayList<Product>(); } public List getListe(){ return liste; } public void setCatalog(ArrayList<Product> catalog){ this.Catalog = catalog; } public ArrayList<Product> getCatalog() { return Catalog; } public ProductFacade getproductfacade(){ return productfacade; } public void setproductFacade(ProductFacade productfacade){ this.productfacade = productfacade; } @PostConstruct public void InitMethod() { liste = productfacade.findAll(); } public void setFieldID(int fieldID) { this.fieldID = fieldID; } public void setFieldNAME(String fieldNAME) { this.fieldNAME = fieldNAME; } public void setFieldPRIX(double fieldPRIX) { this.fieldPRIX = fieldPRIX; } public int getFieldID() { return fieldID; } public String getFieldNAME() { return fieldNAME; } public double getFieldPRIX() { return fieldPRIX; } public void createProduct() { setFieldID(fieldID); setFieldNAME(fieldNAME); setFieldPRIX(fieldPRIX); bean.setID(fieldID); bean.setNom(fieldNAME); bean.setPrix(fieldPRIX); productfacade.create(bean); } }
mon abstractfacade
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package facade; import Model.Product; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; /** * * @author Kayri */ @Stateless public class ProductFacade extends AbstractFacade<Product> { @PersistenceContext(unitName = "eMarketPU") private EntityManager em; @Override protected EntityManager getEntityManager() { return em; } public ProductFacade() { super(Product.class); } }
et ma page xhtml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package facade; import java.util.List; import javax.persistence.EntityManager; /** * * @author Kayri */ public abstract class AbstractFacade<T> { private Class<T> entityClass; public AbstractFacade(Class<T> entityClass) { this.entityClass = entityClass; } protected abstract EntityManager getEntityManager(); public void create(T entity) { getEntityManager().persist(entity); } public void edit(T entity) { getEntityManager().merge(entity); } public void remove(T entity) { getEntityManager().remove(getEntityManager().merge(entity)); } public T find(Object id) { return getEntityManager().find(entityClass, id); } public List<T> findAll() { javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery(); cq.select(cq.from(entityClass)); return getEntityManager().createQuery(cq).getResultList(); } public List<T> findRange(int[] range) { javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery(); cq.select(cq.from(entityClass)); javax.persistence.Query q = getEntityManager().createQuery(cq); q.setMaxResults(range[1] - range[0]); q.setFirstResult(range[0]); return q.getResultList(); } public int count() { javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery(); javax.persistence.criteria.Root<T> rt = cq.from(entityClass); cq.select(getEntityManager().getCriteriaBuilder().count(rt)); javax.persistence.Query q = getEntityManager().createQuery(cq); return ((Long) q.getSingleResult()).intValue(); } }
voili voilou. Merci pour votre aide!
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 <h:form> <h:commandLink action="index"> <h:outputText value="Lien vers l'accueil"/> </h:commandLink> <br/><br/><br/> <h:outputLabel id="fieldLabelID" for="fieldID" value="Identifiant du prouit: "/> <h:inputText id="fieldID" value="#{catalogManager.fieldID}"/> <br/> <h:outputLabel id="fieldLabelNAME" for="fieldNAME" value="Nom du produit: "/> <h:inputText id="fieldNAME" value="#{catalogManager.fieldNAME}"/> <br/> <h:outputLabel id="fieldLabelPRIX" for="fieldPRIX" value="Prix du produit: "/> <h:inputText id="fieldPRIX" value="#{catalogManager.fieldPRIX}"/> <br/><br/> <h:commandButton value="Ajouter" action="#{catalogManager.createProduct()}"/> </h:form>
Partager