Reputation: 268
I have the following JPA entity class. And another Entity OptProject that extends Project. I'm using InheritanceType.JOINED
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", nullable = false)
private Long id;
@Basic
@Column(name = "NAME", nullable = false, length = 150)
private String name;
@Basic
@Column(name = "DESCRIPTION", nullable = false, length = 500)
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
@Entity
@Table( name = "opt_project")
public class OptProject extends Project {
@OneToMany(mappedBy = "project", cascade = {CascadeType.ALL})
private List<Case> cases = new ArrayList<>();
public OptProject() {}
public OptProject(String name, String description) {
super(name, description);
}
}
The JPA repository for OptProject entity is the following
@Repository
public interface OptProjectRepository extends CrudRepository<OptProject, Long> {
}
When I create a OptProject and try to save it, I get the error message below. It makes zero sense to me since I'm using @GeneratedValue(strategy = GenerationType.IDENTITY) for SQL server to generate the ID primary key.
OptProject optProject = new OptProject("name", "description");
OptProject savedProject = optProjectRepo.save(optProject);
com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert explicit value for identity column in table 'optimization_project' when IDENTITY_INSERT is set to OFF.
After turning hibernate logging to debug with the following snippets
logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql=trace
These are the hibernate SQL logs:
DEBUG org.hibernate.SQL - insert into dbo.project (name, description) values (?, ?)
TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [name]
TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [VARCHAR] - [description]
DEBUG org.hibernate.SQL - insert into opt_project (id) values (?)
TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [11]
WARN o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 544, SQLState: S0001
ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Cannot insert explicit value for identity column in table 'opt_project' when IDENTITY_INSERT is set to OFF.
The first SQL statement to insert the data into the table for the base class Project is correct. But the SQL statement to insert the data to the second table for the child class OptProject is the issue. 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.
Upvotes: 1
Views: 902
Reputation: 23246
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.
Upvotes: 2