JPA2.0中的@OneToOne单向映射 - 持久存在SQL错误

时间:2012-02-09 07:28:55

标签: java hibernate jpa-2.0

我无法弄清楚如何声明OneToOne单向关系。 TripDeparture模型具有Trip模型的外键(这是DB的设置方式)。尝试添加tripLocation设置为null的行程时,我收到以下错误。当我拿出'departureLocation'声明时,一切正常。

11891 [btpool0-2] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1054, SQLState: 42S22
11891 [btpool0-2] ERROR org.hibernate.util.JDBCExceptionReporter - Unknown column 'tripId' in 'field list'

我的想法已经用完了......任何人都可以帮忙吗?

谢谢!

父母:

@Entity
@Table(name = "TRIP")
public class Trip implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
    @JoinColumn(name = "tripId")
    private TripDeparture departureLocation;

孩子:

@Entity
@Table(name = "DEPARTURE_LOCATION")
public class TripDeparture implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private int tripId;

1 个答案:

答案 0 :(得分:3)

以下是我如何解决它。我没有使用@JoinColumn,而是使用了@PrimaryKeyJoinColumn。我还删除了DEPARTURE_LOCATION中的'id'变量(和列),但这不应该是重要的。

父:

@Entity
@Table(name = "TRIP")
public class Trip implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
    @PrimaryKeyJoinColumn
    private TripDeparture departureLocation;

孩子:

@Entity
@Table(name = "DEPARTURE_LOCATION")
public class TripDeparture implements Serializable {

    @Id
    private int tripId;