2014-10-16 73 views
0

我想存储与整数,实体但JPA被存储实体直列所以这意味着我得到的是一个错误的地图:JPA 2.0地图<整数,实体>

时遇到截断误差试图缩小VARCHAR “곭获4捯洮瑨潭悭灥牯畴欢⹡汰莹浩涌⹭润敬⹈慲摷慲敆浔瑰物湴Ȁౌ彰敲獩獴敮捥彦整舍䝲潵灴,䱯牧⽥捬楰獥⽰敲獩獴&“到 长度255 ..

我怎么能强迫JPA只存储实体的id?

编辑:

@ElementCollection 
private Map<Integer, Footprint> footprints = new LinkedHashMap<>(); 
+0

请将您的地图代码添加到问题中。 – unwichtich 2014-10-16 17:53:29

+0

已经完成。 Integer应该表示脚印的位置 – perotom 2014-10-16 17:56:41

+0

按照定义,ElementCollection不包含实体。ElementCollection包含基本类型或嵌入类型。你想要的是一个OneToMany。 – 2014-10-16 19:17:45

回答

0

我不会说这是不可能与@ElementCollection但你可能想要的是一个单向@OneToMany关系。

如果在你的地图整数真是相应足迹实体的ID,地图的构建也将是多余的。

尝试这样:

@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<>(); 

} 

参见:

+0

多数民众赞成在良好的但我想添加一个额外的整数不是实体的ID。该表应具有两个属性:脚印的id和不同的额外整数。 – perotom 2014-10-20 07:40:37

0

比方说,你Footprint实体的定义如下:

<!--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; 
    } 

通常用于密钥可以以两种方式被建模Map<Basic, Entity>类型的映射:

  1. @MapKeyColumn注解创建在足迹实体(的关系持有端)的附加列来存储地图键

    @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<>(); 
    } 
    

    上述关系将产生CORRE既受数据库表:

    Footprint 
    -------------------- 
    id   int PK 
    id_something int FK 
    id_footprint int  <-- referenced by the map's key 
    
    Something 
    -------------------- 
    id   int PK 
    
  2. @MapKey批注不创建足迹实体(的关系持有端)附加列和地图键是通过参考其主键存储作为实体的一部分:

    @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<>(); 
    } 
    

    上述关系将产生相应的数据库表:

    Footprint 
    -------------------- 
    id   int PK <-- referenced by the map's key 
    id_something int FK 
    
    Something 
    -------------------- 
    id   int PK 
    
相关问题