怎么实现一个POJO对应多个不同的数据库表
首先先建好一个pojo类,
public class IcWsprice implements Serializable
{
??? /**
???? *
???? */
??? private static final long serialVersionUID = 1L;
??????? // primary key
??? private java.util.Calendar icEffdate;
??? private java.lang.String icCode;
??? private java.lang.String icArea;
??? // fields
??? private java.lang.Integer icOWp;
??? private java.lang.Integer icWp
???? ..............
}
在hibernate 中mapping
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
?"-//Hibernate/Hibernate Mapping DTD//EN"
?"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.fortuneduck.ic.vo">
?<class name="IcWsprice" table="IC_HB_WSPRICE" entity-name="IcHBWsprice">
??<composite-id>
???<key-property
????column="IC_EFFDATE"
????name="icEffDate"
????type="java.util.Calendar"
??? />
???<key-property
????column="IC_CODE"
????name="icCode"
????type="string"
??? />
???<key-property
????column="IC_AREA"
????name="icArea"
????type="string"
??? />
??</composite-id>
??<property
???column="IC_O_WP"
???length="5"
???name="icOWp"
???not-null="false"
???type="integer"
?? />
??<property
???column="IC_WP"
???length="5"
???name="icWp"
???not-null="false"
???type="integer"
?? />
?</class>
?<class name="IcWsprice" table="IC_LG_WSPRICE" entity-name="IcLGWsprice">
??<composite-id>
???<key-property
????column="IC_EFFDATE"
????name="icEffDate"
????type="java.util.Calendar"
??? />
???<key-property
????column="IC_CODE"
????name="icCode"
????type="string"
??? />
???<key-property
????column="IC_AREA"
????name="icArea"
????type="string"
??? />
??</composite-id>
??<property
???column="IC_O_WP"
???length="5"
???name="icOWp"
???not-null="false"
???type="integer"
?? />
??<property
???column="IC_WP"
???length="5"
???name="icWp"
???not-null="false"
???type="integer"
?? />
?</class>
?
?<class name="IcWsprice" table="IC_WA_WSPRICE" entity-name="IcWAWsprice">
??<composite-id>
???<key-property
????column="IC_EFFDATE"
????name="icEffDate"
????type="java.util.Calendar"
??? />
???<key-property
????column="IC_CODE"
????name="icCode"
????type="string"
??? />
???<key-property
????column="IC_AREA"
????name="icArea"
????type="string"
??? />
??</composite-id>
??<property
???column="IC_O_WP"
???length="5"
???name="icOWp"
???not-null="false"
???type="integer"
?? />
??<property
???column="IC_WP"
???length="5"
???name="icWp"
???not-null="false"
???type="integer"
?? />
?</class>
?
?<class name="IcWsprice" table="IC_WT_WSPRICE" entity-name="IcWTWsprice">
??<composite-id>
???<key-property
????column="IC_EFFDATE"
????name="icEffDate"
????type="java.util.Calendar"
??? />
???<key-property
????column="IC_CODE"
????name="icCode"
????type="string"
??? />
???<key-property
????column="IC_AREA"
????name="icArea"
????type="string"
??? />
??</composite-id>
??<property
???column="IC_O_WP"
???length="5"
???name="icOWp"
???not-null="false"
???type="integer"
?? />
??<property
???column="IC_WP"
???length="5"
???name="icWp"
???not-null="false"
???type="integer"
?? />
?</class>
</hibernate-mapping>
这样就可以在DAO中找到不同的的表对应的实体了
public class ProductlineUtils
{
??? private static Map productlines = new HashMap();
??? static
??? {
??????? Map hb = new HashMap();
??????? Map wa = new HashMap();
??????? Map ls = new HashMap();
??????? Map lg = new HashMap();
??????? Map wt = new HashMap();
??????? productlines.put("HB", hb);
??????? productlines.put("WA", wa);
??????? productlines.put("LS", ls);
??????? productlines.put("LG", lg);
??????? productlines.put("WT", wt);
??????? hb.put("main1", "Handbag");
??????? wa.put("main1", "Wallet");
??????? ls.put("main1", "Shoes");
??????? lg.put("main1", "HPHandbag");
??????? wt.put("main1", "HPWallet");
??????? hb.put("stdGrade", "HBStdGrade");
??????? wa.put("stdGrade", "WAStdGrade");
??????? ls.put("stdGrade", "LSStdGrade");
??????? lg.put("stdGrade", "LGStdGrade");
??????? wt.put("stdGrade", "WTStdGrade");
??????? hb.put("pradj", "IcHBPradj");
??????? wa.put("pradj", "IcWAPradj");
??????? ls.put("pradj", "IcLSPradj");
??????? lg.put("pradj", "IcLGPradj");
??????? wt.put("pradj", "IcWTPradj");
??????? hb.put("prelat", "IcHBPrelat");
??????? wa.put("prelat", "IcWAPrelat");
??????? ls.put("prelat", "IcLSPrelat");
??????? lg.put("prelat", "IcLGPrelat");
??????? wt.put("prelat", "IcWTPrelat");
??????? hb.put("pgroup", "IcHBPgroup");
??????? wa.put("pgroup", "IcWAPgroup");
??????? ls.put("pgroup", "IcLSPgroup");
??????? lg.put("pgroup", "IcLGPgroup");
??????? wt.put("pgroup", "IcWTPgroup");
??????? hb.put("overs", "IcHBOvers");
??????? wa.put("overs", "IcWAOvers");
??????? ls.put("overs", "IcLSOvers");
??????? lg.put("overs", "IcLGOvers");
??????? wt.put("overs", "IcWTOvers");
??????? hb.put("price", "IcHBPrice");
??????? wa.put("price", "IcWAPrice");
??????? ls.put("price", "IcLSPrice");
??????? lg.put("price", "IcLGPrice");
??????? wt.put("price", "IcWTPrice");
??????? hb.put("wsprice", "IcHBWsprice");
??????? wa.put("wsprice", "IcWAWsprice");
??????? ls.put("wsprice", "IcLSWsprice");
??????? lg.put("wsprice", "IcLGWsprice");
??????? wt.put("wsprice", "IcWTWsprice");
??? }
??? public static Map getParametersOf(String icProductline)
??? {
??????? return (Map) productlines.get(icProductline);
??? }
??? public static String getEntity(String icProductline, String key)
??? {
??????? Map ps = (Map) productlines.get(icProductline);
??????? if (ps == null)
??????? {
??????????? return null;
??????? }
??????? return (String) ps.get(key);
??? }
}
DAO 中:
package com.fortuneduck.ic.dao.ic180;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.fortuneduck.ic.util.PageSeparator;
import com.fortuneduck.ic.util.ProductlineUtils;
import com.fortuneduck.ic.vo.IcPgroup;
import com.fortuneduck.ic.vo.IcWsprice;
public class IC180_DAOImpl extends HibernateDaoSupport implements IC180_DAO {
?private static final Log log = LogFactory.getLog(IC180_DAOImpl.class);
?/**
? * 查找一个IcPrice's object
? *
? * @param a
? *??????????? String <code>icCode</code>
? * @param a
? *??????????? String <code>area</code>
? * @param a
? *??????????? Calendar <code>effDate</code>
? * @param a
? *??????????? String <code>icProductline</code>
? *
? * @return a IcPrice's object
? */
?public IcWsprice getIcWsprice(String icProductline, String icCode,
???String area, Calendar effDate) {
??Session s = getSession();
??try {
???Transaction tx = s.beginTransaction();
???Query q = s.createQuery(" FROM "
?????+ ProductlineUtils.getEntity(icProductline, "wsprice")
?????+ " p WHERE p.icCode=? AND p.icArea=? AND p.icEffDate=? ");
???q.setString(0, icCode);
???q.setString(1, area);
???q.setCalendar(2, effDate);
???List list = q.list();
???tx.commit();
???
???if (list != null && list.size() > 0) {
????log.info("list");
????return (IcWsprice) list.get(0);
???}
???log.info("list is null");
??} finally {
???s.close();
??}
??return null;
?}
?public void saveIcWsprice(String icProductline, IcWsprice icWsprice) {
??this.getHibernateTemplate()
????.saveOrUpdate(
??????ProductlineUtils.getEntity(icProductline, "wsprice"),
??????icWsprice);
?}
?public void updateIcWsprice(String icProductline, IcWsprice icWsprice) {
??this.getHibernateTemplate()
????.update(ProductlineUtils.getEntity(icProductline, "wsprice"),
??????icWsprice);
?}
?public IcPgroup getIcArea(String icProductline, String icArea) {
??log.info("start search a IcPgroup? exist ??");
??List list = this.getHibernateTemplate().find(
????"FROM " + ProductlineUtils.getEntity(icProductline, "pgroup")
??????+ " p where p.icPgrp='" + icArea + "'");
??if (list != null && list.size() > 0) {
???log.info("IcPgroup exist");
???return (IcPgroup) list.get(0);
??}
??return null;
?}
?public List getWpricingList(String icProductline, Map conditionMap,
???PageSeparator pageSeparator) {
??Calendar icEffDate = (Calendar) conditionMap.get("icEffDate");
??String icCode = (String) conditionMap.get("icCode");
??String icArea = (String) conditionMap.get("icArea");
??
??String hql = "select count(*) from "+ProductlineUtils.getEntity(icProductline, "wsprice")
?????+ " p WHERE p.icCode LIKE ? AND p.icArea LIKE ? ";
??if (icEffDate != null) {
???hql += " AND p.icEffDate=?";
??}
??Session s = this.getSession();
??
??List list=null;
??try {
??
??Query q = s.createQuery(hql);
?
??
???q.setString(0, "%" + icCode + "%");
???q.setString(1, "%" + icArea + "%");
??
??if (icEffDate != null) {
???q.setCalendar(2, icEffDate);
??}
??
???? list = q.list();
????
??Iterator i = list.iterator();
??
??log.debug(i);
??s.close();
??if (i.hasNext()) {
???pageSeparator.calculate(Integer.parseInt(i.next().toString()));
???log.info(pageSeparator);
??}
??
??
??hql = "FROM " + ProductlineUtils.getEntity(icProductline, "wsprice")
????+ " p WHERE p.icCode LIKE ? AND p.icArea LIKE ? ";
??if (icEffDate != null) {
???hql += " AND p.icEffDate=?";
??}
??hql += " ORDER BY p.icEffDate DESC ";
??s = this.getSession();
??q = s.createQuery(hql);
??q.setString(0, "%" + icCode + "%");
??q.setString(1, "%" + icArea + "%");
??if (icEffDate != null) {
???q.setCalendar(2, icEffDate);
??}
??
??q.setMaxResults(pageSeparator.getPageSize());
??q.setFirstResult(pageSeparator.getStartRecord());
??list = q.list();
?
??} catch (Exception e) {
???System.out.println(e.getMessage());
??}finally{
???s.close();
??}
??return list;
?}
}
?
本人实在不会解说,自己慢慢体会吧,不懂可以问我,chengxiaoyou2000@yahoo.com.cn