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
@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; } }
This is His mapping
<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>
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;
@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; } } This is His mapping
<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> Strut2 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); beginTransaction.commit(); session.close(); }
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;
@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> } ServicioCategoria.java 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 } 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> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <br/> <h2>Mopstrar Datos</h2> <s:iterator> <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" /> </s:form> </s:iterator> </body> </html>
|