在实体之间创建关系一对一期间获得部署错误

时间:2014-06-13 13:00:16

标签: java-ee jpa entity

我试图创建两个实体但是我收到了以下错误

Exception Description: Entity class [class it.volaconoi.entity.Airport] must use a @JoinColumn instead of @Column to map its relationship attribute [city].

这些是实体。我想要的是将city_source连接到机场实体中的city,将city_dest连接到@Entity @Table(name = "ROUTE") public class Route implements Serializable { @Id @Column(name = "ID_ROUTE") private String id_route; @Column(name = "AIRLANE", nullable = false) private String airlane; @Column(name = "AIRCRAFT_ID", nullable = false) private String aircraft_id; @Column(name = "CITY_SOURCE", nullable = false) @OneToOne(mappedBy = "city", optional = false) private String airport_city_source; @Column(name = "CITY_DEST", nullable = false) @OneToOne(mappedBy = "city", optional = false) private String airport_city_dest; @Column(name = "DEPARTURE_DATE", nullable = false) @Temporal(TemporalType.DATE) private Date departure_date; @Column(name = "ARRIVAL_DATE", nullable = false) @Temporal(TemporalType.DATE) private Date arrival_date; @Column(name = "TRAVEL_CLASS", nullable = false) private String travel_class; @Column(name = "SEATS", nullable = false) private int seats; @Column(name = "PRICE", nullable = false) private float price; ... 。不幸的是我收到了这个错误毫无疑问,错误是由于我以错误的方式创建了这种关系。

@Entity
@Table(name = "AIRPORT")
public class Airport implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "AIRPORT_ID")
    private Integer id;
    @Column(name = "NAME", nullable = false)
    private String name;
    @Column(name = "CITY")
    @OneToOne
    private String city;
    @Column(name = "COUNTRY")
    private String country;
    @Column(name = "IATA_FAA")
    private String iata_faa_code;
    @Column(name = "ICAO")
    private String icao_code;
    @Column(name = "LATITUDE")
    private String latitude;
    @Column(name = "LONGITUDE")
    private String longitude;
    @Column(name = "ALTITUDE")
    private String altitude;
    @Column(name = "TIMEZONE")
    private String timezone;
    @Column(name = "DST")
    private String dst;
...

机场

{{1}}

2 个答案:

答案 0 :(得分:1)

错误说明,您正在使用关于JPA提供商认为应该是关系映射的列注释 - 由于某种原因,它认为Airport City字符串是关系而不是基本映射。关系映射需要连接列,以指定外键字段以及它们加入的目标表中的字段,而列用于基本映射。

机场实体似乎正确映射了类型 - 它显示了对基本映射的@Column注释,问题必须在其他地方。

另一方面,Route的airport_city_source和airport_city_dest不正确,因为它们声明它们应该映射到列,但实际上是与另一个实体的OneToOne关系,其中该实体的城市映射定义了此所需的外键连接列映射。由于错误是抱怨城市地图不正确,这可能是源头。删除@OneToOne将使它们成为与这些属性的String类型匹配的基本映射。

如果您希望它们映射到实体,您需要将类型更改为实体类型(可能是City?)并删除mappedby或添加连接列。

您无法像尝试一样将字符串链接在一起。相反,也许路线应该知道机场的起点和终点,如果需要,你可以从中拉出城市字符串:

@OneToOne(optional = false)
private Airport airport_source;

答案 1 :(得分:0)

您的错误消息非常清楚地告诉您错误:在建模关系时,您必须指定连接列。有关简短示例,请参阅Javadoc,或阅读整个主题的Java EE tutorial

并提出建议:自Java EE 5以来的想法是使用约定优于配置,因此您不必指定默认值(例如名称)。