我正在尝试使用标准从多对一关系映射字段中选择值。但是我遇到了错误org.hibernate.QueryException: could not resolve property:part_id of:。请查看我的pojo课堂,并在这里告知问题所在。

Criteria partCriteria = session.createCriteria(PartFeatureVersion.class);
partCriteria.add(Restrictions.eq("part_id",part.getPart_id()));


@Entity
@Table(name="DBO.PART_FEATURE_VERSION")
public class PartFeatureVersion {
private Part part;

@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="part_id")
    public Part getPart() {
        return part;
    }


@Entity
@Table(name="DBO.PART")
public class Part {

private String part_id;
    private Set<PartFeatureVersion> partfeatureversion = new HashSet<PartFeatureVersion>(0);

    @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="PART_ID")
public String getPart_id() {
    return part_id;
}

    @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="part")
public Set<PartFeatureVersion> getPartfeatureversion() {
    return partfeatureversion;
}


如果在PartFeatureVersion pojo类中创建getter / setter,则给出错误为org.hibernate.MappingException: Repeated column in mapping for entity:PART_ID (should be mapped with insert="false" update="false")

最佳答案

更改以下代码:

partCriteria.add(Restrictions.eq("part_id",part.getPart_id()));


变成:

partCriteria.add(Restrictions.eq("part", part));


您的代码中的条件基于PartFeatureVersion类。您正在基于PartFeatureVersion.part_id属性限制条件。问题是您的PartFeatureVersion类没有名为part_id的属性。

07-24 09:34