hibernate 映射 Map 值對象 簡單映射

 接上篇hibernate 映射 List 值對象 簡單映射》 再將User對象做小小得修改,假設我們需要存儲工作過的部門,用map存儲部門作爲key,具體工作作爲value;

package com.ccay.test.valueCollection.map.xml;

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


public class User {
	private long userId;
	private String firstname;
	private String lastname;
	private Map<String,String> tasks = new HashMap<String,String>();
	public long getUserId() {
		return userId;
	}
	public void setUserId(long userId) {
		this.userId = userId;
	}
	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	public Map<String, String> getTasks() {
		return tasks;
	}
	public void setTasks(Map<String, String> tasks) {
		this.tasks = tasks;
	}
}


 

<?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="com.ccay.test.valueCollection.map.xml.User" table="VAL_COLLECTION_SIMPLE_SET_USER" >
        <id name="userId" type="long">
            <column name="USER_ID"/>
            <generator class="native" />
        </id>
        <property name="firstname" type="string">
            <column name="FIRSTNAME" length="40" />
        </property>
        <property name="lastname" type="string">
            <column name="LASTNAME" length="50" />
        </property>
        <map name="tasks" table="VAL_COLLECTION_SIMPLE_SET_TASKS">
        	<key column="USER_ID"/>
        	<map-key type="string" column="TASK_ORG"/>
        	<element type="string" column="TASK" not-null="true"></element>
        </map>
    </class>
</hibernate-mapping>


 

package com.ccay.test.valueCollection.map.xml;

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

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ccay.test.HibernateSessionFactory;

public class Test {
	@org.junit.Test
	public void testDDL(){
		Session session = HibernateSessionFactory.getSession();
		Transaction transaction = session.beginTransaction();
		Map<String,String> tasks = new HashMap<String,String>();
		tasks.put("org1", "student");
		tasks.put("org2", "teacher");
		
		User user= new User();
		user.setFirstname("firstName");
		user.setLastname("lastName");
		
		user.setTasks(tasks);
		
		session.save(user);
		transaction.commit();
		HibernateSessionFactory.closeSession();
	}
}


 

    create table VAL_COLLECTION_SIMPLE_SET_TASKS (
        USER_ID bigint not null,
        TASK varchar(255) not null,
        TASK_ORG varchar(255) not null,
        primary key (USER_ID, TASK_ORG)
    )
    create table VAL_COLLECTION_SIMPLE_SET_USER (
        USER_ID bigint not null auto_increment,
        FIRSTNAME varchar(40),
        LASTNAME varchar(50),
        primary key (USER_ID)
    )
    alter table VAL_COLLECTION_SIMPLE_SET_TASKS 
        add index FK839C9A2723909775 (USER_ID), 
        add constraint FK839C9A2723909775 
        foreign key (USER_ID) 
        references VAL_COLLECTION_SIMPLE_SET_USER (USER_ID)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章