java - Hibernate, delete from collection -
i have database contain 2 tables connect one-to-many relationship. enteract database through hibernate.
this hbm.xml files:
question.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="app"> <class name="question" table="question"> <id name="id" column="id" type="java.lang.long"> <generator class="native"/> </id> <property name="text" column="text" type="java.lang.string" not-null="true"/> <set name="answers" cascade="all"> <key column="question_id"/> <one-to-many class="answer"/> </set> </class> </hibernate-mapping>
answer.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="app"> <class name="answer" table="answer"> <id name="id" column="id" type="java.lang.long"> <generator class="native"/> </id> <property name="text" column="text" type="java.lang.string" not-null="true"/> <many-to-one name="question" column="question_id" class="question" not-null="true"/> </class> </hibernate-mapping>
and java files:
question.java
package app; import java.util.set; public class question { long id = null; string text = ""; set<answer> answers = null; // getters , setters }
answer.java
package app; public class answer { long id = null; string text = ""; boolean istrue = false; question question = null; // getters , setters }
so question can have answers , question instance has collection fo answers instances.
this main method:
package application; import app.answer; import app.question; import java.util.hashset; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.cfg.configuration; public class application { public static void main(string[] args) { sessionfactory sessionfactory = new configuration().configure().buildsessionfactory(); session session = sessionfactory.opensession(); question question = new question(); question.settext("question1"); answer answer1 = new answer(); answer1.settext("answer1"); answer1.setquestion(question); answer answer2 = new answer(); answer2.settext("answer2"); answer2.setquestion(question); hashset<answer> answers = new hashset<>(); answers.add(answer1); answers.add(answer2); question.setanswers(answers); session.begintransaction(); try { session.saveorupdate(question); session.gettransaction().commit(); } catch (exception ex) { ex.printstacktrace(); session.gettransaction().rollback(); } answers.remove(answer2); session.begintransaction(); try { session.saveorupdate(question); session.gettransaction().commit(); } catch (exception ex) { ex.printstacktrace(); session.gettransaction().rollback(); } } }
so create question instance, add 2 answers instances collection in question instance , save question instance in database. work correctly - question , answers writen database. remome 1 of answers instance collection in question instance , try save question instance in database again. catch exception. it's in native language, sqlserverexception , this: "can't insert null in column "question_id" in table "test.dbo.answer", in column null forbidden. error in update"
what's wrong? how can delete 1 of answer question?
in bidirectional relationship 1 side has inverse one. in one-to-many
association one-
side:
<set name="answers" cascade="all" inverse="true"> <key column="question_id"/> <one-to-many class="answer"/> </set>
this way many-
side take care of relationship.
in case, both sides it, , since foreign key in answer
class/table, hibernate updates null
when flushing association changes in question
class.
Comments
Post a Comment