Java - Strut2 e ivernate con 2 tablas

 
Vista:
sin imagen de perfil

Strut2 e ivernate con 2 tablas

Publicado por Jorge (2 intervenciones) el 28/03/2015 16:28:35
Estoy tratando de hacer un crud en Strud2 con hibernate y no consigo combinar dos tablas . Me puede ayudar

La 1 Tabla es Trama
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
@Entity
@Table(name="trama", catalog="bd_1")
 
public class Trama  implements java.io.Serializable {
 
     private int traNoca;         //Int
     private int catNoca;       //Enlace con categoria
     private String tradat;       //Dato sin importancia
 
    public Trama() {}
 
    public Trama(int traNoca, int catNoca, String tradat)
    {this.traNoca = traNoca;  this.catNoca = catNoca;  this.tradat = tradat;  }
 
     @Id
    @Column(name="tra_noca", nullable=false)
     public int getTraNoca() {return this.traNoca;} ES
    public void setTraNoca(int traNoca) {this.traNoca = traNoca; }
 
    @Column(name="cat_noca", nullable=false)
    public int getCatNoca() {return this.catNoca;}
    public void setCatNoca(int catNoca) {this.catNoca = catNoca;}
 
    @Column(name="tra_dat", nullable=false, length=45)
    public String getTradat() {return this.tradat;}
    public void setTradat(String tradat) { this.tradat = tradat;  }
 
}


Este es Su mapeado

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<hibernate-mapping>
    <class name="entidad.Trama" table="trama" catalog="bd_1">
        <id name="TraNoca" type="int">
            <column name="tra_noca" />
           <generator class="assigned"></generator>
        </id>
        <property  name="CatNoca" type="int">
            <column name="cat_noca" />
         </property>
        <property name="Tradat" type="string">
            <column name="cat_dat" length="50" />
        </property>
    </class>
</hibernate-mapping>

La 2 Tabla es Categoria que es la principal

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
package entidad;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
 
@Entity
@Table(name = "trama", catalog = "bd_1"   )
 
public class Categoria  implements java.io.Serializable {
 
     private int catNoca;
     private String catNom;
 
    public Categoria() {}
 
    public Categoria(int catNoca, String catNom)
    {this.catNoca = catNoca;   this.catNom = catNom;}
 
     @Id
     @Column(name="cat_noca", nullable=false)
    public int getCatNoca() {return this.catNoca;}
    public void setCatNoca(int catNoca) {this.catNoca = catNoca;}
 
    @Column(name="cat_nom", nullable=false, length=45)
    public String getCatNom() {return this.catNom;}
    public void setCatNom(String catNom) { this.catNom = catNom; }
}

Este es el Mapeado

1
2
3
4
5
6
7
8
9
10
11
<hibernate-mapping>
    <class name="entidad.Categoria" table="categoria" catalog="bd_1">
        <id name="CatNoca" type="int">
            <column name="cat_noca" />
           <generator class="assigned"></generator>
        </id>
        <property name="CatNom" type="string">
            <column name="cat_nom" length="50" />
        </property>
    </class>
</hibernate-mapping>

Y este es Strut2 que manda la orden

1
2
3
4
5
6
7
8
public  void detalle2(String noca,T entity) {
 
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction beginTransaction = session.beginTransaction();
        session.createSQLQuery("select c.CatNoca, c.CatNom, t.CatNoca, t.TraNoca, t.CatNom, t.tradat  from Trama t INNER JOIN Categoria c ON t.CatNoca="+entity);
        beginTransaction.commit();
        session.close();
	}

La entidad me lo manda


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
package acciones;
 
import static com.opensymphony.xwork2.Action.INPUT;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionSupport;
import entidad.Categoria;
import java.util.List;
import servicio.ServicioCategoria;
 
public class CategoriaAction extends ActionSupport {
 
    private ServicioCategoria sc= new ServicioCategoria();
    private List<Categoria> lstCat;
    private Categoria cat;
    private String m;//NO IMPORTANTE PARA LA DUDA
    private String i;//NO IMPORTANTE PARA LA DUDA
    private Integer noca;
 
    @Override
    public String input() throws Exception {
        if (getNoca()!=null) {setCat(sc.find(noca));}
        return INPUT;}
 
    public String detalle2() throws Exception {
        if (getNoca()!=null) {setCat(sc.find(noca)); }
       return INPUT;}
 
    @Override
    public String execute() throws Exception {return SUCCESS;}
    public String save() {    sc.save(getCat());     return "ok"; }
    public String remove(){    sc.remove(getNoca());    return "ok";    }
    public String list() throws Exception { lstCat=sc.findAll();  return execute();}
    public String search() throws Exception { lstCat = sc.search(m);return execute();}
    public String indice() throws Exception { lstCat = sc.search(i);return execute();}
 
 
 
    //<editor-fold defaultstate="collapsed" desc="Getter y Setter">  
 
    public ServicioCategoria getSc() {return sc; }
    public void setSc(ServicioCategoria sc) {this.sc = sc;}
 
    public String getM() { return m;}
    public void setM(String m) {   this.m = m; }
 
    public String getI() { return i; }
    public void setI(String i) { this.i = i;}
 
    public Integer getNoca() { return noca; }
    public void setNoca(Integer noca) {this.noca = noca;}
 
    public Categoria getCat() {return cat;}
    public void setCat(Categoria cat) { this.cat = cat; }
 
    public List<Categoria> getLstCat() {return lstCat;}
    public void setLstCat(List<Categoria> lstCat) {this.lstCat = lstCat;}
 
 
//</editor-fold>
}

Ayudada por

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class ServicioCategoria extends AbstractFacade<Categoria> {
 
    public ServicioCategoria() {super(Categoria.class);}
 
 
 
    //CATEGORIA
 
    public List<Categoria> findAll() {return super.findAll();}
    public void save(Categoria cat) {super.createEdit(cat);}
    public void remove(int noca) {
        final Categoria find = super.find(noca);
        if (find!=null){
        super.remove(find);}
    }
    public Categoria find (int noca) {
        return super.find(noca);
    }
 
    //TRAMA   
}

No Consigo unir las tablas estoy desesperado
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