Problem with entering data with hibernate in java

0

When I enter a data via jsp with hibernate and mysql, this error is thrown at me:

  

Exception Report Type

     

message Execution of the Servlet launched an exception

     

description The server encountered an internal error that caused it not to   I could fill this requirement.

     

exception

     

javax.servlet.ServletException: The Servlet execution launched a   exception     org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:53)   root cause

     

java.lang.ExceptionInInitializerError     Driver.NewHibernateUtil. (NewHibernateUtil.java:29)     model.OperationsSoftware.addTypeSw (OperationsSoftware.java:22)     controller.ServletSw.processRequest (ServletSw.java:40)     Driver.ServletSw.doPost (ServletSw.java:75)     javax.servlet.http.HttpServlet.service (HttpServlet.java:660)     javax.servlet.http.HttpServlet.service (HttpServlet.java:741)     org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:53)   root cause

     

org.hibernate.MappingException: Could not instantiate persister   org.hibernate.persister.entity.SingleTableEntityPersister     org.hibernate.persister.internal.PersisterFactoryImpl.create (PersisterFactoryImpl.java:174)     org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister (PersisterFactoryImpl.java:135)     org.hibernate.internal.SessionFactoryImpl. (SessionFactoryImpl.java:400)     org.hibernate.cfg.Configuration.buildSessionFactory (Configuration.java:1857)     org.hibernate.cfg.Configuration.buildSessionFactory (Configuration.java:1928)     Driver.NewHibernateUtil. (NewHibernateUtil.java:25)     model.OperationsSoftware.addTypeSw (OperationsSoftware.java:22)     controller.ServletSw.processRequest (ServletSw.java:40)     Driver.ServletSw.doPost (ServletSw.java:75)     javax.servlet.http.HttpServlet.service (HttpServlet.java:660)     javax.servlet.http.HttpServlet.service (HttpServlet.java:741)     org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:53)   root cause

     

java.lang.ClassCastException: org.hibernate.type.StringType can not be   cast to org.hibernate.type.VersionType     org.hibernate.tuple.PropertyFactory.buildVersionProperty (PropertyFactory.java:181)     org.hibernate.tuple.entity.EntityMetamodel. (EntityMetamodel.java:218)     org.hibernate.persister.entity.AbstractEntityPersister. (AbstractEntityPersister.java:520)     org.hibernate.persister.entity.SingleTableEntityPersister. (SingleTableEntityPersister.java:148)     sun.reflect.GeneratedConstructorAccessor20.newInstance (Unknown   Source)     sun.reflect.DelegatingConstructorAccessorImpl.newInstance (DelegatingConstructorAccessorImpl.java:45)     java.lang.reflect.Constructor.newInstance (Constructor.java:423)     org.hibernate.persister.internal.PersisterFactoryImpl.create (PersisterFactoryImpl.java:163)     org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister (PersisterFactoryImpl.java:135)     org.hibernate.internal.SessionFactoryImpl. (SessionFactoryImpl.java:400)     org.hibernate.cfg.Configuration.buildSessionFactory (Configuration.java:1857)     org.hibernate.cfg.Configuration.buildSessionFactory (Configuration.java:1928)     Driver.NewHibernateUtil. (NewHibernateUtil.java:25)     model.OperationsSoftware.addTypeSw (OperationsSoftware.java:22)     controller.ServletSw.processRequest (ServletSw.java:40)     Driver.ServletSw.doPost (ServletSw.java:75)     javax.servlet.http.HttpServlet.service (HttpServlet.java:660)     javax.servlet.http.HttpServlet.service (HttpServlet.java:741)     org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:53)

The code of the method is as follows

public void addTipoSw(TipoSoftware sw)
{
    SessionFactory sesion = NewHibernateUtil.getSessionFactory();
    Session session = sesion.openSession();
    Transaction tx = session.beginTransaction();



    session.save(sw);

    tx.commit();
    session.close();
}

The mapping of the tables is:

<?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 12-05-2018 23:47:57 by Hibernate Tools 4.3.1 -->
 <hibernate-mapping>
 <class name="modelo.TipoSoftware" table="tipo_software" catalog="mydb" 
 optimistic-lock="version">
    <id name="id" type="java.lang.Integer">
        <column name="id" />
        <generator class="identity" />
    </id>
    <property name="tipo" type="string">
        <column name="tipo" length="45" not-null="true" />
    </property>
    <set name="softwares" table="software" inverse="true" lazy="true" 
       fetch="select">
        <key>
            <column name="tipo_software_id" not-null="true" />
        </key>
        <one-to-many class="modelo.Software" />
        </set>
        </class>
         </hibernate-mapping>

The hibernate configuration is:

<hibernate-configuration>
<session-factory>
<property 
 name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property 
 name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb? 
 zeroDateTimeBehavior=convertToNull</property>
 <property name="hibernate.connection.username">root</property>
 <property name="hibernate.connection.password">asd</property>
 <mapping resource="modelo/Modelo.hbm.xml"/>
 <mapping resource="modelo/Representante.hbm.xml"/>
 <mapping resource="modelo/TipoSoftware.hbm.xml"/>
 <mapping resource="modelo/Privilegio.hbm.xml"/>
 <mapping resource="modelo/Venta.hbm.xml"/>
 <mapping resource="modelo/Relacion.hbm.xml"/>
 <mapping resource="modelo/Hardware.hbm.xml"/>
 <mapping resource="modelo/TipoRelacion.hbm.xml"/>
 <mapping resource="modelo/Empresa.hbm.xml"/>
 <mapping resource="modelo/TipoEmpresa.hbm.xml"/>
 <mapping resource="modelo/TipoArriendo.hbm.xml"/>
 <mapping resource="modelo/TipoHardware.hbm.xml"/>
 <mapping resource="modelo/Inventario.hbm.xml"/>
 <mapping resource="modelo/DetalleCompra.hbm.xml"/>
 <mapping resource="modelo/DetalleArriendo.hbm.xml"/>
 <mapping resource="modelo/Ejemplo.hbm.xml"/>
 <mapping resource="modelo/DetalleVenta.hbm.xml"/>
 <mapping resource="modelo/Arriendo.hbm.xml"/>
 <mapping resource="modelo/Equipo.hbm.xml"/>
 <mapping resource="modelo/Producto.hbm.xml"/>
 <mapping resource="modelo/DetalleMovimiento.hbm.xml"/>
 <mapping resource="modelo/RegistroMovimiento.hbm.xml"/>
 <mapping resource="modelo/Compra.hbm.xml"/>
 <mapping resource="modelo/Fabricante.hbm.xml"/>
 <mapping resource="modelo/TipoEquipo.hbm.xml"/>
 <mapping resource="modelo/Cargo.hbm.xml"/>
 <mapping resource="modelo/Empleo.hbm.xml"/>
 <mapping resource="modelo/Bodega.hbm.xml"/>
 <mapping resource="modelo/BitacoraAccion.hbm.xml"/>
 <mapping resource="modelo/Rol.hbm.xml"/>
 <mapping resource="modelo/TipoMovimiento.hbm.xml"/>
 <mapping resource="modelo/Ciudad.hbm.xml"/>
 <mapping resource="modelo/Pais.hbm.xml"/>
 <mapping resource="modelo/Sucursal.hbm.xml"/>
 <mapping resource="modelo/Usuario.hbm.xml"/>
 <mapping resource="modelo/Estado.hbm.xml"/>
 <mapping resource="modelo/Software.hbm.xml"/>
 <mapping resource="modelo/Bitacora.hbm.xml"/>
 </session-factory>

The jsp contains the following

<form name="form" action="<%=request.getContextPath()%>/servletSw" method="post">

        <div>
            <label>Ingrese el tipo de software</label>
            <input type="text" name="tipo"/>


        </div>

        <button type="submit">Enviar</button>




    </form>

The sql structure of the table type_software is:

CREATE TABLE 'tipo_software' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'tipo' varchar(45) NOT NULL,
 PRIMARY KEY ('id')
 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

My servlet:

    OperacionesSoftware op = new OperacionesSoftware();


    op.addTipoSw(new TipoSoftware(request.getParameter("tipo")));
    
asked by Camilo Jesus 13.05.2018 в 05:39
source

1 answer

0

Waiting for you to attach what I have asked for in the comment, I would say that the problem is that you have declared, at the table level, the version column of type VARCHAR, when Hibernate expects it to be a numeric type. Notice that in the stacktrace you have the message:

java.lang.ClassCastException: org.hibernate.type.StringType cannot be cast to org.hibernate.type.VersionType

Broad response based on what you have answered. To manage optimistic locking, Hibernate can use a column to save a version number or to save a timestamp (in your case, it's version number). Both options require that the physical table have a column of the corresponding type (numeric if it is a version or DATETIME if it is timestamp).

Therefore, and in summary, in the mapping of your TypeSoftware entity you should:

<version name="version" column="<nombre-column-control-version-hibernate>" type="long" />

If I remember correctly, if you do not add that definition, Hibernate looks for a column called version.

That on the one hand. On the other hand, in one of the comments you indicate that the Software table does have a version attribute of type varchar. I understand that this field version is typical of your logical model (that is, it is the software version, it has nothing to do with Hibernate), but if in the mapping of that table you have also added the optimistic lock control, the reason for the exception you have put in is there. Hibernate looks for a column called version, finds yours, tries to use it but, instead of being numeric, it is a varchar and hence the exception.

    
answered by 13.05.2018 / 08:36
source