映射 Map(基本数据类型)(参考张龙老师的)

来源:互联网 发布:怎么在淘宝上代销宝贝 编辑:程序博客网 时间:2024/04/29 02:40

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, String> students = new HashMap<String, String>();
    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, String> getStudents() {
        return students;
    }
    public void setStudents(Map<String, String> students) {
        this.students = students;
    }
    
    
}

2.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">
            <key column="team_id"></key>
            <!-- 这里的index存放的是KEY -->
            <index column="name" type="java.lang.String"></index>
            <!-- 这里的element存入的是VALUE -->
            <element column="description" type="java.lang.String"></element>
        </map>
            
    </class>
</hibernate-mapping>


3.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);
    }
}


4. 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" />
    </session-factory>
</hibernate-configuration>


5.Test.java

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();
                team.setTeamName("team1");
                
                Map<String, String> map = team.getStudents();
                map.put("zhanggsan", "helloworld");
                map.put("lisi", "welcome");
                    
                session.save(team);
                tx.commit();
            }
            catch(Exception ex)
            {
                if (null != tx)
                {
                    tx.rollback();
                }
                
                ex.printStackTrace();
            }
            finally
            {
                session.close();
            }

       }

}