Bonjour,
J'ai un problème de synchronisation de l'entityManager avec la BD dans le cadre d'un projet utilisant des EJB3 et JPA.
J'ai un ProductStore qui contient plusieurs Product. Je crée d'abord un Product lié à un ProductStore, puis ensuite je veux obtenir la liste des Product de ce ProductStore, le Product précédemment ajouté n'apparaît pas. Par contre, si je redémarre le serveur, il apparait.
Pourtant ils dialoguent avec le même entityManager.
Voici mes classes :
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 @Entity @Table(name="product") public class ProductEntity implements Serializable { @Id private int reference; private String name; private double price; @ManyToOne @JoinColumn (name="reference_store") private ProductStoreEntity productStore; public ProductEntity() { } public ProductEntity(int reference, String name, double price, ProductStoreEntity productStore) { this.reference = reference; this.name = name; this.price = price; this.productStore = productStore; } public int getReference() { return reference; } public String getName() { return name; } public double getPrice() { return price; } public ProductStoreEntity getProductStore() { return productStore; }
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 @Entity @Table(name="product_store") public class ProductStoreEntity implements Serializable { @Id private int reference; private String name; @OneToMany(mappedBy="productStore") private List<ProductEntity> products = new ArrayList<ProductEntity>(); public ProductStoreEntity() { } public ProductStoreEntity(int reference, String name, AccountEntity account) { this.reference = reference; this.name = name; this.account = account; } public int getReference() { return reference; } public String getName() { return name; } public List<ProductEntity> getProducts() { products.size(); return products; }
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 @Stateless public class ProductDao { @PersistenceContext(unitName="Projet-eCOM-PU") private EntityManager em; public ProductEntity get(int reference) { return em.find(ProductEntity.class, reference); } public List<ProductEntity> getAll() { return em.createQuery("select object(o) from ProductEntity as o").getResultList(); } }
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 @Stateless public class ProductStoreDao { @PersistenceContext(unitName="Projet-eCOM-PU") private EntityManager em; public void add(ProductStoreEntity productStore) { em.persist(productStore); } public void edit(ProductStoreEntity productStore) { em.merge(productStore); } public ProductStoreEntity get(int reference) { return em.find(ProductStoreEntity.class, reference); } //ProductEntity public void addProduct(ProductEntity product) { em.persist(product); } public void editProduct(ProductEntity product) { em.merge(product); } public ProductEntity getProduct(int reference) { return em.find(ProductEntity.class, reference); } }
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 @Stateful public class EcomAdminBean implements EcomAdminRemote { @EJB private ProductStoreDao productStoreDao; @Override public void createProduct(int productId, String productName, double productPrice, int productStoreId) { ProductEntity product = new ProductEntity(productId, productName, productPrice, productStoreDao.get(productStoreId)); productStoreDao.addProduct(product); } @Override public List<ProductEntity> getProductsByProductStoreId(int refProductStore) { ProductStoreEntity pse = productStoreDao.get(refProductStore); return pse.getProducts(); } @Override public List<ProductEntity> getAllProducts() { return productDao.getAll(); } }Les méthodes de ma Servlet utilisant ces EJB :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 public interface EcomAdminRemote { void createProduct(int productId, String productName, double productPrice, int productStoreId); public List<ProductEntity> getProductsByProductStoreId(int refProductStore); public List<ProductEntity> getAllProducts(); }
Peut être c'est la manière d'organiser mes EJB qui n'est pas bonne, avez-vous une idée ?
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 private String getProducts(HttpServletRequest request) { int refProductStore = Integer.parseInt(request.getParameter("refProductStore")); List<ProductEntity> products = ecomAdmin.getProductsByProductStoreId(refProductStore); request.setAttribute("products", products); return "afficheProducts"; } private String addProduct(HttpServletRequest request) { int reference = Integer.parseInt(request.getParameter("reference")); double price = Double.parseDouble(request.getParameter("price")); String name = request.getParameter("name"); int referenceStore = Integer.parseInt(request.getParameter("referenceStore")); ecomAdmin.createProduct(reference, name, price, referenceStore); return "test"; }
Merci.
Partager