java - Persisting multiple associations to same entity in Hibernate -
i making chat system, , have below database schema (everything not related core problem has been removed).

a thread represents conversation between 2 participants. when new thread created (persisted), 2 participants should created; 1 sender , 1 receiver (a message added thread, not relevant in case). have mapped 2 database tables 2 entities.
@entity @table(name = "participant") public class participant { @id @generatedvalue(strategy = generationtype.identity) @column private int id; @manytoone(fetch = fetchtype.lazy, targetentity = thread.class, optional = false) @joincolumn(name = "thread_id") private thread thread; // getters , setters } @entity @table(name = "thread") public class thread { @id @generatedvalue(strategy = generationtype.identity) @column private int id; @onetomany(fetch = fetchtype.lazy, mappedby = "thread", targetentity = participant.class, cascade = cascadetype.all) private set<participant> participants = new hashset<>(); @manytoone(fetch = fetchtype.lazy, targetentity = participant.class, cascade = cascadetype.all, optional = false) @joincolumn(name = "sender_id") private participant sender; @manytoone(fetch = fetchtype.lazy, targetentity = participant.class, cascade = cascadetype.all, optional = false) @joincolumn(name = "receiver_id") private participant receiver; // getters , setters } from thread entity, participants association should contain participants in thread, while sender , receiver contain references sender , receiver, respectively, convenience. thus, 2 participants should persisted in database. here code wrote persist new thread:
thread thread = new thread(); participant sender = new participant(); sender.setthread(thread); participant receiver = new participant(); receiver.setthread(thread); thread.setsubject(subject); thread.setsender(sender); thread.setreceiver(receiver); set<participant> participants = new hashset<>(2); participants.add(sender); participants.add(receiver); thread.setparticipants(participants); thread saved = this.threadrepository.save(thread); this throws below exception.
org.hibernate.transientpropertyvalueexception: not-null property references transient value - transient instance must saved before current operation : com.example.thread.entity.participant.thread -> com.example.thread.entity.thread
i tried many variations of cascade attribute on both entities, same exception thrown in cases (although different transient properties). logically speaking, approach should not problematic, has happen thread entity persisted first in order participants obtain generated id, before should persisted themselves.
do have problem mapping, or problem? thank you!
a more detailed answer based on comment, requested answer.
trying solve problem different cascading types, joincolumns , other hibernate annotations. wrong way solve problem that. cascading should used in different way did , should remove mapping now. see semantic/meaning of model in terms of usecases wana accomplish it. cascading participant thread not want, because participant in cases created , maintained indepedent of thread. don't try simplify save process of single entity usage of cascade without actual business case behind it.
i advise split , create/save thread object independent of participants. might few lines of code more, easier understand in long run , better maintainable.
Comments
Post a Comment