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:
- Pick a different generator class, such as "native"
- 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
Select All Projects That Have Matching Tags
Attaching an Mdf File Without Ldf File
Sql Server Bug or Feature? Decimal Numbers Conversion
Sql Dynamic Order by Using Alias
Sql Select Rows Containing Part of String
How Do The SQL "Is" and "=" Operators Differ
How to Get First and Last Day of Week in Oracle
Standard SQL Alternative to Oracle Decode
Db (Sql) Automated Stress/Load Tools
Get The Type of a Variable in MySQL
Date Range for Set of Same Data
Delete Duplicate Records Using Rownum in Sql
Sql 2005 How to Use Keyword Like in a Case Statement
How to Select Most Frequent Value in a Column Per Each Id Group