Isuru Gunawardana
Isuru Gunawardana

Reputation: 2887

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save()

I got the above error though i have set the ID manually, Im not using auto generated key here. when i set the key and pass the object to

entityManager.persist(obj);

it gives the above error. any help... plz

thanks

This is the InstallationInfo class, and I got the above error when persisting the installationInfo object. The complete error is

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): se.cambio.cimonitor.jpa.table.ModuleInfo

so I have attached the ModuleInfo class as well

package se.cambio.cimonitor.jpa.table;

@Entity
@Table(name = "InstallationInfo", catalog="CI_Monitor", schema="dbo")
public class InstallationInfo implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private String installationId;
private String timestamp;
private Module moduleByParentId;
private Environment environmentByEnvironmentClientId;
private Environment environmentByEnvironmentServerId;
private Module moduleByBaseLineId;
private String machineName;
private String status;
private String teamName;
private Set<ModuleInfo> moduleInfos = new HashSet<ModuleInfo>(0);

public InstallationInfo() {
}

public InstallationInfo(String installationId) {
    this.installationId = installationId;
}

public InstallationInfo(String installationId,
        Module moduleByParentId,
        Environment environmentByEnvironmentClientId,
        Environment environmentByEnvironmentServerId,
        Module moduleByBaseLineId, String machineName,
        String status, String teamName,
        Set<ModuleInfo> moduleInfos) {
    this.installationId = installationId;
    this.moduleByParentId = moduleByParentId;
    this.environmentByEnvironmentClientId = environmentByEnvironmentClientId;
    this.environmentByEnvironmentServerId = environmentByEnvironmentServerId;
    this.moduleByBaseLineId = moduleByBaseLineId;
    this.machineName = machineName;
    this.status = status;
    this.teamName = teamName;
    this.moduleInfos = moduleInfos;
}

/*@TableGenerator(name="InstlIds", table="InstlPkTb", pkColumnName="InstlId", pkColumnValue="InstlIdVal", allocationSize=1, catalog="CI_Monitor", schema="dbo")
@GeneratedValue(strategy=GenerationType.TABLE, generator="InstlIds")*/
@Id
@Column(name = "InstallationId", unique = true, nullable = false, insertable = true, updatable = true)
public String getInstallationId() {
    return this.installationId;
}

public void setInstallationId(String installationId) {
    this.installationId = installationId;
}

//@Version This is a bug
@Column(name = "Timestamp")
public String getTimestamp() {
    return this.timestamp;
}

public void setTimestamp(String timestamp) {
    this.timestamp = timestamp;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "ParentId")
public Module getModuleByParentId() {
    return this.moduleByParentId;
}

public void setModuleByParentId(Module moduleByParentId) {
    this.moduleByParentId = moduleByParentId;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "EnvironmentClientId")
public Environment getEnvironmentByEnvironmentClientId() {
    return this.environmentByEnvironmentClientId;
}

public void setEnvironmentByEnvironmentClientId(
        Environment environmentByEnvironmentClientId) {
    this.environmentByEnvironmentClientId = environmentByEnvironmentClientId;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "EnvironmentServerId")
public Environment getEnvironmentByEnvironmentServerId() {
    return this.environmentByEnvironmentServerId;
}

public void setEnvironmentByEnvironmentServerId(
        Environment environmentByEnvironmentServerId) {
    this.environmentByEnvironmentServerId = environmentByEnvironmentServerId;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "BaseLineId")
public Module getModuleByBaseLineId() {
    return this.moduleByBaseLineId;
}

public void setModuleByBaseLineId(Module moduleByBaseLineId) {
    this.moduleByBaseLineId = moduleByBaseLineId;
}

@Column(name = "MachineName")
public String getMachineName() {
    return this.machineName;
}

public void setMachineName(String machineName) {
    this.machineName = machineName;
}

@Column(name = "Status")
public String getStatus() {
    return this.status;
}

public void setStatus(String status) {
    this.status = status;
}

@Column(name = "TeamName")
public String getTeamName() {
    return teamName;
}

public void setTeamName(String teamName) {
    this.teamName = teamName;
}

@OneToMany(fetch = FetchType.EAGER, mappedBy = "installationInfo", targetEntity=ModuleInfo.class, cascade={CascadeType.ALL})    //targerEntity is added by Isuru
public Set<ModuleInfo> getModuleInfos() {
    return this.moduleInfos;
}

public void setModuleInfos(Set<ModuleInfo> moduleInfos) {
    this.moduleInfos = moduleInfos;
}

}

package se.cambio.cimonitor.jpa.table;

@Entity
@Table(name = "ModuleInfo", catalog="CI_Monitor", schema="dbo")
public class ModuleInfo implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private ModuleInfoId id;
private InstallationInfo installationInfo;
private Module moduleByModuleId;
private Module moduleByParentId;
private Module moduleByBaseLineId;
private String buildNo;
private String svnRevision;

public ModuleInfo() {
}

public ModuleInfo(ModuleInfoId id, InstallationInfo installationInfo,
        Module moduleByModuleId) {
    this.id = id;
    this.installationInfo = installationInfo;
    this.moduleByModuleId = moduleByModuleId;
}

public ModuleInfo(ModuleInfoId id, InstallationInfo installationInfo,
        Module moduleByModuleId, Module moduleByParentId,
        Module moduleByBaseLineId, String buildNo,
        String svnRevision) {
    this.id = id;
    this.installationInfo = installationInfo;
    this.moduleByModuleId = moduleByModuleId;
    this.moduleByParentId = moduleByParentId;
    this.moduleByBaseLineId = moduleByBaseLineId;
    this.buildNo = buildNo;
    this.svnRevision = svnRevision;
}

@EmbeddedId
@AttributeOverrides({
        @AttributeOverride(name = "moduleId", column = @Column(name = "ModuleId", nullable = false)),
        @AttributeOverride(name = "installationId", column = @Column(name = "InstallationId", nullable = false)) })
public ModuleInfoId getId() {
    return this.id;
}

public void setId(ModuleInfoId id) {
    this.id = id;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "InstallationId", nullable = false, insertable = false, updatable = false, unique = true)
public InstallationInfo getInstallationInfo() {
    return this.installationInfo;
}

public void setInstallationInfo(InstallationInfo installationInfo) {
    this.installationInfo = installationInfo;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "ModuleId", nullable = false, insertable = false, updatable = false)
public Module getModuleByModuleId() {
    return this.moduleByModuleId;
}

public void setModuleByModuleId(Module moduleByModuleId) {
    this.moduleByModuleId = moduleByModuleId;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "ParentId")
public Module getModuleByParentId() {
    return this.moduleByParentId;
}

public void setModuleByParentId(Module moduleByParentId) {
    this.moduleByParentId = moduleByParentId;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "BaseLineId")
public Module getModuleByBaseLineId() {
    return this.moduleByBaseLineId;
}

public void setModuleByBaseLineId(Module moduleByBaseLineId) {
    this.moduleByBaseLineId = moduleByBaseLineId;
}

@Column(name = "BuildNo")
public String getBuildNo() {
    return this.buildNo;
}

public void setBuildNo(String buildNo) {
    this.buildNo = buildNo;
}

@Column(name = "SvnRevision")
public String getSvnRevision() {
    return this.svnRevision;
}

public void setSvnRevision(String svnRevision) {
    this.svnRevision = svnRevision;
}

}

Upvotes: 1

Views: 7824

Answers (1)

gkamal
gkamal

Reputation: 21010

You have a cascade all on ModuleInfos collection, so when you save InstallationInfo it will try to save all the associated ModuleInfos. You need to make sure that the ids of the ModuleInfos are set before saving the InstallationInfo object.

@OneToMany(fetch = FetchType.EAGER, mappedBy = "installationInfo", 
   targetEntity=ModuleInfo.class, cascade={CascadeType.ALL})    //targerEntity is added by Isuru
public Set<ModuleInfo> getModuleInfos() {
    return this.moduleInfos;
}

Upvotes: 2

Related Questions