Hiberate Problems, Jdbc Identity_Insert Is Set to Off

Hiberate problems, jdbc IDENTITY_INSERT is set to OFF

You cannot insert into an identity column in SQL Server unless "IDENTITY_INSERT" is set to "ON". Since your generator class is "assigned", Hibernate is assuming that you are setting an explicit value for "id" in Java before saving the object and that Hibernate can directly insert the value into the database. You need to either:

  1. Pick a different generator class, such as "native"
  2. Set IDENTITY_INSERT to "ON"

IDENTITY_INSERT with JPA

You are missing the next line under your id field:

@GeneratedValue(strategy=GenerationType.IDENTITY)

So your id field should look like this:

@Entity
@Table(name = "People")
public class Peeps implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "ID", columnDefinition = "Decimal(10,0)")
private String id;

@Column(name = "PERSON_NAME")
private String name;

}

Second, you are not supposed to set an ID to your entity by yourself, the ID will be generated automatically when you'll persist it using hibernate.

so get rid of: entity.setId("10002");

and just do this:

private EntityManager em = createManager();
private EntityTransaction utx = em.getTransaction();

final Peeps entity = new Peeps();
entity.setName("Joe");
utx.begin();
em.persist(entity);
utx.commit();

something else make sure that you configured your primary key in your DB table, with auto increment.

Cannot persist entity with Spring JPA with InhertanceType.JOINED

It is manually adding the ID with value 11 and not letting the db generate it perhaps because there is only field here the ID. I'm not sure what is going on here.

You are using joined inheritance type. The dependent entity must have the same PK as the parent, otherwise how exactly are they going to be joined? The only way that is going to happen then is if the ID value for the dependent entity is manually set on insert. So therefore you need to change the column definition in the opt_project table so it is not an Identity column.

Cannot insert explicit value for identity column in table 'Employee' when IDENTITY_INSERT is set to OFF

IDENTITY(1,1) means insert the value of that column automatically starting from seed value i.e 1 (before comma) and increment the value by increment i.e. 1 (after comma) whenever a new row is inserted.

If you want the id to be autogenerated then you need to do some changes like

public Employee(String name, String role){    
this.name = name;
this.role = role;
}


Related Topics



Leave a reply



Submit