Given these two tables:
CREATE TABLE `soc` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(32),
PRIMARY KEY (`id`));
CREATE TABLE `soc_attitude` (
`soc_id` INT NOT NULL,
`target_soc_id` INT NOT NULL,
`attitude` INT,
PRIMARY KEY (`soc_id`,`target_soc_id`));
In the Soc class, I want to get all rows matching this.soc_id from the soc_attitude table using a field like this:
private Map<Integer,Integer> attitudes;
Where the key of the map is target_soc_id and the value is attitude.
I got as far as this:
@Entity
@Table(name = "soc")
public class Soc {
@Id
@Column( name="id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name="name")
private String name;
@ElementCollection
@CollectionTable(name="soc_attitude",joinColumns=@JoinColumn(name="soc_id"))
@Column(name="attitude")
private Map<Integer,Integer> attitudes;
But I think this will make soc_id the key and attitude the value.
What annotations do I use? (using Hibernate 4.3.11.Final)
Use @MapKeyColumn
Try this:
@ElementCollection
@CollectionTable(name="soc_attitude",joinColumns=@JoinColumn(name="soc_id"))
@Column(name="attitude")
@MapKeyColumn(name="target_soc_id")
private Map<Integer,Integer> attitudes;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments