Voilà
j'ai 2 classes : Order et Product
dans Order, j'ai une HashMap avec comme key une référence vers un objet Product
et comme value un Integer(la quantité)
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 package test; import java.util.HashMap; import java.util.Map; public class Order { private Integer id; private String nbr; private Map<Product,Integer> product = new HashMap<Product,Integer>(); public Order() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getNbr() { return nbr; } public void setNbr(String nbr) { this.nbr = nbr; } public Map<Product, Integer> getProduct() { return product; } public void setProduct(Map<Product, Integer> product) { this.product = product; } public void addProduct(Product p, Integer q){ this.product.put(p, q); } }
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 package test; public class Product { private Integer id; private String ref; private String name; private Double price; public Product() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getRef() { return ref; } public void setRef(String ref) { this.ref = ref; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } }la database
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 package test; import net.hpph.util.HibernateUtil; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; public class Main { public static void main(String[] args) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction transaction = null; Product p1,p2; Order o; try { transaction = session.beginTransaction(); p1 = new Product(); p1.setRef("X45896"); p1.setName("printer HP"); p1.setPrice(100.55); p2 = new Product(); p2.setRef("X458589"); p2.setName("HardDisk 1Tera"); p2.setPrice(70.55); session.persist(p1); session.persist(p2); o = new Order(); o.setNbr("Fact10589"); o.addProduct(p1, new Integer(10)); o.addProduct(p2, new Integer(50)); session.persist(o); transaction.commit(); } catch (HibernateException e) { transaction.rollback(); e.printStackTrace(); } finally { session.close(); } } }
les fichiers de configuration
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 CREATE TABLE IF NOT EXISTS `order` ( `ORDERID` int(11) NOT NULL AUTO_INCREMENT, `NUMBER` varchar(100) COLLATE latin1_general_ci NOT NULL, PRIMARY KEY (`ORDERID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ; CREATE TABLE IF NOT EXISTS `order_product` ( `ORDER_ORDERID` int(11) NOT NULL, `PRODUCT_PRODUCTID` int(11) NOT NULL, `QUANTITY` int(11) NOT NULL, PRIMARY KEY (`ORDER_ORDERID`,`PRODUCT_PRODUCTID`), KEY `fk_ORDER_has_PRODUCT_PRODUCT1` (`PRODUCT_PRODUCTID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; CREATE TABLE IF NOT EXISTS `product` ( `PRODUCTID` int(11) NOT NULL AUTO_INCREMENT, `REF` varchar(100) COLLATE latin1_general_ci NOT NULL, `NAME` varchar(100) COLLATE latin1_general_ci NOT NULL, `PRICE` double NOT NULL, PRIMARY KEY (`PRODUCTID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;
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 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="test"> <class name="Product" table="PRODUCT"> <id name="id" column="PRODUCT_ID"> <generator class="native"/> </id> <property name="ref" column="REF" type="string" not-null="true" length="50" /> <property name="name" column="NAME" type="string" not-null="true" length="100" /> <property name="price" column="PRICE" type="double" not-null="true" /> </class> </hibernate-mapping>je pense que la définition de la colonne QUANTITY n'est pas bonne
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 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="test"> <class name="Order" table="ORDER"> <id name="id" column="ORDER_ID"> <generator class="native"/> </id> <property name="nbr" column="NUMBER" type="string" not-null="true" length="100" /> <map name="product" cascade="save-update,persist" lazy="true"> <key column="ORDER_ORDERID" /> <index-many-to-many column="PRODUCT_PRODUCTID" class="Product"/> <many-to-many column="QUANTITY" class="Product" fetch="select"/> </map> </class> </hibernate-mapping>
merci pour vos commentaires
je suis bloqué pour mon projet ...
voilà l'erreur affiché par Eclipse
un grand merci pour votre lectureSLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hibernate:
/* insert test.Product
*/ insert
into
PRODUCT
(REF, NAME, PRICE)
values
(?, ?, ?)
Hibernate:
/* insert test.Product
*/ insert
into
PRODUCT
(REF, NAME, PRICE)
values
(?, ?, ?)
Hibernate:
/* insert test.Order
*/ insert
into
ORDER (NUMBER
)
values
(?)
org.hibernate.exception.SQLGrammarException: could not insert: [test.Order]
Partager