elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado:


  Mostrar Temas
Páginas: [1]
1  Programación / Java / Crud con Strut2 e hibernate en: 28 Marzo 2015, 17:04 pm
Estoy tratando de hacer un crud en Strud2 con hibernate y no consigo combinar dos tablas . Me puede ayudar

La 1 Tabla es Trama
Código
  1. @Table(name="trama", catalog="bd_1")
  2.  
  3. public class Trama  implements java.io.Serializable {
  4.  
  5.     private int traNoca;         //Int
  6.     private int catNoca;       //Enlace con categoria
  7.     private String tradat;       //Dato sin importancia
  8.  
  9.    public Trama() {}
  10.  
  11.    public Trama(int traNoca, int catNoca, String tradat)
  12.    {this.traNoca = traNoca;  this.catNoca = catNoca;  this.tradat = tradat;  }
  13.  
  14.     @Id    
  15.    @Column(name="tra_noca", nullable=false)
  16.     public int getTraNoca() {return this.traNoca;} ES
  17.    public void setTraNoca(int traNoca) {this.traNoca = traNoca; }
  18.  
  19.    @Column(name="cat_noca", nullable=false)
  20.    public int getCatNoca() {return this.catNoca;}
  21.    public void setCatNoca(int catNoca) {this.catNoca = catNoca;}
  22.  
  23.    @Column(name="tra_dat", nullable=false, length=45)
  24.    public String getTradat() {return this.tradat;}
  25.    public void setTradat(String tradat) { this.tradat = tradat;  }
  26.  
  27. }


Este es Su mapeado

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

La 2 Tabla es Categoria que es la principal

Código
  1. package entidad;
  2.  
  3. import javax.persistence.Column;
  4. import javax.persistence.Entity;
  5. import javax.persistence.Id;
  6. import javax.persistence.Table;
  7.  
  8. @Table(name = "trama", catalog = "bd_1"   )
  9.  
  10. public class Categoria  implements java.io.Serializable {
  11.  
  12.     private int catNoca;
  13.     private String catNom;
  14.  
  15.    public Categoria() {}
  16.  
  17.    public Categoria(int catNoca, String catNom)
  18.    {this.catNoca = catNoca;   this.catNom = catNom;}
  19.  
  20.     @Id    
  21.     @Column(name="cat_noca", nullable=false)
  22.    public int getCatNoca() {return this.catNoca;}
  23.    public void setCatNoca(int catNoca) {this.catNoca = catNoca;}
  24.  
  25.    @Column(name="cat_nom", nullable=false, length=45)
  26.    public String getCatNom() {return this.catNom;}
  27.    public void setCatNom(String catNom) { this.catNom = catNom; }    
  28. }

Este es el Mapeado

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

Y este es Strut2 que manda la orden

    
Código
  1. public  void detalle2(String noca,T entity) {
  2.  
  3.        Session session = HibernateUtil.getSessionFactory().openSession();
  4.        Transaction beginTransaction = session.beginTransaction();
  5.        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);  
  6.        beginTransaction.commit();
  7.        session.close();
  8. }

La entidad me lo manda


Código
  1. package acciones;
  2.  
  3. import static com.opensymphony.xwork2.Action.INPUT;
  4. import static com.opensymphony.xwork2.Action.SUCCESS;
  5. import com.opensymphony.xwork2.ActionSupport;
  6. import entidad.Categoria;
  7. import java.util.List;
  8. import servicio.ServicioCategoria;
  9.  
  10. public class CategoriaAction extends ActionSupport {
  11.  
  12.    private ServicioCategoria sc= new ServicioCategoria();
  13.    private List<Categoria> lstCat;
  14.    private Categoria cat;
  15.    private String m;//NO IMPORTANTE PARA LA DUDA
  16.    private String i;//NO IMPORTANTE PARA LA DUDA
  17.    private Integer noca;
  18.  
  19.    @Override
  20.    public String input() throws Exception {
  21.        if (getNoca()!=null) {setCat(sc.find(noca));}
  22.        return INPUT;}      
  23.  
  24.    public String detalle2() throws Exception {
  25.        if (getNoca()!=null) {setCat(sc.find(noca)); }
  26.       return INPUT;}  
  27.  
  28.    @Override
  29.    public String execute() throws Exception {return SUCCESS;}
  30.    public String save() {    sc.save(getCat());     return "ok"; }  
  31.    public String remove(){    sc.remove(getNoca());    return "ok";    }
  32.    public String list() throws Exception { lstCat=sc.findAll();  return execute();}
  33.    public String search() throws Exception { lstCat = sc.search(m);return execute();}
  34.    public String indice() throws Exception { lstCat = sc.search(i);return execute();}
  35.  
  36.  
  37.  
  38.    //<editor-fold defaultstate="collapsed" desc="Getter y Setter">  
  39.  
  40.    public ServicioCategoria getSc() {return sc; }  
  41.    public void setSc(ServicioCategoria sc) {this.sc = sc;}
  42.  
  43.    public String getM() { return m;}
  44.    public void setM(String m) {   this.m = m; }
  45.  
  46.    public String getI() { return i; }
  47.    public void setI(String i) { this.i = i;}
  48.  
  49.    public Integer getNoca() { return noca; }
  50.    public void setNoca(Integer noca) {this.noca = noca;}
  51.  
  52.    public Categoria getCat() {return cat;}
  53.    public void setCat(Categoria cat) { this.cat = cat; }
  54.  
  55.    public List<Categoria> getLstCat() {return lstCat;}
  56.    public void setLstCat(List<Categoria> lstCat) {this.lstCat = lstCat;}
  57.  
  58.  
  59. //</editor-fold>
  60. }

Ayudada por

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




Código
  1. <%@ taglib prefix="s" uri="/struts-tags" %>
  2. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4.    <head>
  5.        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6.        <title>JSP Page</title>
  7.    </head>
  8.    <body>
  9.        <br/>
  10.        <h2>Mopstrar Datos</h2>
  11.        <s:iterator>
  12.        <s:form action="categoriaDetalle2">
  13.            <s:label label="Nombre" name="cat.catNoca" />
  14.            <s:label label="Nombre" name="cat.catNom" />
  15.            <s:label label="La 2 tabla" />
  16.             <s:label label="Nombre de trama" name="tra.Tradat" />          
  17.            <s:label label="Nombre de trama" name="tra.catNom" />    
  18.        </s:form>    
  19.        </s:iterator>
  20.  
  21.    </body>
  22. </html>

El JSP SOLO MUESTRA los datos de la tabla categoria
No Consigo unir las tablas estoy desesperado

Mod: Usar etiquetas GeSHi.
2  Programación / Desarrollo Web / Quisiera pasar de Php a j2ee en: 10 Febrero 2015, 13:19 pm
Quiero hacer una preesentacion en jsp de una select.
Pero quiero presentarla asi.
En cada Celda quiero un registro, que se vea en una tabla un total de x
registros (paguinacion incluida).

El codigo lo he encontrado en Php, pero no se como presentarlo en jsp

Pido ayuda por favor


Código
  1. //CODIGO DE PHP
  2.  
  3. <?PHP
  4. $objConnect = mysql_connect("localhost","root","") or die(mysql_error());
  5. $objDB = mysql_select_db("otaku");
  6.  
  7. $strSQL = "SELECT *FROM serie";
  8.  
  9. $objQuery = mysql_query($strSQL);
  10. $Num_Rows = mysql_num_rows($objQuery);
  11.  
  12. $Per_Page = 8; // Per Page
  13.  
  14. @$Page = $_GET["Page"];
  15. if(!@$_GET["Page"])
  16. {
  17. $Page=1;
  18. }
  19.  
  20. $Prev_Page = $Page-1;
  21. $Next_Page = $Page+1;
  22.  
  23. $Page_Start = (($Per_Page*$Page)-$Per_Page);
  24. if($Num_Rows<=$Per_Page)
  25. {
  26. $Num_Pages =1;
  27. }
  28. else if(($Num_Rows % $Per_Page)==0)
  29. {
  30. $Num_Pages =($Num_Rows/$Per_Page) ;
  31. }
  32. else
  33. {
  34. $Num_Pages =($Num_Rows/$Per_Page)+1;
  35. $Num_Pages = (int)$Num_Pages;
  36. }
  37.  
  38. $strSQL .=" order by id_serie ASC LIMIT $Page_Start , $Per_Page";
  39. $objQuery = mysql_query($strSQL);
  40.  
  41.  
  42. echo"<table border=\"0\" width=\"50%\" align=\"center\" cellspacing=\"1\" cellpadding=\"1\"><tr>";
  43. $intRows = 0;
  44. while($objResult = mysql_fetch_array($objQuery))
  45. {
  46. echo "<td>";
  47. $intRows++;
  48. ?>
  49. <center><img with="150" height="150" src="..\<?=$objResult["imagen_principal"]; ?>"><br>
  50. </br>
  51. <span class="titulo"> <?=$objResult["Nombre"];?></span>
  52.  
  53. </br>
  54. <span class="pequeño"><?=$objResult["Descripcion_p"];?></span><br />
  55. </center>
  56.  
  57. <?php
  58. echo"<a href=\"eliminarserie.php?inombre='".$objResult['id_serie']."'\">ELIMINAR</a>
  59.  
  60. <a href=\"updateserie.php?inombre='".$objResult['id_serie']."'\">UPDATE</a>
  61. <a href=\"fichaserie.php?inombre='".$objResult['id_serie']."'\">FICHA</a>";?>
  62. <?PHP
  63. echo"</td>";
  64. if(($intRows)%4==0)
  65. {
  66. echo"</tr>";
  67. }
  68. }
  69. echo"</tr></table>";
  70. ?>
  71.  
  72. <br>
  73. <span class="paginas">Total
  74. <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :</span>
  75.  
  76. <?PHP
  77. if($Prev_Page)
  78. {
  79. echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> ";
  80. }
  81.  
  82. for($i=1; $i<=$Num_Pages; $i++){
  83. if($i != $Page)
  84. {
  85. echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
  86. }
  87. else
  88. {
  89. echo "<b> $i </b>";
  90. }
  91. }
  92. if($Page!=$Num_Pages)
  93. {
  94. echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next>></a> ";
  95. }
  96. ?>
  97.  
  98. <?PHP
  99. mysql_close($objConnect);
  100. ?> <br />
  101. <a href="serie.php">INSERTAR NUEVO REGISTRO<br />
  102. <a href="listar_galerias.php">Galerias
  103. </a></p>
  104. </body>
  105. </html>

Mod: Etiquetas GeSHi OBLIGATORIAS.
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines