映射Map(包含对象)(参考张龙老师的)

1.Team.java

package org.yang.hibernate;


import java.util.HashMap;
import java.util.Map;

public class Team
{
    private String id;
    private String teamName;
    private Map<String, Student> students = new HashMap<String, Student>();
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getTeamName() {
        return teamName;
    }
    public void setTeamName(String teamName) {
        this.teamName = teamName;
    }
    public Map<String, Student> getStudents() {
        return students;
    }
    public void setStudents(Map<String, Student> students) {
        this.students = students;
    }
}


2.Student.java:

package org.yang.hibernate;

public class Student
{
    private String id;
    private String cardId;
    private String name;
    private int age;
    
    private Team team;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    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 Team getTeam() {
        return team;
    }

    public void setTeam(Team team) {
        this.team = team;
    }
    
    
}

3.Team.hbm.xml:

<?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="org.yang.hibernate.Team" table="team">
        <id name="id" column="id" type="string">
            <generator class="uuid"></generator>
        </id>
        
        <property name="teamName" type="string">
            <column name="teamname" length="13"></column>
        </property>
                
        <map name="students" table="student" cascade="all">
            <key column="team_id"></key>
            <!-- 这里的index存放的是KEY -->
            <index column="card_id" type="java.lang.String"></index>
            
            <one-to-many class="org.yang.hibernate.Student"/>
        </map>
            
    </class>
</hibernate-mapping>

4.Student.hbm.xml;

<?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="org.yang.hibernate.Student" table="student">
        <id name="id" column="id" type="string">
            <generator class="uuid"></generator>
        </id>
        
        <property name="cardId" column="card_id" type="string"></property>
        <property name="name" column="name" type="string"></property>
        <property name="age" column="age" type="integer"></property>
                
        <many-to-one name="team" column="team_id" class="org.yang.hibernate.Team" fetch="join"></many-to-one>
            
    </class>
</hibernate-mapping>

5.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" >
<hibernate-configuration>
  <session-factory>
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://localhost:5432/myhibernate</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">yang</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="current_session_context_class">thread</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        
        <mapping resource="Team.hbm.xml" />
        <mapping resource="Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


6.CreateTable.java

package org.yang.hibernate;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class CreateTable
{
    public static void main(String[] args)
    {
        SchemaExport export = new SchemaExport(new Configuration().configure());
        export.create(true, true);
    }
}


7.测试文件:

package org.yang.hibernate;

import java.util.Map;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Test
{
        private static SessionFactory sessionFactory;
        static
        {
            try
            {
                sessionFactory = new Configuration().configure().buildSessionFactory();
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
        
        public static void main(String[] args)
        {        
            Session session = sessionFactory.openSession();
            Transaction tx = null;
            try
            {
                tx = session.beginTransaction();
                Team team = new Team();
                
                Student student = new Student();
                student.setAge(20);
                student.setName("zhangsan");
                student.setTeam(team);
                
                Student student2 = new Student();
                student2.setAge(20);
                student2.setName("zhangsan2");
                student2.setTeam(team);
                
                Student student3 = new Student();
                student3.setAge(20);
                student3.setName("zhangsan3");
                student3.setTeam(team);
                
                team.setTeamName("team1");
                
                Map<String, Student> map = team.getStudents();
                map.put("111",student);
                map.put("222", student2);
                map.put("333", student3);
                    
                session.save(team);
                tx.commit();
            }
            catch(Exception ex)
            {
                if (null != tx)
                {
                    tx.rollback();
                }
                
                ex.printStackTrace();
            }
            finally
            {
                session.close();
            }

     }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值