Chave primaria de banco de dados legados no Hibernate

0 respostas
N

Bom dia pessoal !!!

Estou tentando gerar a chave primaria de uma tabela pesquisando o ultimo id cadastrado e incrementado 1 atraves do comando Select max(),
estou usando as anotacoes GenericGenerator e GeneratedValue.
Mas estou tendo alguns problemas que nao consigo entender… Já pesquisei bastante incluisive na documentacao, peguei varios exemplos e ainda nao
consegui entender o motivo dos erros.

Se alguem puder me dar uma luz eu agradeco muito !!!

Quando retorno um valor inteiro tomo essa exception:

"Hibernate flushing: could not insert: [br.com.atenainformatica.cetus.modelo.CadRamo]; uncategorized SQLException for SQL [insert into CAD_RAMO (datupdate, descricao, inativo, id) values (?, ?, ?, ?)]; SQL state [HY000]; error code [335544347]; GDS Exception. 335544347. validation error for column ID, value “*** null "; nested exception is org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544347. validation error for column ID, value " null ***” "

Quando retorno um valor string tomo essa exception:

“could not set a field value by reflection setter of br.com.atenainformatica.cetus.modelo.CadRamo.id; nested exception is org.hibernate.PropertyAccessException: could not set a field value by reflection setter of br.com.atenainformatica.cetus.modelo.CadRamo.id

Esta é minha classe:

@Entity
@GenericGenerator(name = "geradorID", strategy="br.com.atenainformatica.cetus.spring.dao.util.Generator")  
@Table(name="CAD_RAMO")
public class CadRamo implements Serializable {
	@Id
	@GeneratedValue(generator = "geradorID")  	
	private Integer id;
	@NotEmpty
	@NotNull
	@Length(min=4, max=30)
	@Column(length=30, columnDefinition="varchar(30)", nullable=false)
	private String descricao;
	@NotEmpty
	@NotNull
	@Column(length=1, columnDefinition="char(1)", nullable=false)
	private String inativo;
	@Temporal(TemporalType.TIMESTAMP)
	private Calendar datupdate;
	
	public CadRamo() {
		// TODO Auto-generated constructor stub
		setId(-1);
		setInativo("F");
	}

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getDescricao() {
		return descricao;
	}
	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}
	public String getInativo() {
		return inativo;
	}
	public void setInativo(String inativo) {
		this.inativo = inativo;
	}
	public Calendar getDatupdate() {
		return datupdate;
	}
	public void setDatupdate(Calendar datupdate) {
		this.datupdate = datupdate;
	}
}

Esta é a classe do Generator:

public class Generator implements Configurable, IdentifierGenerator {
	private String entity;
	private String fieldMax;
	private String parameters, parameters2;

	
	public Generator() {
		// TODO Auto-generated constructor stub
	}
	
	public Serializable generate(SessionImplementor session, Object o)
			throws HibernateException {
		// TODO Auto-generated method stub
		String sql = null;
		String id = "0";
		Session sessao = session.getFactory().openSession();
		
		if (CadRamo.class.getName().equals(entity)) {
			sql = "SELECT max(" + fieldMax + ") + 1 as " + fieldMax + "  FROM " + entity + " as CadRamo";
			
		}
		
		id = (String) ((Session) session).createQuery(sql).uniqueResult().toString();
		Integer i = Integer.parseInt(id);
		return i;
	}

	@Override
	public void configure(Type type, Properties params, Dialect dialect)
			throws MappingException {
		// TODO Auto-generated method stub
		this.fieldMax = params.getProperty(PersistentIdentifierGenerator.PK);
		this.entity = params.getProperty(PersistentIdentifierGenerator.ENTITY_NAME);
		this.parameters = params.getProperty("kgrupo");
		this.parameters2 = params.getProperty("kclasse");
	}

}
Criado 24 de agosto de 2011
Respostas 0
Participantes 1