尽管我手动设置了ID,但出现了以上错误,我在这里没有使用自动生成的密钥。
当我设置键并将对象传递给

entityManager.persist(obj);


它给出了以上错误。
任何帮助...请

谢谢

这是InstallationInfo类,在保留installationInfo对象时出现了以上错误。完整的错误是

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


所以我也附加了ModuleInfo类

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;
}


}

最佳答案

您在ModuleInfos集合上具有所有的层叠,因此当您保存InstallationInfo时,它将尝试保存所有关联的ModuleInfos。保存InstallationInfo对象之前,需要确保已设置ModuleInfos的ID。

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

08-26 05:14