3

我目前正在努力使用休眠中的注释进行一对多的映射,每当我获得一个对象时,从相关表返回的项目集始终为空(即使我可以在数据库中看到与数据的对应关系)。我在同一个班级中有一些多对一的关系,它们运行良好。

为便于阅读,省略了其他不相关的代码

我有两个表,其中一个成员可以有 0 个或多个成员资格期:

CREATE TABLE member (
    member_id INT NOT NULL AUTO_INCREMENT
    PRIMARY KEY (member_id)
)

CREATE TABLE member_membership_period (
    member_membership_period_id INT NOT NULL AUTO_INCREMENT ,
    member_id INT NOT NULL ,
    test_column VARCHAR(45) NOT NULL ,
    PRIMARY KEY (member_membership_period_id) ,
    INDEX member_membership_period_member_idx (member_id ASC) ,
    CONSTRAINT member_membership_period_member
        FOREIGN KEY (member_id)
        REFERENCES member (member_id)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION)

Member 类映射到成员表:

@Entity
@Table(name="member")
public class Member implements Serializable  {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "member_id")
    private int id;

    @OneToMany
    @JoinColumn(name = "member_id")
    @ForeignKey(name = "member_membership_period_member")
    private Set<MemberMembershipPeriod> memberMembershipPeriods = new HashSet<MemberMembershipPeriod>();

    public int getId() {
        return id;
    }

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

    public Set<MemberMembershipPeriod> getMemberMembershipPeriods() {
        return memberMembershipPeriods;
    }

    public void setMemberMembershipPeriods(Set<MemberMembershipPeriod> memberMembershipPeriods) {
        this.memberMembershipPeriods = memberMembershipPeriods;
    }
}

并且 MemberMembershipPeriod 类映射到 member_membership_period 表

@Entity
@Table(name="member_membership_period")
public class MemberMembershipPeriod implements Serializable {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name = "member_membership_period_id")
    private int id;

    @Column(name = "test_column")
    String testColumn;

    public int getId() {
        return id;
    }

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

    public String getTestColumn() {
        return testColumn;
    }

    public void setTestColumn(String testColumn) {
        this.testColumn = testColumn;
    }
}

我的 DAO

public class MemberDaoImpl extends AbstractDAO<Member> implements MemberDao {

    public MemberDaoImpl(SessionFactory factory) {
       super(factory);
    }

    @Override
    public List<Member> getAllMembers() {
        Query query = currentSession().createQuery("from Member");
        return list(query);
    }

    @Override
    public Member getMemberById(int id) {
        return get(id);
    }
}

get(id) 的实现(drop wizards hibernate 包的一部分)

protected E get(Serializable id) {
    return (E) currentSession().get(entityClass, checkNotNull(id));
}

提供的任何帮助将不胜感激,我开始失去生活的意愿!

正在使用的其他技术是 DropWizard(执行休眠配置)和 MySQL

4

1 回答 1

0

尝试这个。它可能会帮助你。

@Override
    public List<Member> getAllMembers() {
        Criteria criteria = currentSession().createCriteria(Member.class,"member");
        criteria.createAlias("member.memberMembershipPeriods","period");
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        return criteria.list();
    }
于 2014-05-17T09:19:06.437 回答