0

我有这个数据库设计:

CREATE TABLE report (
    ID          MEDIUMINT PRIMARY KEY NOT NULL AUTO_INCREMENT,
    user        MEDIUMINT NOT NULL,
    created     TIMESTAMP NOT NULL,
    state       INT NOT NULL,
    FOREIGN KEY (user) REFERENCES user(ID) ON UPDATE CASCADE ON DELETE CASCADE
);


CREATE TABLE reportProperties (
    ID          MEDIUMINT NOT NULL, 
    k           VARCHAR(128) NOT NULL,
    v           TEXT NOT NULL,
    PRIMARY KEY(
        ID, k
    ),
    FOREIGN KEY (ID) REFERENCES report(ID) ON UPDATE CASCADE ON DELETE CASCADE
);

和这个休眠标记:

@Table(name="report")
@Entity(name="ReportEntity")
public class ReportEntity extends Report{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="ID")
    private Integer ID;
    @Column(name="user")
    private Integer user;
    @Column(name="created")
    private Timestamp created;
    @Column(name="state")
    private Integer state = ReportState.RUNNING.getLevel();

    @OneToMany(mappedBy="pk.ID", fetch=FetchType.EAGER)
    @JoinColumns(
            @JoinColumn(name="ID", referencedColumnName="ID")
    )
    @MapKey(name="pk.key")
    private Map<String, ReportPropertyEntity> reportProperties = new HashMap<String, ReportPropertyEntity>();
}

@Table(name="reportProperties")
@Entity(name="ReportPropertyEntity")
public class ReportPropertyEntity extends ReportProperty{

    @Embeddable
    public static class ReportPropertyEntityPk implements Serializable{
        /**
         * long#serialVersionUID
         */
        private static final long serialVersionUID = 2545373078182672152L;
        @Column(name="ID")
        protected int ID;
        @Column(name="k")
        protected String key;
    }

    @EmbeddedId
    protected ReportPropertyEntityPk pk = new ReportPropertyEntityPk();

    @Column(name="v")
    protected String value;
}

我已经为该报告插入了报告和 4 个属性。现在当我执行这个:

this.findByCriteria(
                        Order.asc("created"), 
                        Restrictions.eq("user", user.getObject(UserField.ID))
                    )
                );

我返回报告 4 次,而不是只有一次,其中包含 4 个属性的 Map。老实说,我不擅长 Hibernate,更喜欢直接 SQL,但我必须学习,但我看不出它是什么那是错的.....?

有什么建议么?

4

2 回答 2

0

我不知道你的问题的答案,但你可以用另一种方式做到这一点并取消整个ReportPropertyEntity班级:

@CollectionOfElements
@JoinTable(name = "reportProperties", joinColumns = { @JoinColumn(name = "id") })
@MapKey(columns = { @Column(name = "`key`") })
@Column(name = "`value`", length = 131070, nullable = false)
private Map<String, String> reportProperties = new HashMap<String, String>();
于 2010-05-24T17:19:07.537 回答
0

尝试在您的标准/查询上使用它:

setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);

这将适用于您的所有表格。无需修改映射。

于 2010-05-28T15:43:19.553 回答