jpa / hibernate how to map element collection by foreign key with annotations

Maxfield Chandler

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)

Ish

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use a Primary Key also as a Foreign Key reference with JPA and Hibernate?

From Dev

Is it possible to map foreign keys with hibernate annotations?

From Dev

How to define the order of fields on foreign key mapping using Hibernate and JPA?

From Dev

How to match a hibernate/JPA table without a foreign key

From Dev

How to map one to one relation with no foreign key in JPA?

From Dev

Hibernate: map a foreign key that points to unique key

From Dev

JPA, Hibernate: OneToOne mapping with foreign key only

From Dev

Cascades settings in JPA and foreign key violation in hibernate

From Dev

JPA hibernate foreign key not set in entity

From Dev

How to use Map with hibernate annotations

From Dev

JPA, Hibernate can I do composite primary key which one element is foreign kay @OneToMany?

From Dev

How to save a foreign key in hibernate?

From Dev

How to save a foreign key in hibernate?

From Dev

Is it possible to limit the size of a @OneToMany collection with Hibernate or JPA Annotations?

From Dev

Hibernate/JPA mapping a map <entity, basic> using annotations

From Dev

Hibernate/JPA mapping a map <entity, basic> using annotations

From Dev

Understanding annotations and JPA(hibernate)

From Dev

Java hibernate how to map Inheritance using annotations

From Dev

Why does JPA (Hibernate) foreign key definition on @JoinTable not work?

From Dev

Hibernate JPA : ignore wrong sql foreign key values

From Dev

Increment field based on foreign key (JPA, Hibernate, Oracle DB)

From Dev

JPA Hibernate startup trying to drop the foreign key twice

From Dev

Increment field based on foreign key (JPA, Hibernate, Oracle DB)

From Dev

JPA foreign key zero 0 value hibernate TransientPropertyValueException

From Dev

Hibernate/JPA auto creates foreign key instead of using exisiting foreign key in many to one association

From Dev

How to return a Map where the key is an entity property and the value is the entity with JPA and Hibernate

From Dev

Hibernate OGM map a Map<String, Object> element collection

From Dev

How to add annotations above ArrayList<String> | Hibernate (Jpa)

From Dev

How to join on two columns using hibernate JPA annotations

Related Related

  1. 1

    How to use a Primary Key also as a Foreign Key reference with JPA and Hibernate?

  2. 2

    Is it possible to map foreign keys with hibernate annotations?

  3. 3

    How to define the order of fields on foreign key mapping using Hibernate and JPA?

  4. 4

    How to match a hibernate/JPA table without a foreign key

  5. 5

    How to map one to one relation with no foreign key in JPA?

  6. 6

    Hibernate: map a foreign key that points to unique key

  7. 7

    JPA, Hibernate: OneToOne mapping with foreign key only

  8. 8

    Cascades settings in JPA and foreign key violation in hibernate

  9. 9

    JPA hibernate foreign key not set in entity

  10. 10

    How to use Map with hibernate annotations

  11. 11

    JPA, Hibernate can I do composite primary key which one element is foreign kay @OneToMany?

  12. 12

    How to save a foreign key in hibernate?

  13. 13

    How to save a foreign key in hibernate?

  14. 14

    Is it possible to limit the size of a @OneToMany collection with Hibernate or JPA Annotations?

  15. 15

    Hibernate/JPA mapping a map <entity, basic> using annotations

  16. 16

    Hibernate/JPA mapping a map <entity, basic> using annotations

  17. 17

    Understanding annotations and JPA(hibernate)

  18. 18

    Java hibernate how to map Inheritance using annotations

  19. 19

    Why does JPA (Hibernate) foreign key definition on @JoinTable not work?

  20. 20

    Hibernate JPA : ignore wrong sql foreign key values

  21. 21

    Increment field based on foreign key (JPA, Hibernate, Oracle DB)

  22. 22

    JPA Hibernate startup trying to drop the foreign key twice

  23. 23

    Increment field based on foreign key (JPA, Hibernate, Oracle DB)

  24. 24

    JPA foreign key zero 0 value hibernate TransientPropertyValueException

  25. 25

    Hibernate/JPA auto creates foreign key instead of using exisiting foreign key in many to one association

  26. 26

    How to return a Map where the key is an entity property and the value is the entity with JPA and Hibernate

  27. 27

    Hibernate OGM map a Map<String, Object> element collection

  28. 28

    How to add annotations above ArrayList<String> | Hibernate (Jpa)

  29. 29

    How to join on two columns using hibernate JPA annotations

HotTag

Archive