获取OneToMany映射的org.hibernate.id.IdentifierGenerationException

时间:2014-12-30 06:38:55

标签: hibernate jpa jpa-2.0

尝试保存Project对象时出现以下错误:

org.hibernate.id.IdentifierGenerationException: null id generated for:class com.assetproj.model.ProjectAssetType

我的课程是:

Project.java

@JsonManagedReference
private Set<ProjectAssetType> projectAssetTypes = new HashSet<ProjectAssetType>(
        0);

@OneToMany(fetch = FetchType.LAZY)
@JsonIgnore
@Cascade(CascadeType.SAVE_UPDATE)
@JoinTable(name = "tbl_project_asset_type", joinColumns = { @JoinColumn(name = "projectId") })
public Set<ProjectAssetType> getProjectAssetTypes() {
    return this.projectAssetTypes;
}

public void setProjectAssetTypes(
        Set<ProjectAssetType> projectAssetTypes) {
    this.projectAssetTypes = projectAssetTypes;
}

ProjectAssetType.java

private ProjectAssetTypeId id;
private AssetTypeMaster assetTypeMaster;
@JsonBackReference
private Project project;


@EmbeddedId
@AttributeOverrides({
        @AttributeOverride(name = "projectId", column = @Column(name = "projectId", nullable = false)),
        @AttributeOverride(name = "assetTypeId", column = @Column(name = "assetTypeId", nullable = false)) })
public ProjectAssetTypeId getId() {
    return this.id;
}

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

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "assetTypeId", nullable = false, insertable = false, updatable = false)
public AssetTypeMaster getAssetTypeMaster() {
    return this.assetTypeMaster;
}

public void setAssetTypeMaster(AssetTypeMaster assetTypeMaster) {
    this.assetTypeMaster = assetTypeMaster;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "projectId", nullable = false, insertable = false, updatable = false)
public Project getProject() {
    return this.project;
}

public void setProject(Project project) {
    this.project = project;
}

ProjectAssetTypeId.java

private long projectId;
private int assetTypeId;

public ProjectAssetTypeId() {
}

public ProjectAssetTypeId(long projectId, int assetTypeId) {
    this.projectId = projectId;
    this.assetTypeId = assetTypeId;
}

@Column(name = "projectId", nullable = false)
public long getProjectId() {
    return this.projectId;
}

public void setProjectId(long projectId) {
    this.projectId = projectId;
}

@Column(name = "assetTypeId", nullable = false)
public int getAssetTypeId() {
    return this.assetTypeId;
}

public void setAssetTypeId(int assetTypeId) {
    this.assetTypeId = assetTypeId;
}

所有pojos都是由Hibernate Tools创建的。我的Mapping有问题吗?

1 个答案:

答案 0 :(得分:0)

你没有提到@Id的AutoGeneration策略, 喜欢:

@Id
@GeneratedValue(strategy=GenerationType.AUTO)

所以你需要在Pojo中明确设置ID。