日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Hibernate List集合映射

發(fā)布時間:2025/4/16 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hibernate List集合映射 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

package?model;import?java.util.List;public?class?Student1?{private?int?id;private?String?name;private?int?age;private?List<String>?hobby;public?Student1(){}public?int?getId()?{return?id;}public?void?setId(int?id)?{this.id?=?id;}public?String?getName()?{return?name;}public?void?setName(String?name)?{this.name?=?name;}public?int?getAge()?{return?age;}public?void?setAge(int?age)?{this.age?=?age;}public?List<String>?getHobby()?{return?hobby;}public?void?setHobby(List<String>?hobby)?{this.hobby?=?hobby;}} <?xml?version="1.0"?> <!DOCTYPE?hibernate-mapping?PUBLIC?"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping><!--?name屬性指定類名(全限定名)?table指明表名,不指明table數(shù)據(jù)默認的表名和實體名一致?--><class?name="model.Student1"?table="stu_tab"><!--?type指明當前字段的類型????name對應(yīng)實體中的屬性名?--><id?type="integer"?name="id"><!--?提供ID自增的策略??native會根據(jù)數(shù)據(jù)庫自行判斷?--><generator?class="native"/></id><property?name="name"?type="string"></property><property?name="age"?type="integer"></property><!--?set?標簽中name表示的是實體類中集合的屬性名稱?--><list?name="hobby"?table="stu_hobby"><!--?key子元素指明當前表的外鍵列?--><key?column="stu_id"?foreign-key="stu_hobby_fk"></key><!--?索引列,指明添加數(shù)據(jù)的順序?--><index?column="position"></index><!--?element元素是用來保存集合屬性中的值?--><element?column="hobby_name"?type="string"></element></list></class></hibernate-mapping> <!DOCTYPE?hibernate-configuration?PUBLIC"-//Hibernate/Hibernate?Configuration?DTD?3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><!--?配置連接數(shù)據(jù)庫的參數(shù)?--><!--?配置數(shù)據(jù)庫的方言?--><property?name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property><!--?數(shù)據(jù)庫驅(qū)動?--><property?name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property?name="hibernate.connection.url">jdbc:mysql:///test</property><property?name="hibernate.connection.username">root</property><property?name="hibernate.connection.password">root</property><property?name="hibernate.show_sql">true</property><!--?其它屬性配置?--><!--?指明C3P0的提供者?--><property?name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property><!--?連接池參數(shù)的配置?--><property?name="hibernate.c3p0.min_size">5</property>?<property?name="hibernate.c3p0.max_size">30</property>?<property?name="hibernate.c3p0.timeout">1800</property>?<property?name="hibernate.c3p0.max_statements">50</property><!--?打印SQL語句到控制臺?--><property?name="hibernate.show_sql">true</property><property?name="hibernate.format_sql">true</property><property?name="hibernate.hbm2ddl.auto">update</property>?<!--?注冊實體的對象關(guān)系映射文件?--><mapping?resource="model/Student.hbm.xml"/><mapping?resource="model/Student1.hbm.xml"/></session-factory> </hibernate-configuration> package?test;import?java.util.ArrayList; import?java.util.List; import?model.Student1;import?org.hibernate.Session; import?org.hibernate.Transaction; import?org.hibernate.cfg.Configuration; import?org.hibernate.tool.hbm2ddl.SchemaExport; import?org.junit.Test;import?util.HibernateUtils;public?class?Student1Test?{@Testpublic?void?createTable(){Configuration?cfg=new?Configuration().configure();SchemaExport?se=new?SchemaExport(cfg);se.create(true,?true);}@Testpublic?void?save(){Session?session=HibernateUtils.getSession();Transaction?tx=session.beginTransaction();Student1?stu=new?Student1();stu.setName("zhangsan");stu.setAge(20);List<String>list=new?ArrayList<>();list.add("eat");list.add("sleep");list.add("play");stu.setHobby(list);session.save(stu);tx.commit();HibernateUtils.close(session);}@Testpublic?void?get(){Session?session=HibernateUtils.getSession();Transaction?tx=session.beginTransaction();Student1?stu=(Student1)session.get(Student1.class,?1);System.out.println(stu.getId()+"--->"+stu.getName()+"--->"+stu.getAge());// for(String?str:stu.getHobby()){// System.out.println(str);// }tx.commit();HibernateUtils.close(session);} }


轉(zhuǎn)載于:https://my.oschina.net/u/2356966/blog/466431

總結(jié)

以上是生活随笔為你收集整理的Hibernate List集合映射的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。