Hibernate ing MAP value object simple ing

Source: Internet
Author: User

Next, Hibernate ing List Value Object simple ing
Then, make a small modification to the user object. Assume that we need to store the working department, and use the MAP storage Department as the key, and the specific work as the 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">

 

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.Testpublic 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.