映射集合屬性之③:Set集合屬性

來源:互聯網
上載者:User

hibernate.cfg.xml :

<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   --><hibernate-configuration>    <session-factory>        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>        <property name="connection.url">jdbc:mysql://localhost:3306/temp</property>        <property name="connection.username">root</property>        <property name="connection.password">mysqladmin</property>        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="hbm2ddl.auto">update</property>        <property name="show_sql">true</property>        <property name="format_sql">true</property>        <mapping resource="db/mapping/Person.hbm.xml"/>    </session-factory></hibernate-configuration>

Person.java :

public class Person {private int id;private String name;private int age;private Set<String> schools=new HashSet<String>();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 Set<String> getSchools() {return schools;}public void setSchools(Set<String> schools) {this.schools = schools;}}

Person.hbm.xml :

<?xml version="1.0"?>  <!DOCTYPE hibernate-mapping PUBLIC       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">        <hibernate-mapping package="db.domain">    <class name="Person" table="persons">            <id name="id">            <generator class="identity"></generator>        </id>                <property name="name" type="string"/>        <property name="age" type="integer"/>                <!-- 映射set集合屬性 -->        <set name="schools" table="schools">            <key column="person_id" not-null="true"/>            <element column="school_name" type="string" not-null="true"/>        </set>             </class></hibernate-mapping>

Test.java :

public class Test {public static void main(String[] args) {Session session=HibernateSessionFactory.getSession();Transaction txt=session.beginTransaction();Person p1=new Person();p1.setName("小紅");p1.setAge(18);Set<String> schools1=new HashSet<String>();schools1.add("小學");schools1.add("初中");schools1.add("高中");p1.setSchools(schools1);Person p2=new Person();p2.setName("小明");p2.setAge(22);Set<String> schools2=new HashSet<String>();schools2.add("小學");schools2.add("初中");schools2.add("高中");schools2.add("大學");p2.setSchools(schools2);session.save(p1);session.save(p2);txt.commit();HibernateSessionFactory.closeSession();}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.