Question

I'am trying to persist two entities that have OneToMany relationship the header of my two entities are:

@ManyToOne(cascade = CascadeType.PERSIST)
private Customer customer;

/******
@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "customer")
private List<Data> datas;

in both entities manager i've created 2 method that allow to create nd persist the two entities.

 public Data createData(String name, int number) {
        Data d = new Data();
        data.setNamte(name);
        data.setNumber(number);
        return mail;
    }

 public Customer createCustomer(String name, String famillyname, int age, List<Data> datas) {
        Customer cust=new Customer();  
       cust.setName(name);
       cust.setFamillyName(famillyname);
       cust.setAge(age);
       cust.setData(data);
       em.persist(cust)
        return data;
    }

When i called both methods the entities were successfully created and persisted except the customer id in the data datatable which get Where do You think the problem consists in?

Was it helpful?

Solution

The owner side of the association is Data.customer. JPA doesn't care about the inverse side (Customer.datas), but this is the only side you initialize.

Call data.setCustomer(cust) on every Data that you want to associate with the customer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top