例如,一个问题有多个答案:
1)创建持久化类
package list; import java.util.list; public class question { private int id; private string qname; private list<string> answers; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getqname() { return qname; } public void setqname(string qname) { this.qname = qname; } public list<string> getanswers() { return answers; } public void setanswers(list<string> answers) { this.answers = answers; } }
2)创建映射文件
<?xml version='1.0' encoding='utf-8'?> <!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="list.question" table="quesion"> <cache usage="read-write"/> <id name="id"> <generator class="increment"></generator> </id> <property name="qname"></property> <list name="answers" table="answers"> <key column="qid"></key> <index column="type"></index> <element column="answer" type="string"></element> </list> </class> </hibernate-mapping>
3)在全局配置文件中增加
<!-- list of xml mapping files --> <mapping resource="list/question.hbm.xml"/>
4)测试
package list; import java.util.arraylist; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.cfg.configuration; public class test { public static void main(string[] args) { configuration cfg = new configuration(); cfg.configure("hibernate.cfg.xml"); sessionfactory factory = cfg.buildsessionfactory(); session session = factory.opensession(); session.begintransaction(); arraylist<string> list1 = new arraylist<string>(); list1.add("answer1"); list1.add("answer2"); question question1 = new question(); question1.setqname("question1"); question1.setanswers(list1); session.save(question1); session.gettransaction().commit(); session.close(); //factory.close(); } }
二 list一对多映射一个问题有多个答案,每个答案有自己的信息,需要使用一对多关联来映射。
1)创建持久化类
package list; import java.util.list; public class question { private int id; private string qname; private list<answer> answers; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getqname() { return qname; } public void setqname(string qname) { this.qname = qname; } public list<answer> getanswers() { return answers; } public void setanswers(list<answer> answers) { this.answers = answers; } }
package list; public class answer { private int id; private string answername; private string postedby; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getanswername() { return answername; } public void setanswername(string answername) { this.answername = answername; } public string getpostedby() { return postedby; } public void setpostedby(string postedby) { this.postedby = postedby; } }
2)配置文件
<?xml version='1.0' encoding='utf-8'?> <!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="list.question" table="quesion"> <cache usage="read-write"/> <id name="id"> <generator class="increment"></generator> </id> <property name="qname"></property> <list name="answers" cascade="all"> <key column="qid"></key> <index column="type"></index> <one-to-many class="list.answer"/> </list> </class> <class name="list.answer" table="answers"> <cache usage="read-write"/> <id name="id"> <generator class="increment"></generator> </id> <property name="answername"></property> <property name="postedby"></property> </class> </hibernate-mapping>
3)在hibernate.cfg.xml中添加配置
<!-- list of xml mapping files --> <mapping resource="list/question.hbm.xml"/>
4)测试
package list; import java.util.arraylist; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.cfg.configuration; public class test { public static void main(string[] args) { configuration cfg = new configuration(); cfg.configure("hibernate.cfg.xml"); sessionfactory factory = cfg.buildsessionfactory(); session session = factory.opensession(); session.begintransaction(); answer ans1 = new answer(); ans1.setanswername("ans1"); ans1.setpostedby("post1"); answer ans2 = new answer(); ans2.setanswername("ans2"); ans2.setpostedby("post2"); answer ans3 = new answer(); ans3.setanswername("ans3"); ans3.setpostedby("post3"); arraylist<answer> list1 = new arraylist<answer>(); list1.add(ans1); list1.add(ans2); list1.add(ans3); question question1 = new question(); question1.setqname("question1"); question1.setanswers(list1); session.save(question1); session.gettransaction().commit(); session.close(); //factory.close(); } }
以上就是hibernate之集合映射详解的详细内容。
