I'm getting the following exception but I can't figure out why it is happening. Test code is below.
Code:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert explicit value for identity column in table 'Agent' when IDENTITY_INSERT is set to OFF.
The hibernate sql command output for the operation is below. The FDS_Agent_ID is the item causing the problem and I don't know why it is part of the insert statement when MinorStage does not have a similar insert statement and the Id's are defined similarly except that Agent uses a composite primary key.
Code:
configService=net.fractech.ops.fds.db.service.ConfigService@59dbd
Hibernate:
/* insert net.fractech.ops.fds.db.domain.MinorStage
*/ insert
into
Minor_Stage
(End_DateTime, Major_Stage_ID_FK, Stage_Number, Start_DateTime)
values
(?, ?, ?, ?)
Hibernate:
/* insert net.fractech.ops.fds.db.domain.MinorStageConfig
*/ insert
into
Minor_Stage_Configuration
(FDS_Minor_Stage_Configuration_ID, Minor_Stage_ID_FK)
values
(?, ?)
Hibernate:
/* insert net.fractech.ops.fds.db.domain.Agent
*/ insert
into
Agent
(Agent_Name, Agent_Type, FDS_Agent_ID, Minor_Stage_Configuration_ID_FK, Minor_Stage_ID_FK)
values
(?, ?, ?, ?, ?)
ConfigService.java
Code:
@Service
public class ConfigService implements IConfigService {
@Autowired(required = true)
IMinorStageDAO minorStageDAO;
public void processConfigEvent() throws InvalidDatabaseOperationException {
MinorStage minorStage = null;
final int minorStageNumber = 1;
minorStage = new MinorStage();
minorStage.setMajorStageIdFK(10);
minorStage.setStageNumber(minorStageNumber);
minorStage.setStartTimestamp(new Date());
final MinorStageConfig minorStageConfig = new MinorStageConfig();
minorStageConfig.init(5, minorStage);
minorStage.getMinorStageConfigurations().add(minorStageConfig);
final Agent agent = new Agent();
agent.init(minorStageConfig, "id1", "agentName1", "agentType1");
minorStageConfig.getAgents().add(agent);
this.minorStageDAO.save(minorStage);
}
}
MinorStage.java
Code:
@Entity
@Table(name="Minor_Stage")
public class MinorStage implements Serializable {
private static final long serialVersionUID = -7072856325780739098L;
private List<MinorStageConfig> minorStageConfigs = new ArrayList<MinorStageConfig>();
private Date endTimestamp;
private Date startTimestamp;
private Integer id;
private Integer majorStageIdFK;
private Integer stageNumber;
@Column(name="End_DateTime")
@DateTimeFormat(style = "M-")
@Temporal(TemporalType.TIMESTAMP)
public Date getEndTimestamp(){return(this.endTimestamp);}
public void setEndTimestamp(final Date endTimestamp_){this.endTimestamp = endTimestamp_;}
@Id
@Column(name="FDS_Minor_Stage_ID")
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getId(){return(this.id);}
public void setId(final Integer id_){this.id = id_;}
@NotNull
@Column(name="Major_Stage_ID_FK")
public Integer getMajorStageIdFK(){return(this.majorStageIdFK);}
public void setMajorStageIdFK(final Integer majorStageIdFK_){this.majorStageIdFK = majorStageIdFK_;}
@NotNull
@Column(name="Stage_Number")
public Integer getStageNumber(){return(this.stageNumber);}
public void setStageNumber(final Integer stageNumber_){this.stageNumber = stageNumber_;}
@NotNull
@Column(name="Start_DateTime")
@DateTimeFormat(style = "M-")
@Temporal(TemporalType.TIMESTAMP)
public Date getStartTimestamp(){return(this.startTimestamp);}
public void setStartTimestamp(final Date startTimestamp_){this.startTimestamp = startTimestamp_;}
@OneToMany(orphanRemoval=true, cascade=CascadeType.ALL,
mappedBy="minorStage", fetch=FetchType.LAZY)
public List<MinorStageConfig> getMinorStageConfigurations(){return(this.minorStageConfigs);}
public void setMinorStageConfigurations(final List<MinorStageConfig> minorStageConfigs_){this.minorStageConfigs = minorStageConfigs_;}
}
MinorStageConfig.java
Code:
@Configurable
@Entity
@Table(name="Minor_Stage_Configuration")
public class MinorStageConfig implements Serializable
{
private List<Agent> agents = new ArrayList<Agent>();
private MinorStage minorStage;
private MinorStageConfigPK primaryKey;
public MinorStageConfig(){}
public void init(final Integer id_,
final MinorStage minorStage_){
this.primaryKey = new MinorStageConfigPK(id_);
this.setMinorStage(minorStage_);
}
@EmbeddedId
public MinorStageConfigPK getPrimaryKey(){return(this.primaryKey);}
public void setPrimaryKey(final MinorStageConfigPK minorStageConfigPK_){this.primaryKey = minorStageConfigPK_;}
@OneToMany(orphanRemoval=true, cascade=CascadeType.ALL,
mappedBy="minorStageConfig", fetch=FetchType.LAZY)
public List<Agent> getAgents(){return(this.agents);}
public void setAgents(final List<Agent> agents_){this.agents = agents_;}
@NotNull
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="Minor_Stage_ID_FK",
referencedColumnName="FDS_Minor_Stage_ID",
insertable=false,updatable=false)
public MinorStage getMinorStage(){return(this.minorStage);}
public void setMinorStage(final MinorStage minorStage_){
this.minorStage = minorStage_;
if(this.minorStage.getId() != null){this.primaryKey.setMinorStageIdFK(this.minorStage.getId());}
}
}
MinorStageConfigPK.java
Code:
@Configurable
@Embeddable
public class MinorStageConfigPK implements Serializable
{
private Integer id;
private Integer minorStageIdFK;
public MinorStageConfigPK(){}
public MinorStageConfigPK(final Integer minorStageConfigId_){this.id = minorStageConfigId_;}
@Column(name="FDS_Minor_Stage_Configuration_ID")
public Integer getId(){return(this.id);}
public void setId(final Integer id_){this.id = id_;}
@NotNull
@Column(name="Minor_Stage_ID_FK")
public Integer getMinorStageIdFK(){return(this.minorStageIdFK);}
public void setMinorStageIdFK(final Integer minorStageIdFK_){this.minorStageIdFK = minorStageIdFK_;}
public boolean equals(final Object obj_){
if(this == obj_){return(true);}
if(obj_ == null || !(obj_ instanceof MinorStageConfigPK)){return(false);}
final MinorStageConfigPK other = (MinorStageConfigPK)obj_;
if(this.id == null){if(other.id != null){return(false);}}
else if(!this.id.equals(other.id)){return(false);}
if(this.minorStageIdFK == null){if(other.minorStageIdFK != null){return(false);}}
else if(!this.minorStageIdFK.equals(other.minorStageIdFK)){return(false);}
return(true);
}
public int hashCode(){
// generate hashcode based on parameter values
final int prime = 31;
int result = 17;
result = prime * result + (this.id == null ? 0 : this.id.hashCode());
result = prime * result + (this.minorStageIdFK == null ? 0 : this.minorStageIdFK.hashCode());
return(result);
}
}
Agent.java
Code:
@Configurable
@Entity
@Table(name="Agent")
public class Agent implements Serializable
{
private AgentPK primaryKey;
private MinorStageConfig minorStageConfig;
private String id;
private String name;
private String type;
public Agent(){}
public void init(final MinorStageConfig minorStageConfig_,final String id_,final String name_,final String type_)
{
this.primaryKey = new AgentPK(minorStageConfig_);
this.minorStageConfig = minorStageConfig_;
this.id = id_;
this.name = name_;
this.type = type_;
}
@NotNull
@Column(name="Agent_ID",length=50)
public String geId(){return(this.id);}
public void setId(final String id_){this.id = id_;}
@NotNull
@Column(name="Agent_Name",length=50)
public String getName(){return(this.name);}
public void setName(final String name_){this.name = name_;}
@EmbeddedId
public AgentPK getPrimaryKey(){return(this.primaryKey);}
public void setPrimaryKey(final AgentPK agentPK_){this.primaryKey = agentPK_;}
@NotNull
@Column(name="Agent_Type",length=50)
public String getType(){return(this.type);}
public void setType(final String type_){this.type = type_;}
@NotNull
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumns({@JoinColumn(name="Minor_Stage_Configuration_ID_FK",
referencedColumnName="FDS_Minor_Stage_Configuration_ID",
insertable=false,updatable=false),
@JoinColumn(name="Minor_Stage_ID_FK",
referencedColumnName="Minor_Stage_ID_FK",
insertable=false,updatable=false)})
public MinorStageConfig getMinorStageConfig(){return(this.minorStageConfig);}
public void setMinorStageConfig(final MinorStageConfig minorStageConfig_){this.minorStageConfig = minorStageConfig_;}
}
AgentPK.java
Code:
@Configurable
@Embeddable
public class AgentPK implements Serializable {
private Integer id;
private Integer minorStageIdFK;
private Integer minorStageConfigIdFK;
private MinorStageConfig minorStageConfig;
public AgentPK() {}
public AgentPK(final MinorStageConfig minorStageConfig_) {this.minorStageConfig = minorStageConfig_;}
@Column(name="FDS_Agent_ID")
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getId() { return(this.id);}
public void setId(final Integer id_) {this.id = id_;}
@NotNull
@Column(name="Minor_Stage_ID_FK")
public Integer getMinorStageIdFK() {
if(this.minorStageConfig != null){
this.minorStageIdFK = this.minorStageConfig.getPrimaryKey().getMinorStageIdFK();
}
return(this.minorStageIdFK);
}
public void setMinorStageIdFK(final Integer minorStageIdFK_){this.minorStageIdFK = minorStageIdFK_;}
@NotNull
@Column(name="Minor_Stage_Configuration_ID_FK")
public Integer getMinorStageConfigIdFK(){
if(this.minorStageConfig != null){
this.minorStageConfigIdFK = this.minorStageConfig.getPrimaryKey().getId();
}
return(this.minorStageConfigIdFK);
}
public void setMinorStageConfigIdFK(final Integer minorStageConfigIdFK_){this.minorStageConfigIdFK = minorStageConfigIdFK_;}
@Override
public boolean equals(final Object obj_){
if(this == obj_){return(true);}
if(obj_ == null || !(obj_ instanceof AgentPK)){return(false);}
final AgentPK other = (AgentPK)obj_;
if(this.id == null){if(other.id != null){return(false);}}
else if(!this.id.equals(other.id)){return(false);}
if(this.minorStageIdFK == null){
if(other.minorStageIdFK != null){return(false);}
}
else if(!this.minorStageIdFK.equals(other.minorStageIdFK)){return(false);}
if(this.minorStageConfigIdFK == null){
if(other.minorStageConfigIdFK != null){
return(false);
}
}
else if(!this.minorStageConfigIdFK.equals(other.minorStageConfigIdFK)){return(false);}
return(true);
}
public int hashCode() {
final int prime = 31;
int result = 17;
result = prime * result + (this.id == null ? 0 : this.id.hashCode());
result = prime * result + (this.minorStageIdFK == null ? 0 : this.minorStageIdFK.hashCode());
result = prime * result + (this.minorStageConfigIdFK == null ? 0 : this.minorStageConfigIdFK.hashCode());
return(result);
}
}