Hibernate @OneToOne child not persisting

1.9k views Asked by At

I have a Stock entity which has 2 child entities, stock can only be vehicle or property, not both, stock is defined as follows:

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

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

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

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:

enter image description here

StockProperty's stock fields are all null:

enter image description here

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.

Update SQL as requested:

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.

1

There are 1 answers

2
Jean Cedron On BEST ANSWER

Try this on StockProperty:

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

And this on Stock:

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