Why is Hibernate searches the same column 4 times? Help mapping?

My class looks like these. Why is the same column selected 4 times? What’s wrong with the mapping?

@Entity @Table(name="CLIENTS")
public class Client implements Serializable {

@Id @GeneratedValue @Column(name="GENERATED_ID")
private Long id;

@Column(name="NAME")
private String name;

@OneToMany(cascade=CascadeType.ALL, mappedBy="client", fetch=FetchType.EAGER)
private Map params = new HashMap();
}< br />
@Entity @Table(name="PARAMS")
public class Param implements Serializable {

@EmbeddedId
private ParamPK paramPK;

@Column(name="VALUE")
private String value;

@ManyToOne @MapsId("clientId")
private Client client;
}

@Embeddable
public class ParamPK implements Serializable {

@Column(name="PARAM_KEY")
private String key;

@Column(name="CLIENT_GENERATED_ID")
private Long clientId;
}

The query generated by select gets the same column 4 times.

/* from Client */ 
select
client0_.GENERATED_ID as GENERATED1_1_,
client0_.NAME as NAME1_
from
CLIENTS client0_

/* load one-to-many Client.params */
select
params0_.client_GENERATED_ID as client3_1_1_,
params0_.client_GENERATED_ID as client3_1_,
params0_.PARAM_KEY as PARAM1_1_,
params0_ .CLIENT_GENERATED_ID as CLIENT3_1_,
params0_.client_GENERATED_ID as client3_0_0_,
params0_.PARAM_KEY as PARAM1_0_0_,
params0_.VALUE as VALUE0_0_
from params0_.
params0_where
params0_.client_GENERATED_ID=?

Note that using Hibernate 3.5.3. The remaining boilerplate code has been deleted as irrelevant.

You forgot to tell Hibernate what constitutes the key to the parameter map. Add the following comment to this map:

< pre>@OneToMany(cascade=CascadeType.ALL, mappedBy=”client”, fetch=FetchType.EAGER)
@MapKey(name = “paramPK”)
private Map params = new HashMap();

This tells Hibernate that the paramPK attribute of the Param entity is the key to the map.

My class looks like these. Why is the same column selected 4 times? What’s wrong with the mapping?

@Entity @Table(name="CLIENTS")
public class Client implements Serializable {

@Id @GeneratedValue @Column(name="GENERATED_ID")
private Long id;

@Column(name="NAME")
private String name;

@OneToMany(cascade=CascadeType.ALL, mappedBy="client", fetch=FetchType.EAGER)
private Map params = new HashMap();
}< br />
@Entity @Table(name="PARAMS")
public class Param implements Serializable {

@EmbeddedId
private ParamPK paramPK;

@Column(name="VALUE")
private String value;

@ManyToOne @MapsId("clientId")
private Client client;
}

@Embeddable
public class ParamPK implements Serializable {

@Column(name="PARAM_KEY")
private String key;

@Column(name="CLIENT_GENERATED_ID")
private Long clientId;
}

The query generated by select gets the same column 4 times.

/* from Client */ 
select
client0_.GENERATED_ID as GENERATED1_1_,
client0_.NAME as NAME1_
from
CLIENTS client0_

/* load one -to-many Client.params */
select
params0_.client_GENERATED_ID as client3_1_1_,
params0_.client_GENERATED_ID as client3_1_,
params0_.PARAM_KEY as PARAM1_1_,
params0_. CLIENT_GENERATED_ID as CLIENT3_1_,
params0_.client_GENERATED_ID as client3_0_0_,
params0_.PARAM_KEY as PARAM1_0_0_,
params0_.VALUE as VALUE0_0_
from
PARAMS />ms0_
from
PARAMS />ms0_ br /> params0_.client_GENERATED_ID=?

Note that using Hibernate 3.5.3. The remaining boilerplate code has been deleted as irrelevant.

You forgot to tell Hibernate what constitutes the key to the parameter map. Add the following comment to this map:

@OneToMany(cascade=CascadeType.ALL, mappedBy= "client", fetch=FetchType.EAGER)
@MapKey(name = "paramPK")
private Map params = new HashMap();

This tells Hibernate that the paramPK attribute of the Param entity is the key to the map.

Leave a Comment

Your email address will not be published.