简体   繁体   English

具有继承的密钥和一对多关系的JPA实体:无法获取适当的外键

[英]JPA Entity with inherited key and a one-to-many relation: cannot get a proper foreign key

I have 3 entities: 我有3个实体:

  1. a Poi entity Poi实体
  2. a PolygonPoi entity that inherits from the Poi entity (also the @Id) via a joined inheritance type. 一个通过联接继承类型从Poi实体(也是@Id)继承的PolygonPoi实体。
  3. and the Coordinate entity that is connected to the Polygon poi via a many to on relation (1-PolygonPoi N-Coordinates) 以及通过多对多关系(1-PolygonPoi N-Coordinates)与Polygon poi连接的Coordinate实体

My problem is that I can't get proper foreign keys ( I get only only null values) for the Coordinate entity. 我的问题是我无法为Coordinate实体获得适当的外键(我只能得到空值)。 Coordinate should use the Id of Poi. 协调员应使​​用Poi的ID。 How do I fix this ?? 我该如何解决 ??

Here are the three mentioned entity classes: 以下是三个提到的实体类:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Poi
{
    @Id
    @Column(name = "NAME")
    protected String name;

@Entity
public class PolygonPoi extends Poi
{
    @OneToMany(mappedBy = "poi", cascade={CascadeType.ALL})
    protected List<Coordinate> coordinate = new ArrayList<Coordinate>();

@Entity
public class Coordinate
{
    @Id
    @GeneratedValue
    private long id;

    @ManyToOne  
    Poi poi;

Also It would be nice to get ride of PolygonPoi in the Database entirely, since it does not hold any information beside the relation to the Coordinate. 另外,最好完全在数据库中使用PolygonPoi,因为它除了与坐标的关系以外不保存任何信息。

E: E:

Here is how my Tables look like: 这是我的表格的样子:

SELECT * FROM POI;
NAME    CREATEDBY  
pp0     1

SELECT * FROM POLYGONPOI;
NAME  
pp0

SELECT * FROM COORDINATE ;
ID      LATITUDE            LONGITUDE           POI_NAME  
1       |30.030000686645508 |50.29999923706055  |null
2       |31.030000686645508 |51.29999923706055  |null
3       |31.030000686645508 |50.29999923706055  |null

Uunless you specify a @JoinColumn annotation and define the foreign key column, JPA will default to using a "poi_NAME" field within the Coordinate table as a foriegn key to Poi.NAME for the Coordinate ->Poi relationship. 除非您指定@JoinColumn批注并定义外键列,否则JPA将默认使用Coordinate表中的“ poi_NAME”字段作为Poi.NAME的外键,用于Coordinate-> Poi关系。 This will be set with the name value contained within the referenced poi instance. 将使用引用的poi实例中包含的名称值进行设置。 If the referenced poi doesn't have a name set - your application is responsible for assigning it as a Poi entity (or subclass) cannot exist without an ID. 如果所引用的poi没有设置名称-您的应用程序负责将其分配,因为没有ID便无法存在Poi实体(或子类)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM