Désolé je sais pas ce que j'ai foutu mais je voulais poster dans le forum Hibernate, si quelqu'un peut le déplacer ... --> Merci ;-)
Bonjour,
Dans un nouveau projet je tente de me mettre au mapping de collection et je rencontre une erreur dont je n'arrive pas à savoir si elle vient d'une étourderie ou d'un vrai problème de compréhension du fonctionnement d'Hibernate (d'utilisation du lazy loading par exemple).
J'ai une entity Custumer qui contient une collection d'entity Soft :
Custumer :
Avec comme fichier de 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 @Entity public class Custumer implements Serializable { private int id; private String login; private String pass; private String name; private List<Soft> softs; public Custumer(){ } @OneToMany(targetEntity=Soft.class, mappedBy="idcustumer") public List<Soft> getSofts(){ return this.softs; } public void setSofts(List<Soft> softs){ this.softs = softs; } @Id @GeneratedValue public int getId (){ return this.id; } public void setId(int id){ this.id = id; } et autres setter et getter .. }
Soft :
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 <hibernate-mapping> <class name="data.Custumer" table="custumer" catalog="portal"> <id name="id" type="int"> <column name="id" /> <generator class="identity" /> </id> <property name="login" type="string"> <column name="login" length="30" /> </property> <property name="name" type="string"> <column name="name" length="30" /> </property> <property name="pass" type="string"> <column name="pass" length="30" /> </property> <set name="softs" inverse="true" cascade="all" lazy="true"> <key column="idcustumer" not-null="true" /> <one-to-many class="data.Soft" /> </set> </class> </hibernate-mapping>
Avec comme fichier de 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 @Entity public class Soft implements Serializable { private int id; private String addr; private String version; private Custumer custumer; public Soft(){ } @ManyToOne @JoinColumn(name = "idcustumer") public Custumer getCustumer(){ return this.custumer; } public void setCustumer(Custumer custumer){ this.custumer = custumer; } @Id @GeneratedValue public int getId(){ return this.id; } public void setId(int id){ this.id = id; } etc ... }
J'ai également des DAO et des services associés mais je ne pense pas qu'il soit utile que je les mette ici. Dites moi si besoin.
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 <hibernate-mapping> <class name="data.Soft" table="soft" catalog="portal"> <id name="id" type="int"> <column name="id" /> <generator class="identity" /> </id> <property name="addr" type="string"> <column name="addr" length="100" /> </property> <property name="version" type="string"> <column name="version" length="30" /> </property> <many-to-one name="custumer" class="data.Custumer" insert="false" update="false"> <column name="idcustumer" /> </many-to-one> </class> </hibernate-mapping>
Dans un test unitaire, j'essaye simplement mes fonctions CRUD sur la classe Custumer. La base ne contient aucune entrée dans la table Softs.
L'ajout se passe bien, j'insère trois Custumer.
Le read plante en mettant l'erreur suivante :
Est-ce que quelqu'un voit quelque chose dans mon code qui pourrait provoquer ça ?
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 Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 2.031 sec <<< FAILURE! testAddEditGetDataRemove(test.service.dao.CustumerManagerTest) Time elapsed: 2 sec <<< FAILURE! junit.framework.AssertionFailedError: custumerManager:find: IllegalArgumentException occurred while calling setter of data.Custumer.softs; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of data.Custumer.softs at junit.framework.Assert.fail(Assert.java:47) at test.service.dao.CustumerManagerTest.testAddEditGetDataRemove(CustumerManagerTest.java:84) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at junit.framework.TestCase.runTest(TestCase.java:168) at junit.framework.TestCase.runBare(TestCase.java:134) at junit.framework.TestResult$1.protect(TestResult.java:110) at junit.framework.TestResult.runProtected(TestResult.java:128) at junit.framework.TestResult.run(TestResult.java:113) at junit.framework.TestCase.run(TestCase.java:124) at junit.framework.TestSuite.runTest(TestSuite.java:232) at junit.framework.TestSuite.run(TestSuite.java:227) at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:81) at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62) at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140) at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127) at org.apache.maven.surefire.Surefire.run(Surefire.java:177) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338) at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
N'hésitez pas à me demander des précisions, je suis réactif.
Merci d'avance pour votre aide.
Bonne journée.
Partager