休眠@OneToOne child 不持久

Hibernate @OneToOne child not persisting

我有一个库存实体,它有 2 个 child 实体,库存只能是车辆或 属性,不能两者都是,库存定义如下:

@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Stock {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "version")
    private Integer version;

    ...

    @OneToOne(mappedBy = "stock", cascade = CascadeType.ALL)
    private StockProperty property;
    private Boolean isProperty;

    @OneToOne(mappedBy = "stock", cascade = CascadeType.ALL)
    private StockVehicle vehicle;
    private Boolean isVehicle;

    ....

}

然后 child 实体 StockProperty:

@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class StockProperty {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "version")
    private Integer version;

    ....

    @PrimaryKeyJoinColumn
    @OneToOne(cascade = CascadeType.ALL)
    private Stock stock;

    ... more fields containing property details

}

StockVehicle 遵循完全相同的模式:

@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class StockVehicle {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "version")
    private Integer version;

    ....

    @PrimaryKeyJoinColumn
    @OneToOne(cascade = CascadeType.ALL)
    private Stock stock;

    ... more fields containing vehicle details

}

当试图link两者时,它在数据库中总是空的:

Stock stock = new Stock();
stock.setProperty(new StockProperty());
stock.getProperty().persist();
stock.persist();

或者反过来,两边仍然为空:

Stock stock = new Stock();

... fill in stock details

StockProperty property = new StockProperty();
property.setStock(stock);
... fill in property details

stock.setProperty(property);
stock.persist();

property.persist();

我在日志中没有看到任何错误,但是 linking 从未发生过:

股票的 属性 字段全部为空:

StockProperty 的股票字段全部为空:

我正在尝试实现级联删除,所以当我删除 Stock 时,它必须删除 StockProperty,如果可能的话,反之亦然,否则我会把它变成 one-directional参考。

根据要求更新 SQL:

Hibernate: insert into stock (account, company, created, full_description, ..., is_property, is_vehicle, modified, version) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Hibernate: select last_insert_id()

Hibernate: insert into stock_property (created, modified, ... , version) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Hibernate: select last_insert_id()

我没有在股票插入 sql 中看到 属性 字段,我也没有在 stock_property 的插入 sql 中看到股票字段。

在 StockProperty 上试试这个:

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "idStock", nullable = false)
private Stock stock;

还有这个库存:

@OneToOne(mappedBy="stock")
private StockProperty stockProperty;