Lost with hibernate - OneToMany resulting in the one being pulled back many times..
Posted
by Andy
on Stack Overflow
See other posts from Stack Overflow
or by Andy
Published on 2010-05-24T17:10:09Z
Indexed on
2010/05/24
17:21 UTC
Read the original article
Hit count: 293
I have this DB design:
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
);
and this Hibernate Markup:
@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>();
}
and
@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;
}
And i have inserted on Report and 4 Properties for that report. Now when i execute this:
this.findByCriteria(
Order.asc("created"),
Restrictions.eq("user", user.getObject(UserField.ID))
)
);
I get back the report 4 times, instead of just the once with a Map with the 4 properties in. I'm not great at Hibernate to be honest, prefer straight SQL but I must learn, but i can't see what it is that is wrong.....?
Any suggestions?
© Stack Overflow or respective owner