Click here to Skip to main content
15,068,988 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I 'm trying to make a crud in Strud2 with hibernate and I can not combine two tables. Can you help me
Table 1 is Trama

@Table(name="trama", catalog="bd_1")

public class Trama  implements {

     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;  }

    @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;  }

This is His mapping

    <class name="entidad.Trama" table="trama" catalog="bd_1">
        <id name="TraNoca" type="int">
            <column name="tra_noca" />
           <generator class="assigned"></generator>
        <property  name="CatNoca" type="int">
            <column name="cat_noca" />
        <property name="Tradat" type="string">
            <column name="cat_dat" length="50" />

Categoria is 2 Table is which is the main

package entidad;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "trama", catalog = "bd_1"   )

public class Categoria  implements {

     private int catNoca;
     private String catNom;

    public Categoria() {}

    public Categoria(int catNoca, String catNom)
    {this.catNoca = catNoca;   this.catNom = catNom;}

     @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; }
This is His mapping

    <class name="entidad.Categoria" table="categoria" catalog="bd_1">
        <id name="CatNoca" type="int">
            <column name="cat_noca" />
           <generator class="assigned"></generator>
        <property name="CatNom" type="string">
            <column name="cat_nom" length="50" />
Which sends the order

    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);

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;
    private String i;
    private Integer noca;

    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;}

    public String execute() throws Exception {return SUCCESS;}
    public String save() {;     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 =;return execute();}
    public String indice() throws Exception { lstCat =;return execute();}

    //<editor-fold defaultstate="collapsed" desc="Getter y Setter">
    public ServicioCategoria getSc() {return sc; }
    public void setSc(ServicioCategoria 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) { = cat; }
    public List<Categoria> getLstCat() {return lstCat;}
    public void setLstCat(List<Categoria> lstCat) {this.lstCat = lstCat;}
public class ServicioCategoria extends AbstractFacade<Categoria> {
    public ServicioCategoria() {super(Categoria.class);}


    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){
    public Categoria find (int noca) {
        return super.find(noca);

The JSP SAMPLE ONLY data table category
No I Get to join the tables I'm desperate

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <h2>Mopstrar Datos</h2>
        <s:form action="categoriaDetalle2">
            <s:label label="Nombre" name="cat.catNoca" />
            <s:label label="Nombre" name="cat.catNom" />
            <s:label label="La 2 tabla" />
             <s:label label="Nombre de trama" name="tra.Tradat" />
            <s:label label="Nombre de trama" name="tra.catNom" />


I Get to join the tables I'm desperate

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900