Bonjour,
J'essai de me mettre à Hibernate depuis peu, j'arrive à faire des insertions dans une table unique mais pas à gérer les une relation clé primaire, clé étrangère.
Dans ma BDD (MySQL) j'ai 2 tables :
- t_catogories (id_category clé primaire, name)
- t_products (id_product clé primaire, name, price, id_category clé étrangère pointant sur la table t_categories)
Ma BDD est correctement configurée.
Voila mon fichier de mapping pour l'objet Category :
Pour mon objet Product :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 <hibernate-mapping> <class name="com.testspring.domain.model.Category" table="t_categories"> <cache usage="read-write" /> <id name="categoryID" column="id_category"> <generator class="uuid.hex"></generator> </id> <property name="name" column="name" /> </class> </hibernate-mapping>
Mon objet Product :
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 <hibernate-mapping> <class name="com.testspring.domain.model.Product" table="t_products"> <cache usage="read-write" /> <id name="productID" column="id_product"> <generator class="uuid.hex"></generator> </id> <property name="name" column="name" /> <many-to-one name="category" class="com.testspring.domain.model.Category" cascade="none" outer-join="auto" update="true" insert="true" column="category_id" /> <property name="price" column="price" /> </class> </hibernate-mapping>
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 package com.testspring.domain.model; import java.io.Serializable; public class Product implements Serializable{ private static final long serialVersionUID = 4048798961366546485L; private String productID; private String name; private String price; private Category category; public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getProductID() { return productID; } public void setProductID(String productID) { this.productID = productID; } public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof Product)) { return false; } final Product product = (Product) o; if (productID != null ? !productID.equals(product.productID) : product.productID != null) { return false; } return true; } public int hashCode() { return (productID != null ? productID.hashCode() : 0); } }
Ma classe permettant de faire mon insertion :
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 public class ProductManagerImpl implements ProductManager { private final Logger log = Logger.getLogger(ProductManagerImpl.class); private ProductDAO productDAO = null; private CategoryManager categoryManager = null; public void setCategoryManager(CategoryManager categoryManager) { this.categoryManager = categoryManager; } public void setProductDAO(ProductDAO productDAO) { this.productDAO = productDAO; } public void createProduct(Product product) { log.debug("Creating Product : " + product.getName()); Category category = categoryManager.findCategory(categoryID); product.setCategory(category); productDAO.saveProduct(product); } ... }
Et là evidemment lorsque j'appelle la méthode createProduct j'ai droit à au beau NullPointerException et sincerement je ne voit pas trop pourquoi...
Si qqun voit pourquoi...
Merci d'avance.
Partager