0

I would like to store a map with Integer,Entity but JPA is storing the entity inline so that means i get an error like that:

A truncation error was encountered trying to shrink VARCHAR '곭獲4捯洮瑨潭慳灥牯畴歡⹡汰瑩浩湧⹭潤敬⹈慲摷慲敆潯瑰物湴���Ȁౌ彰敲獩獴敮捥彦整捨䝲潵灴,䱯牧⽥捬楰獥⽰敲獩獴&' to length 255..

How can i force JPA to only store the id of the entity?

EDIT:

@ElementCollection
private Map<Integer, Footprint> footprints = new LinkedHashMap<>();
perotom
  • 851
  • 13
  • 33

2 Answers2

0

I wouldn't say that this isn't possible with an @ElementCollection but what you probably want is an unidirectional @OneToMany relationship.

If the Integer in your Map is really the ID of corresponding the Footprint entity, the Map construct would also be redundant.

Try something like this:

@Entity
public class Something {

  @Id
  @Column(name="SOME_ID")
  private long id;   

  @OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
  @JoinColumn(name="SOMETHING_ID", referencedColumnName="SOME_ID")
  private List<Footprint> footprints = new LinkedList<>();

}

See also:

Community
  • 1
  • 1
unwichtich
  • 13,712
  • 4
  • 53
  • 66
  • thats good but i want to add an extra integer which isn´t the id of the entity. The table should have two properties: id of the footprint and an extra integer which is different. – perotom Oct 20 '14 at 07:40
0

Let's say your Footprint entity is defined as follows:

<!--language: java-->

    @Entity
    public class Footprint {
        @Id
        private String id;                 //PK of Footprint

        @ManyToOne                         //owning side of the relationship
        @JoinColumn(name = "id_something") //FK of Something
        private Something something;
    }

In general for mappings of type Map<Basic, Entity> the key can be modeled in two ways:

  1. @MapKeyColumn annotation creates an additional column in Footprint entity (the owning side of the relationship) to store the map key

    @Entity
    public class Something {
        @Id
        private Integer id;
    
        @OneToMany(mappedBy = "something")   //non-owning side of the relationship
        @MapKeyColumn(name = "id_footprint") //specifies the map's key
        private Map<Integer, Footprint> footprints = new LinkedHashMap<>();
    }
    

    The above relationship will produce the corresponding database tables:

    Footprint
    --------------------
    id            int PK
    id_something  int FK
    id_footprint  int     <-- referenced by the map's key
    
    Something
    --------------------
    id            int PK
    
  2. @MapKey annotation does NOT create additional column in Footprint entity (the owning side of the relationship) and the map key is stored as the part of the entity by referencing its primary key:

    @Entity
    public class Something {
        @Id
        private Integer id;
    
        @OneToMany(mappedBy = "something") //non-owning side of the relationship
        @MapKey(name = "id")               //refers to Footprint's primary key
        private Map<Integer, Footprint> footprints = new LinkedHashMap<>();
    }
    

    The above relationship will produce the corresponding database tables:

    Footprint
    --------------------
    id            int PK  <-- referenced by the map's key
    id_something  int FK
    
    Something
    --------------------
    id            int PK
    
wypieprz
  • 7,981
  • 4
  • 43
  • 46