简体   繁体   English

休眠@OneToOne子项不持久

[英]Hibernate @OneToOne child not persisting

I have a Stock entity which has 2 child entities, stock can only be vehicle or property, not both, stock is defined as follows: 我有一个股票实体,其中有2个子实体,股票只能是车辆或财产,不能同时是两者,股票的定义如下:

@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;

    ....

}

And then the child entity StockProperty: 然后是子实体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 follows exactly the same pattern: 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

}

When trying to link the two, it's always null in the database: 尝试链接两者时,在数据库中始终为null:

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

Or the other way around, it's still null on both sides: 或反之,两边仍然为空:

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();

I'm not seeing any errors in the logs, but the linking just never happens: 我没有在日志中看到任何错误,但是链接从未发生:

Stock's propery fields are all null: 股票的属性字段均为空:

在此处输入图片说明

StockProperty's stock fields are all null: StockProperty的库存字段全部为空:

在此处输入图片说明

I'm trying to implement a cascade delete, so when I'm deleting Stock, it must delete StockProperty and if possible, the other way around as well, otherwise I would have made it a one-directional reference. 我正在尝试实现级联删除,因此,当我删除Stock时,它必须删除StockProperty,如果可能的话,也必须删除它,否则,我将其设为单向引用。

Update SQL as requested: 根据要求更新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()

I don't see the property field in the stock insert sql, neither do I see the stock field in the insert sql for stock_property. 我看不到stock插入sql中的property字段,也看不到stock_property的插入sql中的stock字段。

Try this on StockProperty: 在StockProperty上尝试:

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

And this on Stock: 这在股票上:

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

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

相关问题 使用@MapsId持久保存@OneToOne子实体会在Hibernate中抛出“错误:已分离的实体传递给持久化” - Persisting a @OneToOne child entity with @MapsId throws “error:detached entity passed to persist” in Hibernate 休眠:具有OnetoOne关系的重复项将保留一个列表 - Hibernate: Duplicated items with OnetoOne relation persisting a List Hibernate以错误的顺序保留了我的OneToOne实体 - Hibernate is persisting my OneToOne entities in the wrong order Hibernate OneToOne关系:无法删除子表 - Hibernate OneToOne Relationship: Unable to delete the child table 在休眠子类中使用OneToMany映射覆盖OneToOne - Overriding OneToOne with OneToMany Mapping in Hibernate Child Class Hibernate映射:子对象属性上的OneToMany和OneToOne - Hibernate mapping: OneToMany and OneToOne on child object property Hibernate - 包含父实体的持久子实体 - Hibernate - Persisting child entity containing parent entity Hibernate @OneToOne 无法添加或更新子行:外键约束 - Hibernate @OneToOne cannot add or update a child row: a foreign key constraint JPA/Hibernate5:如何替换用@OneToOne 和@MapsId 映射的子 object? - JPA/Hibernate5: How to replace a child object mapped with @OneToOne and @MapsId? 与SpringData保持一致时,Hibernate未设置子外键 - Hibernate is not setting child foreign key when persisting with SpringData
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM