0

Im trying to save List of class instances inside a parent object, with hibernate. Child instance should map to parent by parents ID "CONTRACTID", which is generated.

PARENT:

    @Id
    @Column(name = "CONTRACTID")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence")
    @SequenceGenerator(name = "id_Sequence", sequenceName = "ID_SEQ_CONTRACT")
    private Long contract_id;

and then

    @OneToMany(targetEntity = CustomerGroup.class, fetch = FetchType.LAZY, cascade = {CascadeType.ALL}, mappedBy = "contractId")
    private List<CustomerGroup> customerGroups;

CHILD:

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CONTRACTID", foreignKey=@ForeignKey(name="CONTRACTID_FK"))
    private ContractHolder contractId;

also tried without foreignKey=@ForeignKey(name="CONTRACTID_FK")

I see in database that both fields are existing, parents ID is generated as expected on .save but the FK field of CustomerGroup stays as null when parent is saved.

What am i doing wrong here?

EDIT:

Save code is very simple line in Database.class after setting all the needed values for parent object, also the List of CustomerGroup is set to parent object and saved properly but not the FK autogenerated id

contractRepo.save(contract);

EDIT2:

and if i try to set parent object to @JoinColumn of childeren before saving. like

customerGroup.setContractId(contractHolder);

i get what seems to be an infinite loop. goddamn

Clomez
  • 1,410
  • 3
  • 21
  • 41
  • 1
    show the code how you are saving parent object ? – Derrick Aug 30 '18 at 12:01
  • @Dev911 added some code – Clomez Aug 30 '18 at 12:11
  • 1
    I think you missed up the mapping because parent class mapped @OneToMany with CustomerGroup and another side (child class) is ContractHolder – Angad Bansode Aug 30 '18 at 12:17
  • 1
    @AngadBansode im trying to save List of CustomerGroup's inside one ContractHolder class instance. i dont really understand what you are saying. They are suppose to be different classes? – Clomez Aug 30 '18 at 13:21
  • 1
    You have to set the `ContractHolder` in the `CustomerGroup`. – K.Nicholas Aug 31 '18 at 16:39
  • yeah, can you tell me how not to get infinite loop while retrieving it tho? Also if you put that as an answer i can accept it. @K.Nicholas – Clomez Sep 03 '18 at 06:37
  • 1
    Naw, you should do your own debugging. Start with posting code that runs. Look at https://stackoverflow.com/questions/11938253/whats-the-difference-between-joincolumn-and-mappedby-when-using-a-jpa-onetoma. Turn on the SQL output and see what is going on: logging.level.org.hibernate.SQL=debug – K.Nicholas Sep 03 '18 at 15:40
  • Yeah no prob. i already solved the problem, just thought if you have some information to clear it further. Either way, if you post your comment from above as answer i can accept that (You have to set the ContractHolder in the CustomerGroup.) @K.Nicholas – Clomez Sep 05 '18 at 12:17

0 Answers0