Next, if hibernate ing bag/idbag value object simple ing allows a user to add multiple tasks and record the order, what should I do?
This is the same as the list semantics and is mapped using list.
package com.ccay.test.valueCollection.list.xml;import java.util.ArrayList;import java.util.List;public class User {private long userId;private String firstname;private String lastname;private List<String> tasks = new ArrayList<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 List<String> getTasks() {return tasks;}public void setTasks(List<String> tasks) {this.tasks = tasks;}}
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">
Test
package com.ccay.test.valueCollection.list.xml;import java.util.ArrayList;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();ArrayList<String> tasks = new ArrayList<String>();tasks.add("student");tasks.add("student");tasks.add("progarm");tasks.add("doctor");User user= new User();user.setFirstname("firstName");user.setLastname("lastName");user.setTasks(tasks);session.save(user);transaction.commit();HibernateSessionFactory.closeSession();}}
Data script
create table VAL_COLLECTION_SIMPLE_SET_TASKS ( USER_ID bigint not null, TASK varchar(255) not null, POSITION integer not null, primary key (USER_ID, POSITION) ) 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 FK839C9A2766CC214F (USER_ID), add constraint FK839C9A2766CC214F foreign key (USER_ID) references VAL_COLLECTION_SIMPLE_SET_USER (USER_ID)
Reverse Engineering Drawing