From 9fe3940548806dc9925b2d70fe545896075fcb31 Mon Sep 17 00:00:00 2001 From: aristides Date: Wed, 13 Jun 2018 22:51:53 +0000 Subject: [PATCH] Alterando a forma de gerar o id para a clase_Sevicio fixes bug#11283 dev:gleimar qua:Victor git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Model/trunk/modelWeb@82614 d1611594-4594-4d17-8e1d-87c2c4800839 --- .../dao/util/ClaseServicioGenerator.java | 23 ++++++++ .../dao/util/ClaseServicioStore.java | 54 +++++++++++++++++++ .../ventaboletos/entidad/ClaseServicio.java | 12 +++-- 3 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 src/com/rjconsultores/ventaboletos/dao/util/ClaseServicioGenerator.java create mode 100644 src/com/rjconsultores/ventaboletos/dao/util/ClaseServicioStore.java diff --git a/src/com/rjconsultores/ventaboletos/dao/util/ClaseServicioGenerator.java b/src/com/rjconsultores/ventaboletos/dao/util/ClaseServicioGenerator.java new file mode 100644 index 000000000..f0266d60d --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/dao/util/ClaseServicioGenerator.java @@ -0,0 +1,23 @@ +package com.rjconsultores.ventaboletos.dao.util; + +import java.io.Serializable; + +import org.apache.log4j.Logger; +import org.hibernate.HibernateException; +import org.hibernate.engine.SessionImplementor; +import org.hibernate.id.IdentifierGenerator; + +public class ClaseServicioGenerator implements IdentifierGenerator { + private static Logger log = Logger.getLogger(ClaseServicioGenerator.class); + + @Override + public Serializable generate(SessionImplementor session, Object object) throws HibernateException { + // TODO Auto-generated method stub + log.info("Inicio geração ClaseServicioID"); + Serializable nextId = ClaseServicioStore.getInstance().getNextId(session.connection()); + log.info("Fim geração ClaseServicioID:" + nextId); + + return nextId; + } + +} diff --git a/src/com/rjconsultores/ventaboletos/dao/util/ClaseServicioStore.java b/src/com/rjconsultores/ventaboletos/dao/util/ClaseServicioStore.java new file mode 100644 index 000000000..ca6111a79 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/dao/util/ClaseServicioStore.java @@ -0,0 +1,54 @@ +package com.rjconsultores.ventaboletos.dao.util; + +import java.io.Serializable; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.apache.log4j.Logger; +import org.hibernate.HibernateException; + +public class ClaseServicioStore { + + private static Logger log = Logger.getLogger(ClaseServicioStore.class); + private static ClaseServicioStore INSTANCE = new ClaseServicioStore(); + protected int qtdMaxima = 99; + + private ClaseServicioStore() { + + } + + public static ClaseServicioStore getInstance() { + return INSTANCE; + } + + public synchronized Serializable getNextId(Connection connection) throws HibernateException { + + log.info(String.format("qtdMaxima:%s", qtdMaxima)); + Integer idEncontrado = 0; + PreparedStatement ps; + + try { + ps = connection.prepareStatement("select min(a.CLASESERVICIO_ID) as idEncontrado from CLASE_SERVICIO a left join CLASE_SERVICIO b on b.CLASESERVICIO_ID = (a.CLASESERVICIO_ID +1) where b.CLASESERVICIO_ID is null and a.CLASESERVICIO_ID<>-1"); + ResultSet rs = ps.executeQuery(); + rs.next(); + idEncontrado = rs.getInt("idEncontrado"); + if (idEncontrado == null) + idEncontrado = 1; + + } catch (SQLException e) { + log.error("erro ao buscar id clase_servicio", e); + throw new HibernateException(e); + } + + log.info("indiceAtual=" + idEncontrado); + idEncontrado = idEncontrado + 1; + log.info("idEncontrado=" + idEncontrado); + if (idEncontrado > qtdMaxima) { + throw new HibernateException("A qtd de IDs permitidos para clase_servicio foi atingido:" + qtdMaxima); + } + return idEncontrado; + } + +} diff --git a/src/com/rjconsultores/ventaboletos/entidad/ClaseServicio.java b/src/com/rjconsultores/ventaboletos/entidad/ClaseServicio.java index be466f94a..e6b50ddfb 100644 --- a/src/com/rjconsultores/ventaboletos/entidad/ClaseServicio.java +++ b/src/com/rjconsultores/ventaboletos/entidad/ClaseServicio.java @@ -18,6 +18,9 @@ import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; + +import org.hibernate.annotations.GenericGenerator; + import javax.persistence.SequenceGenerator; /** @@ -32,10 +35,11 @@ public class ClaseServicio implements Serializable { public final static short TODOS = -1; private static final long serialVersionUID = 1L; + @Id - @Basic(optional = false) - @GeneratedValue(strategy = GenerationType.AUTO, generator = "CLASE_SERVICIO_SEQ") - @Column(name = "CLASESERVICIO_ID") + @GenericGenerator(name = "claseservicioIdgen", strategy = "com.rjconsultores.ventaboletos.dao.util.ClaseServicioGenerator") + @GeneratedValue(generator = "claseservicioIdgen") + @Column(name = "CLASESERVICIO_ID") private Integer claseservicioId; @Column(name = "DESCCLASE") private String descclase; @@ -75,6 +79,8 @@ public class ClaseServicio implements Serializable { this.claseservicioId = claseservicioId; this.fecmodif = fecmodif; } + + public Integer getClaseservicioId() { return claseservicioId;