我有这个数据库设计:
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,但我必须学习,但我看不出它是什么那是错的.....?
有什么建议么?