Bonjour à tous,
Je travaille sur une application Java/J2ee avec JSF et JPA, et j'ai un souci avec mon JPA....
Quand je veux "setter " (faire setSubIndustryCollection), à partir de Industry, ca ne me met pas la valeur dans SubIndustry....pourtant je lui envoie bien quelque chose...
En EAGER, ca marche bien mais je veux le laisser en LAZY au niveau de :
pour ne pas remonter toute la base à chaque fois !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 @OneToMany(mappedBy="industryId",fetch=FetchType.LAZY,cascade=CascadeType.ALL) private List<SubIndustry> subIndustryCollection;
Est ce normal qu'en LAZY on ne puisse pas faire ce genre d'action ?
Merci pour votre aide ou vos explications.
Voici mon code :
J'ai 2 Entités où Sub_Industry découle d'Industry et une industrie peut avoir plusieurs sous-industries....
Industry.java
SubIndustry.java
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 @Entity @NamedQueries({@NamedQuery(name="getIndustry", query = "SELECT i FROM Industry i"),@NamedQuery(name="getIndustryByIndustryId", query = "SELECT i FROM Industry i WHERE i.industryId = :industryId"), @NamedQuery(name="getIndustryByIndustryName", query = "SELECT i FROM Industry i WHERE i.industryName = :industryName"), @NamedQuery(name="getIndustryNameOrder", query = "SELECT i FROM Industry i ORDER BY i.industryName")}) public class Industry implements Serializable { @Id @Column(name="INDUSTRY_ID") private int industryId; @Column(name="INDUSTRY_NAME") private String industryName; private static final long serialVersionUID = 1L; @OneToMany(mappedBy="industryId",fetch=FetchType.LAZY,cascade=CascadeType.ALL) private List<SubIndustry> subIndustryCollection; public Industry() { super(); } public int getIndustryId() { return this.industryId; } public void setIndustryId(int industryId) { this.industryId = industryId; } public String getIndustryName() { return this.industryName; } public void setIndustryName(String industryName) { this.industryName = industryName; } /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + industryId; return result; } /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof Industry)) { return false; } Industry other = (Industry) obj; if (industryId != other.industryId) { return false; } return true; } /** */ public List<SubIndustry> getSubIndustryCollection() { return this.subIndustryCollection; } /** */ public void setSubIndustryCollection(List<SubIndustry> subIndustryCollection) { this.subIndustryCollection = subIndustryCollection; } }
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 @Entity @Table(name="SUB_INDUSTRY") @NamedQueries({@NamedQuery(name="getSubIndustry", query = "SELECT s FROM SubIndustry s"),@NamedQuery(name="getSubIndustryBySubIndustryId", query = "SELECT s FROM SubIndustry s WHERE s.subIndustryId = :subIndustryId"), @NamedQuery(name="getSubIndustryBySubIndustryName", query = "SELECT s FROM SubIndustry s WHERE s.subIndustryName = :subIndustryName"), @NamedQuery(name="getSubIndustryByIndustryId", query = "SELECT s FROM SubIndustry s WHERE s.industryId.industryId = :industryId_industryId"), @NamedQuery(name="getSubIndByIndustryIdOrder", query = "SELECT s FROM SubIndustry s WHERE s.industryId.industryId = :industryId_industryId ORDER BY s.subIndustryName")}) public class SubIndustry implements Serializable { @Id @Column(name="SUB_INDUSTRY_ID") private int subIndustryId; @Column(name="SUB_INDUSTRY_NAME") private String subIndustryName; @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="INDUSTRY_ID") private Industry industryId; private static final long serialVersionUID = 1L; public SubIndustry() { super(); } public int getSubIndustryId() { return this.subIndustryId; } public void setSubIndustryId(int subIndustryId) { this.subIndustryId = subIndustryId; } public String getSubIndustryName() { return this.subIndustryName; } public void setSubIndustryName(String subIndustryName) { this.subIndustryName = subIndustryName; } public Industry getIndustryId() { return this.industryId; } public void setIndustryId(Industry industryId) { this.industryId = industryId; } /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + subIndustryId; return result; } /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof SubIndustry)) { return false; } SubIndustry other = (SubIndustry) obj; if (subIndustryId != other.subIndustryId) { return false; } return true; } }
Mon Bean où je récupère mon objet Industry en fonction de son ID et récupèrer la liste de sous industries lui appartenant....
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 IndustryManager indManager = (IndustryManager)getManagedBean("industryManager"); Industry ind = indManager.findIndustryByIndustryId(industryId); ind.setSubIndustryCollection(new ArrayList<SubIndustry>()); for (Iterator<SubIndustry> i = allSubInd.iterator(); i.hasNext(); ) { SubIndustry si = (SubIndustry) i.next(); ind.getSubIndustryCollection().add(si); listSub_i.add(new SelectItem(si.getSubIndustryId(),si.getSubIndustryName())); } ind.setSubIndustryCollection(allSubInd);
Partager