Hibernate @Id @GeneratedValue (strategy = GenerationType.AUTO) para MSSql

1 resposta
O

Ola !!!

Alguém poderia me ajudar para salvar um objeto Cliente (conforme o codigo abaixo) no banco Microsoft Sql Server 2000 ?

Eu estou começando a estudar o Hibernate agora, então estou tendo muitas dificuldades.

@Entity
@Table(name = "Cliente")
public class Cliente {

	@Id 
	@GeneratedValue (strategy = GenerationType.AUTO)
	private Long id;
	
	@Column(name = "nome_cliente", nullable = false, length=60)
	private String nome;
        ...
        ...

No banco, criei a tabela Cliente com o campo "id" c/ identity, auto incrementando em 1.

Porem, ao tentar salvar o objeto Cliente, a seguinte SQLException é lançada:

Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Cannot insert explicit value for identity column in table 'Cliente' when IDENTITY_INSERT is set to OFF.

Sei que o hibernate está utilizando a seguinte query DDL:

Hibernate: insert into Cliente (id, idDaCategoria, idade_cliente, nome_cliente) values (null, ?, ?, ?)

Se alguém puder me ajudar, ficarei muito grato.

1 Resposta

T

Mude seu codigo para

Criado 9 de fevereiro de 2010
Ultima resposta 9 de fev. de 2010
Respostas 1
Participantes 2