部分复合键上的Hibernate映射

时间:2012-08-30 07:30:04

标签: hibernate partial composite

我有以下表格结构,一对多,多对一 映射到复合键之一。

@Entity
@Table(name = "parent", catalog = "testdb")
public class Parent implements java.io.Serializable {

private Long fileId;

private Set<Children> children = new HashSet<Children>();

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "file_id", unique = true, nullable = false)
public Long getFileId() {
    return this.fileId;
}

public void setFileId(Long fileId) {
    this.fileId = fileId;
}

@OneToMany(mappedBy = "id.childrenIc")
public Set<Children> getChildren() {
    return children;
}

public void setChildren(Set<Children> children) {
    this.children= children;
}
}


@Entity
@Table(name = "children", catalog = "testdb")
public class Children implements java.io.Serializable {
private ChildrenId id;

@EmbeddedId
@AttributeOverrides({
    @AttributeOverride(name = "childId", column = @Column(name = "child_id", nullable = false)),
    @AttributeOverride(name = "childrenIc", column = @Column(name = "child_ic", nullable = false)) })
public ChildrenId getId () {
    return this.id;
}

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


@Embeddable
public class ChildrenId implements java.io.Serializable {
private long childId;
private Children childIc;

@ManyToOne
public Children getChildIc() {
    return childIc;
}

public void setChildIc(Children childIc) {
    this.childIc= childIc;
}

@Column(name = "child_id", nullable = false)
public long getChildId() {
    return this.childId;
}

    public void setChildId(long childId) {
        this.childId= childId;
    }
}


Criteria criteria = session.createCriteria(Parents.class);
Criteria childCri = criteria.createCriteria("childrenIc");
childCri .add(Restrictions.eq("childId", childId));

当我尝试使用条件加入表时,它失败了。 'where子句'中的未知列'this_.childrenIc_child_id' org.hibernate.exception.SQLGrammarException:无法执行查询

即使我改为

Criteria childCri = criteria.createCriteria("id.childrenIc");

它也失败了。

请帮助,谢谢。

0 个答案:

没有答案