[size=x-large]现[/size]开发一个小项目,其中有一个需求,要保存往年的数据,一年一个数据,按以前的习惯就会是做一个从表指向主表。但我不想这么做,我分析了一下,做从表的意义不大。一是只有两个字段,年和值,二是不会单独去查从表。所以,我就想使用Hibernate中的UserType。
[b]1,在数据库中,加一个字段[/b]
[b]2,修改XML文件[/b]
[b]3,修改POJO[/b]
[b]4,PcYearScheList类[/b]
[b]5,PcYearSche类[/b]
[b]6,操作项目表年执行进度字段[/b]
[b]1,在数据库中,加一个字段[/b]
alter table TLPC.PC_PROJECT add SCHE_YEAR_SCHE VARCHAR2(1024);
[b]2,修改XML文件[/b]
<property
name="scheYearSche"
column="SCHE_YEAR_SCHE"
type="com.XXXX.tlpc.worktime.model.PcYearScheList"
>
[b]3,修改POJO[/b]
public class PcProject extends com.icsshs.tlpc.framework.model.BaseObject implements java.io.Serializable {
private List scheYearSche;//userType要处理的
public List getScheYearSche() {
return scheYearSche;
}
public void setScheYearSche(List scheYearSche) {
this.scheYearSche = scheYearSche;
}
}
[b]4,PcYearScheList类[/b]
package com.XXXXX.tlpc.worktime.model;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
import com.XXXXX.tlpc.DatabaseConstants;
/**
* hibernate 用户自定义类型
* @author chenbf
*/
public class PcYearScheList implements UserType {
private static final String SPLITTER = ";";
private static final int[] TYPES = new int[] { Types.VARCHAR };
/*
* (non-Javadoc)
*
* @see net.sf.hibernate.UserType#sqlTypes()
*/
public int[] sqlTypes() {
return TYPES;
}
/*
* (non-Javadoc)
*
* @see net.sf.hibernate.UserType#returnedClass()
*/
public Class returnedClass() {
return List.class;
}
/*
* (non-Javadoc)
*
* @see net.sf.hibernate.UserType#equals(java.lang.Object, java.lang.Object)
*/
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y) return true;
if (x != null && y != null) {
List xList = (List) x;
List yList = (List) y;
if (xList.size() != yList.size()) return false;
for (int i = 0; i < xList.size(); i++) {
PcYearSche str1 = (PcYearSche) xList.get(i);
PcYearSche str2 = (PcYearSche) yList.get(i);
if (!str1.equals(str2)) return false;
}
return true;
}
return false;
}
/*
* (non-Javadoc)
*
* @see net.sf.hibernate.UserType#nullSafeGet(java.sql.ResultSet,
* java.lang.String[], java.lang.Object)
*/
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
String value = (String) Hibernate.STRING.nullSafeGet(rs, names[0]);
if (value != null) {
return parse(value);
} else {
return null;
}
}
/*
* (non-Javadoc)
*
* @see net.sf.hibernate.UserType#nullSafeSet(java.sql.PreparedStatement,
* java.lang.Object, int)
*/
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
System.out.println("Set method excecuted");
if (value != null) {
String str = assemble((List) value);
Hibernate.STRING.nullSafeSet(st, str, index);
} else {
Hibernate.STRING.nullSafeSet(st, value, index);
}
}
/*
* (non-Javadoc)
*
* @see net.sf.hibernate.UserType#deepCopy(java.lang.Object)
*/
public Object deepCopy(Object value) throws HibernateException {
List sourcelist = (List) value;
List targetlist = new ArrayList();
if(sourcelist!=null)
targetlist.addAll(sourcelist);
return targetlist;
}
/*
* (non-Javadoc)
*
* @see net.sf.hibernate.UserType#isMutable()
*/
public boolean isMutable() {
return false;
}
/**
* 拼装,以“;”为分隔
* @param splitList
* @return
*/
private String assemble(List splitList) {
StringBuffer strBuf = new StringBuffer();
for (int i = 0; i < splitList.size() - 1; i++) {
PcYearSche pcYearSche = (PcYearSche)splitList.get(i);
String strPcScheYear = pcYearSche.getScheYear()+ DatabaseConstants.SPLITTER + pcYearSche.getTotalSche();
strBuf.append(strPcScheYear).append(SPLITTER);
}
PcYearSche pcYearSche = (PcYearSche)splitList.get(splitList.size() - 1);
String strPcScheYear = pcYearSche.getScheYear()+ DatabaseConstants.SPLITTER + pcYearSche.getTotalSche();
strBuf.append(strPcScheYear);
return strBuf.toString();
}
/**
* 解析
* @param value
* @return
*/
private List parse(String value) {
String[] strs = StringUtils.split(value, SPLITTER);
List splitList = new ArrayList();
for (int i = 0; i < strs.length; i++) {
PcYearSche pcYearSche = new PcYearSche();
String splitter = "\\" + DatabaseConstants.SPLITTER;
String[] strsYearSche = StringUtils.split(strs[i], splitter);
pcYearSche.setScheYear(strsYearSche[0]);
pcYearSche.setTotalSche(strsYearSche[1]);
splitList.add(pcYearSche);
}
return splitList;
}
public Object assemble(Serializable arg0, Object arg1) throws HibernateException {
return null;
}
public Serializable disassemble(Object arg0) throws HibernateException {
return null;
}
public int hashCode(Object arg0) throws HibernateException {
return 0;
}
public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException {
return null;
}
public static List replace(List pcYearScheList, String scheYear, String totalSche){
boolean isExist = false;
if (pcYearScheList != null){
//判断有无,有则覆盖
for (Iterator iter = pcYearScheList.iterator(); iter.hasNext();) {
PcYearSche pcYearSche = (PcYearSche) iter.next();
if (pcYearSche.getScheYear().equals(scheYear)){
pcYearSche.setTotalSche(totalSche);
isExist = true;
}
}
}
//无则新增一条
if (!isExist){
PcYearSche pcYearSche = new PcYearSche();
pcYearSche.setScheYear(scheYear);
pcYearSche.setTotalSche(totalSche);
pcYearScheList.add(pcYearSche);
}
return pcYearScheList;
}
}
[b]5,PcYearSche类[/b]
package com.XXXXX.tlpc.worktime.model;
/**
*
* 年累计进度单元
* @author linhw
*
*/
public class PcYearSche {
private String scheYear;
private String totalSche;
public String getScheYear() {
return scheYear;
}
public void setScheYear(String scheYear) {
this.scheYear = scheYear;
}
public String getTotalSche() {
return totalSche;
}
public void setTotalSche(String totalSche) {
this.totalSche = totalSche;
}
}
[b]6,操作项目表年执行进度字段[/b]
/**
* 操作项目表年执行进度字段
* @param pcProjectWeekly
* @param pcProject
* @return
*/
private List setScheYearSche(PcProjectWeekly pcProjectWeekly, PcProject pcProject) {
List scheYearSches = pcProject.getScheYearSche();
String strYear = Integer.toString(PcDateUtil.getYear(pcProjectWeekly.getReportDate()));
String totalSche = pcProjectWeekly.getScheThisWeekTotal().toString();
scheYearSches = PcYearScheList.replace(scheYearSches, strYear, totalSche);
return scheYearSches;
}