java - Can anyone help me with this drawback with Hibernate and JSF? -


i have problem hibernate when updating data in table.

if generate new record, recorded in table. if want perform update of primary key, throw following error if scope of managed bean "requestscoped": "batch update returned unexpected row count update [0]; current row count: 0; expected: 1"

if scope of managed bean "viewscoped", error follows: "could not extract resultset".

if modify data, record recorded satisfactorily.

i understand can permissions problem modify primary key, since in turn foreign key table, last table not contain records yet. if pgadmin change primary key, can without problem, therefore not referential integrity problem.

this puzzles me lot.

i add pojo code:

import java.util.date; import java.util.hashset; import java.util.set;  /**  * combustibles generated hbm2java  */ public class combustibles  implements java.io.serializable {    private int idcombustible;  private string descripcion;  private boolean baja;  private date fechabaja;  private set valeses = new hashset(0);  public combustibles() { }   public combustibles(int idcombustible, string descripcion, boolean baja) {     this.idcombustible = idcombustible;     this.descripcion = descripcion;     this.baja = baja; } public combustibles(int idcombustible, string descripcion, boolean baja, date fechabaja, set valeses) {    this.idcombustible = idcombustible;    this.descripcion = descripcion;    this.baja = baja;    this.fechabaja = fechabaja;    this.valeses = valeses; }  public int getidcombustible() {     return this.idcombustible; }  public void setidcombustible(int idcombustible) {     this.idcombustible = idcombustible; } public string getdescripcion() {     return this.descripcion; }  public void setdescripcion(string descripcion) {     this.descripcion = descripcion; } public boolean isbaja() {     return this.baja; }  public void setbaja(boolean baja) {     this.baja = baja; } public date getfechabaja() {     return this.fechabaja; }  public void setfechabaja(date fechabaja) {     this.fechabaja = fechabaja; } public set getvaleses() {     return this.valeses; }  public void setvaleses(set valeses) {     this.valeses = valeses; } } 

also mapping file:

<?xml version="1.0"?> <!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- generated 24/03/2017 21:42:13 hibernate tools 4.3.1 --> <hibernate-mapping>     <class name="pojos.combustibles" table="combustibles" schema="public" optimistic-lock="version">     <id name="idcombustible" type="int">         <column name="idcombustible" />         <generator class="assigned" />     </id>     <property name="descripcion" type="string">         <column name="descripcion" not-null="true" />     </property>     <property name="baja" type="boolean">         <column name="baja" not-null="true" />     </property>     <property name="fechabaja" type="time">         <column name="fechabaja" length="15" />     </property>     <set name="valeses" table="vales" inverse="true" lazy="true" fetch="select">         <key>             <column name="idcombustible" not-null="true" />         </key>         <one-to-many class="pojos.vales" />     </set> </class> </hibernate-mapping> 

i add code of managed bean, inside method called modifycombustible (), in charge of update.

package managedbeansrequest;  import daos.daocombustibles; import hibernateutil.hibernateutil; import pojos.combustibles; import java.util.list; import javax.inject.named; import javax.enterprise.context.requestscoped; import javax.faces.application.facesmessage; import javax.faces.context.facescontext; import org.hibernate.session; import org.hibernate.transaction;  /**  *  * @author gustavo  */ @named(value = "mbcombustibles") @requestscoped public class mbcombustibles {  private combustibles combustible; private list<combustibles> listacombustibles; private session sesion; private transaction transaccion;  /**  * creates new instance of mbcombustibles  */ public mbcombustibles() {     this.combustible = new combustibles(); }  public void registrar() throws exception{     //antes era public string, pero se cambió "void" ya que en la vista se cambió el "action" que requiere una cadena, por "actionlistener"     this.sesion = null;     this.transaccion = null;      try{          this.sesion = hibernateutil.getsessionfactory().opensession();         this.transaccion = this.sesion.begintransaction();          daocombustibles daoc = new daocombustibles();         daoc.registrar(this.sesion, this.combustible);          this.transaccion.commit();          facescontext.getcurrentinstance().addmessage(null, new facesmessage(facesmessage.severity_info, "registro","se registró satisfactoriamente el combustible"));          //requestcontext.getcurrentinstance().execute("limpiarformulario('frmregistrarcombustible')");         this.combustible = new combustibles();  //esto reemplaza la función javascript de borrado de campos del formulario, debido que al instanciar un nuevo objeto, viene con sus atributos limpios          //return "/combustibles/combustiblealta"; //se reemplaza el return, ya que en la vista se cambió el "action" que requiere una cadena, por "actionlistener"       }catch(exception e){         if(this.transaccion != null){             this.transaccion.rollback();             facescontext.getcurrentinstance().addmessage(null, new facesmessage(facesmessage.severity_error, "ocurrió un error","descripcion: " + e.getmessage()));         }         return;  //se reemplaza el return "null", ya que en la vista se cambió el "action" que requiere una cadena, por "actionlistener"     }     finally{         if(sesion != null){             sesion.close();         }      }  }  public list<combustibles> gettodos(){      this.sesion = null;     this.transaccion = null;      try{          daocombustibles daoc = new daocombustibles();          this.sesion = hibernateutil.getsessionfactory().opensession();         this.transaccion = this.sesion.begintransaction();          this.listacombustibles = daoc.vertodos(this.sesion);          this.transaccion.commit();          return listacombustibles;      }catch(exception e){         if(this.transaccion != null){             this.transaccion.rollback();         }         return null;     }finally{         if(this.sesion != null){             this.sesion.close();         }     } }  public void modificarcombustible() throws exception{     //antes era public string, pero se cambió "void" ya que en la vista se cambió el "action" que requiere una cadena, por "actionlistener"     this.sesion = null;     this.transaccion = null;      try{          this.sesion = hibernateutil.getsessionfactory().opensession();         this.transaccion = this.sesion.begintransaction();          daocombustibles daoc = new daocombustibles();         daoc.modificar(this.sesion, this.combustible);          this.transaccion.commit();          facescontext.getcurrentinstance().addmessage(null, new facesmessage(facesmessage.severity_info, "registro","se guardaron satisfactoriamente los cambios"));          //requestcontext.getcurrentinstance().execute("limpiarformulario('frmregistrarcombustible')");         //this.combustible = new combustibles();  //esto reemplaza la función javascript de borrado de campos del formulario, debido que al instanciar un nuevo objeto, viene con sus atributos limpios          //return "/combustibles/combustiblealta"; //se reemplaza el return, ya que en la vista se cambió el "action" que requiere una cadena, por "actionlistener"       }catch(exception e){         if(this.transaccion != null){             facescontext.getcurrentinstance().addmessage(null, new facesmessage(facesmessage.severity_error, "ocurrió un error","descripcion: " + e.getmessage()));             this.transaccion.rollback();         }     }     finally{         if(sesion != null){             sesion.close();         }      }  }  public combustibles getcombustible() {     return combustible; }  public void setcombustible(combustibles combustible) {     this.combustible = combustible; }  public list<combustibles> getlistacombustibles() {     return listacombustibles; }  public void setlistacombustibles(list<combustibles> listacombustibles) {     this.listacombustibles = listacombustibles; } } 

for clarity add code portion of view called dialog of primefaces, , complete code of dialog:

<p:column>     <p:commandbutton value="editar" oncomplete="pf('dialogoeditarcombustible').show()" update=":frmeditarcombustible">         <f:setpropertyactionlistener target="#{mbcombustibles.combustible}" value="#{fila}"/>      </p:commandbutton> </p:column>   <h:form id="frmeditarcombustible">     <p:dialog header="editar combustible" widgetvar="dialogoeditarcombustible" modal="true" resizable="false" width="900" showeffect="explode" hideeffect="explode" >         <p:panelgrid id="editarcombustible" columns="3">                 <p:outputlabel value="identificador de combustible:" for="txtidentificador"/>                 <p:inputtext id="txtidentificador" label="identificador" value="#{mbcombustibles.combustible.idcombustible}">                     <f:validator validatorid="validadorvacio"/>                 </p:inputtext>                 <p:message for="txtidentificador"/>                 <p:outputlabel value="nombre de combustible:" for="txtdescripcion"/>                  <p:inputtext id="txtdescripcion" label="nombre" value="#{mbcombustibles.combustible.descripcion}">                      <f:validator validatorid="validadorvacio"/>                  </p:inputtext>                  <p:message for="txtdescripcion"/>                  <p:commandbutton value="confirmar edición" actionlistener="#{mbcombustibles.modificarcombustible()}" update=":frmlistacombustibles,editarcombustible"/>          </p:panelgrid>      </p:dialog> </h:form> 

while might find platforms permit change of table row's primary key value, not acceptable practice relational compliant database.

the accepted way modify row's pk value first disable or drop pk on table owns row. may have consequence other table has fk constraint may have altered disable or drop fk prior to. once done, can make changes necessary pk or values without database problem. once changes complete, you'll need re-add or enable pk followed associated fks.

that's nature of relational database has nothing hibernate.

i believe (i'd have double check code absolute) hibernate default specifies pk columns insertable=true, updatable=false. in other words, assumes relational compliance, pks don't change unless manual work occurs described above.

so if attempt modify value in java code, hibernate silently ignores column update , error see result of such.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -