JSF - Como hacer Join con dos tablas distintas usando Java Persistence

 
Vista:
sin imagen de perfil

Como hacer Join con dos tablas distintas usando Java Persistence

Publicado por Everth (2 intervenciones) el 21/03/2014 18:51:45
Hola, buenas, megustaría que me pudieran ayudar.

Resulta que tengo las clases:
Producto(IdProducto,Producto, Existencia, Precio, IdCategoria, IdMarca)
Categoria (IdCategoria,Categoria)
Marca(IdMarca,Marca)

En una página xhtml quiero mostrar el inventario de productos, pero no se como mostrar la descripción de la categoria y de la marca en la página, lo que logro hacer hasta el momento es mostrar el IdCategoria y el IdMarca, pero no eso lo que quiero, si no mostrar dichas descripciones.


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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
Clase Producto
 
 
@Entity
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Producto.findAll", query = "SELECT p FROM Producto p"),
    @NamedQuery(name = "Producto.findByIdProducto", query = "SELECT p FROM Producto p WHERE p.idProducto = :idProducto"),
    @NamedQuery(name = "Producto.findByProducto", query = "SELECT p FROM Producto p WHERE p.producto = :producto"),
    @NamedQuery(name = "Producto.findByPrecio", query = "SELECT p FROM Producto p WHERE p.precio = :precio"),
    @NamedQuery(name = "Producto.findByExistencia", query = "SELECT p FROM Producto p WHERE p.existencia = :existencia"),
    @NamedQuery(name = "Producto.findByIdCategoria", query = "SELECT p FROM Producto p WHERE p.idCategoria = :idCategoria"),
    @NamedQuery(name = "Producto.findByIdMarca", query = "SELECT p FROM Producto p WHERE p.idMarca = :idMarca")})
    public class Producto implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    private Integer idProducto;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    private String producto;
    @Basic(optional = false)
    @NotNull
    private int precio;
    @Basic(optional = false)
    @NotNull
    private int existencia;
    @Basic(optional = false)
    @NotNull
    @Column(name = "IdCategoria")
    private int idCategoria;
 
    @Basic(optional = false)
    @NotNull
    @Column(name = "IdMarca")
    private int idMarca;
 
   @ManyToOne
    private Marca marca;
 
    public Producto() {
 
    }
 
    public Marca getMarca() {
        return marca;
    }
 
    public void setMarca(Marca marca) {
        this.marca = marca;
    }
 
 
    public Producto(Integer idProducto) {
        this.idProducto = idProducto;
    }
 
    public Producto(Integer idProducto, String producto, int precio, int existencia, int idCategoria, int idMarca) {
        this.idProducto = idProducto;
        this.producto = producto;
        this.precio = precio;
        this.existencia = existencia;
        this.idCategoria = idCategoria;
        this.idMarca = idMarca;
    }
 
    public Integer getIdProducto() {
        return idProducto;
    }
 
    public void setIdProducto(Integer idProducto) {
        this.idProducto = idProducto;
    }
 
    public String getProducto() {
        return producto;
    }
 
    public void setProducto(String producto) {
        this.producto = producto;
    }
 
    public int getPrecio() {
        return precio;
    }
 
    public void setPrecio(int precio) {
        this.precio = precio;
    }
 
    public int getExistencia() {
        return existencia;
    }
 
    public void setExistencia(int existencia) {
        this.existencia = existencia;
    }
 
    public int getIdCategoria() {
        return idCategoria;
    }
 
    public void setIdCategoria(int idCategoria) {
        this.idCategoria = idCategoria;
    }
 
    public int getIdMarca() {
        return idMarca;
    }
 
    public void setIdMarca(int idMarca) {
        this.idMarca = idMarca;
    }
 
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idProducto != null ? idProducto.hashCode() : 0);
        return hash;
    }
 
    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Producto)) {
            return false;
        }
        Producto other = (Producto) object;
        if ((this.idProducto == null && other.idProducto != null) || (this.idProducto != null && !this.idProducto.equals(other.idProducto))) {
            return false;
        }
        return true;
    }
 
    @Override
    public String toString() {
        return "entidad.Producto[ idProducto=" + idProducto + " ]";
    }
 
 
 
}
----------------------------------------------------
Clase Marca
 
 
 
@Entity
@Table(name = "marca")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Marca.findAll", query = "SELECT m FROM Marca m"),
    @NamedQuery(name = "Marca.findByIdMarca", query = "SELECT m FROM Marca m WHERE m.idMarca = :idMarca"),
    @NamedQuery(name = "Marca.findByMarca", query = "SELECT m FROM Marca m WHERE m.marca = :marca")})
public class Marca implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "IdMarca")
    private Integer idMarca;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 40)
    @Column(name = "Marca")
    private String marca;
 
@OneToMany
    @JoinColumn(name="IdMarca")
    private List<Marca> entidadMarca;
 
    public Marca() {
 
    }
 
    public List<Marca> getEntidadMarca() {
        return entidadMarca;
    }
 
    public void setEntidadMarca(List<Marca> entidadMarca) {
        this.entidadMarca = entidadMarca;
    }
 
    public Marca(Integer idMarca) {
        this.idMarca = idMarca;
    }
 
    public Marca(Integer idMarca, String marca) {
        this.idMarca = idMarca;
        this.marca = marca;
    }
 
    public Integer getIdMarca() {
        return idMarca;
    }
 
    public void setIdMarca(Integer idMarca) {
        this.idMarca = idMarca;
    }
 
    public String getMarca() {
        return marca;
    }
 
    public void setMarca(String marca) {
        this.marca = marca;
    }
 
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idMarca != null ? idMarca.hashCode() : 0);
        return hash;
    }
 
    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Marca)) {
            return false;
        }
        Marca other = (Marca) object;
        if ((this.idMarca == null && other.idMarca != null) || (this.idMarca != null && !this.idMarca.equals(other.idMarca))) {
            return false;
        }
        return true;
    }
 
    @Override
    public String toString() {
        return "entidad.Marca[ idMarca=" + idMarca + " ]";
    }
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder