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
master
aristides 2018-06-13 22:51:53 +00:00
parent 79fdc2750b
commit 9fe3940548
3 changed files with 86 additions and 3 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -18,6 +18,9 @@ import javax.persistence.OneToMany;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Temporal; import javax.persistence.Temporal;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.SequenceGenerator; import javax.persistence.SequenceGenerator;
/** /**
@ -32,10 +35,11 @@ public class ClaseServicio implements Serializable {
public final static short TODOS = -1; public final static short TODOS = -1;
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@Id @Id
@Basic(optional = false) @GenericGenerator(name = "claseservicioIdgen", strategy = "com.rjconsultores.ventaboletos.dao.util.ClaseServicioGenerator")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "CLASE_SERVICIO_SEQ") @GeneratedValue(generator = "claseservicioIdgen")
@Column(name = "CLASESERVICIO_ID") @Column(name = "CLASESERVICIO_ID")
private Integer claseservicioId; private Integer claseservicioId;
@Column(name = "DESCCLASE") @Column(name = "DESCCLASE")
private String descclase; private String descclase;
@ -75,6 +79,8 @@ public class ClaseServicio implements Serializable {
this.claseservicioId = claseservicioId; this.claseservicioId = claseservicioId;
this.fecmodif = fecmodif; this.fecmodif = fecmodif;
} }
public Integer getClaseservicioId() { public Integer getClaseservicioId() {
return claseservicioId; return claseservicioId;