One-to-One unidirectional mapping JPA - EclipseLink

Viewed 98

I have the following entity classes

public class Container {


    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private final Long id;

    @Column(name = "container_id")
    private final String containerId;

    @OneToMany(cascade = { CascadeType.ALL }, orphanRemoval = true, fetch = FetchType.EAGER)
    @JoinColumn(name = ContainerItem.container_id, referencedColumnName = "container_id", nullable = true)
    private Collection<ContainerItem> containerItems = Collections.emptyList();

}


public class ContainerItem {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private final Long id;

    @Column(name = "container_id")
    private String containerId;

    @OneToOne(targetEntity = Order.class)
    @NonNull
    private final Order order;
}


public class Order {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private final Long id;

    @Column(name = "order_id")
    private String orderId;

    @OneToMany(cascade = { CascadeType.ALL }, orphanRemoval = true, fetch = FetchType.EAGER)
    @JoinColumn(name = Product."order_id", referencedColumnName = "order_id")
    private Collection<Product> products;

}


public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private final Long id;


    @Column(name = "order_id")
    private String orderId;

    @Column(name = "text")
    private final String text;
}

I have used random names for objects to keep it simple. (Edited to resemble a fictional real-world scenario to give some meaning to it.

Basically, there are two main operations -

  1. I execute CRUD operations for Order and Product and there are no issues here. When an Order is inserted/updated/deleted, the Product is also inserted/updated/deleted.

  2. I execute CRUD operations for Container and ContainerItem. For this to work successfully, there needs to be some Order that can be used by the ContainerItem in its foreign key (OneToOne mapping) that I specified (the column I use for the foreign key is the orderId in Order). I create them too, but I DO NOT want to create/update/delete the Order when I insert an Container which inserts a ContainerItem containing a mapping to the Order. How can I fix this?

At the same time, Order is not the rightful owner of ContainerItem and I do not want to have a mapping from the other table.

The save is performed as below with the entityManager.

public Long create(Container container) {
    this.entityManager.persist(container);
    this.entityManager.flush();
    return container.getId();
}

With the above entity mapping, I see an error as below, which I sort of understand, but do not know how to resolve.

During synchronization a new object was found through a relationship that was not marked cascade PERSIST: Order (id:null, orderId: "1", products: [])

The object received here is correct, I do not want to store/update/delete the state of Order or Product. How can I specify this unidirectional relation here?

0 Answers
Related