简体   繁体   English

休眠-一对一关系

[英]Hibernate - one to one relation

I have two tables 我有两张桌子

User 用户

CREATE TABLE "user"
(
    id INTEGER PRIMARY KEY NOT NULL,
    phone TEXT,
    position_id BIGINT,
    CONSTRAINT user_position_id_fk FOREIGN KEY (position_id) REFERENCES position (id)
);
CREATE UNIQUE INDEX user_id_uindex ON "user" (id);

Position 位置

CREATE TABLE position
(
    id INTEGER PRIMARY KEY NOT NULL,
    latitude DOUBLE PRECISION,
    longitude DOUBLE PRECISION,
    bearing DOUBLE PRECISION,
    accuracy DOUBLE PRECISION,
    speed DOUBLE PRECISION,
    user_id BIGINT,
    CONSTRAINT position_user_id_fk FOREIGN KEY (user_id) REFERENCES "user" (id)
);
CREATE UNIQUE INDEX position_id_uindex ON position (id);

They are mapped by annotations like this: 它们通过如下注释进行映射:

@Entity
@Table(name = "user")
public class User {

    public static final String ID = "id";
    public static final String PHONE = "phone";

    public User() {
        position = new Position();
    }

    @Id
    @Column(name = ID, unique = true)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = PHONE)
    private String phone;

    @OneToOne(mappedBy = "user")
    @Cascade(CascadeType.ALL)
    @JoinColumn(name = "id", referencedColumnName = "position_id")
    private Position position;
}

@Entity
@Table(name = "position")
public class Position {

    public static final String ID = "id";
    public static final String LATITUDE = "latitude";
    public static final String LONGITUDE = "longitude";
    public static final String BEARING = "bearing";
    public static final String ACCURACY = "accuracy";
    public static final String SPEED = "speed";
    public static final String USER_ID = "user_id";

    @Column(name = ID, unique = true)
    @GenericGenerator(name = "generator", strategy = "foreign",
            parameters = {@org.hibernate.annotations.Parameter(name = "property", value = "user")})
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = LATITUDE)
    private double latitude;

    @Column(name = LONGITUDE)
    private double longitude;

    @Column(name = BEARING)
    private double bearing;

    @Column(name = ACCURACY)
    private double accuracy;

    @Column(name = SPEED)
    private double speed;

    @OneToOne
    @PrimaryKeyJoinColumn
    @Cascade(CascadeType.ALL)
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    private User user;
}

I'm saving new object into database. 我正在将新对象保存到数据库中。 Everything is correct except one thing. 除了一件事,一切都正确。 Fields user_id and position_id are null. 字段user_idposition_id为空。 Is there any solution which help me do this? 有什么解决方案可以帮助我做到这一点吗?

It will take a few steps to fix your problem. 将需要一些步骤来解决您的问题。

Let's start with that in One to One association there's only one foreign key, you can decide which entity will own the foreign key. 让我们从一对一关联中开始,只有一个外键,您可以决定哪个实体拥有该外键。

The @joinColumn annotation is supposed to be only in the owning entity, for example, if you decided that user will own the position key as a foreign key then the @joinColumn annotation will be in the user entity under the position field. @joinColumn批注应该仅在所属实体中,例如,如果您确定用户将拥有位置键作为外键,则@joinColumn批注将在用户实体中的position字段下。

on the other entity just use 在另一个实体上只使用

@OneToOne(mappedby = "the_matching_field_on_the_other entity")

for example: 例如:

CREATE TABLE "user"
(
    id INTEGER PRIMARY KEY NOT NULL,
    phone TEXT,
    position_id BIGINT,
    CONSTRAINT user_position_id_fk FOREIGN KEY (position_id) REFERENCES position (id)
);

CREATE TABLE position
(
    id INTEGER PRIMARY KEY NOT NULL,
    latitude DOUBLE PRECISION,
    longitude DOUBLE PRECISION,
    bearing DOUBLE PRECISION,
    accuracy DOUBLE PRECISION,
    speed DOUBLE PRECISION,
);

@Entity
@Table(name = "user")

public class User {

public static final String ID = "id";
public static final String PHONE = "phone";

public User() {
    position = new Position();
}

@Id
@Column(name = ID, unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column(name = PHONE)
private String phone;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "id", referencedColumnName = "position_id")
private Position position;

} }

@Entity
@Table(name = "position")
public class Position {

    public static final String ID = "id";
    public static final String LATITUDE = "latitude";
    public static final String LONGITUDE = "longitude";
    public static final String BEARING = "bearing";
    public static final String ACCURACY = "accuracy";
    public static final String SPEED = "speed";
    public static final String USER_ID = "user_id";

    @Column(name = ID, unique = true)
    @GenericGenerator(name = "generator", strategy = "foreign",
            parameters = {@org.hibernate.annotations.Parameter(name = "property", value = "user")})
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = LATITUDE)
    private double latitude;

    @Column(name = LONGITUDE)
    private double longitude;

    @Column(name = BEARING)
    private double bearing;

    @Column(name = ACCURACY)
    private double accuracy;

    @Column(name = SPEED)
    private double speed;

    @OneToOne(mappedBy = "position")
    private User user;
}

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

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