---
title: "hibernate映射无主键表"
author: "kidd"
date: "2009-02-20T17:01:33Z"
tags: []
slug: "hibernate"
draft: false
lastmod: "2009-02-20T17:01:33Z"
id: 336138
url: "https://kidd.iteye.com/blog/336138"
---

hibernate映射无主键表时会自动生成3个文件分别为：
<span style="color:blue">Table.java 代码下：</span>
public class Table implements java.io.Serializable {

	// Fields

	private TableId id;

	// Constructors

	/** default constructor */
	public Table() {
	}

	/** full constructor */
	public Table(TableId id) {
		this.id = id;
	}

	// Property accessors

	public TableId getId() {
		return this.id;
	}

	public void setId(TableId id) {
		this.id = id;
	}

<span style="color:blue">TableId.java代码下：</span>
package com.htdz.cust.action;

public class TableId implements java.io.Serializable {

	// Fields

	private long custid;
	private long sale;
	private long cons;
	private long bala;
	private String statperiod;
	
	// Constructors

	/** default constructor */
	public TableId() {
	}

	/** full constructor */
	public TableId(long custid, long sale, long cons, long bala,
			String statperiod) {
		this.custid = custid;
		this.sale = sale;
		this.cons = cons;
		this.bala = bala;
		this.statperiod = statperiod;
	}

	// Property accessors

	public long getCustid() {
		return this.custid;
	}

	public void setCustid(long custid) {
		this.custid = custid;
	}

	public long getSale() {
		return this.sale;
	}

	public void setSale(long sale) {
		this.sale = sale;
	}

	public long getCons() {
		return this.cons;
	}

	public void setCons(long cons) {
		this.cons = cons;
	}

	public long getBala() {
		return this.bala;
	}

	public void setBala(long bala) {
		this.bala = bala;
	}

	public String getStatperiod() {
		return this.statperiod;
	}

	public void setStatperiod(String statperiod) {
		this.statperiod = statperiod;
	}

	public boolean equals(Object other) {
		if ((this == other))
			return true;
		if ((other == null))
			return false;
		if (!(other instanceof TableId))
			return false;
		TableId castOther = (TableId) other;

		return (this.getCustid() == castOther.getCustid())
				&& (this.getSale() == castOther.getSale())
				&& (this.getCons() == castOther.getCons())
				&& (this.getBala() == castOther.getBala())
				&& ((this.getStatperiod() == castOther.getStatperiod()) || (this
						.getStatperiod() != null
						&& castOther.getStatperiod() != null && this
						.getStatperiod().equals(castOther.getStatperiod())));
	}

	public int hashCode() {
		int result = 17;

		result = 37 * result + (int) this.getCustid();
		result = 37 * result + (int) this.getSale();
		result = 37 * result + (int) this.getCons();
		result = 37 * result + (int) this.getBala();
		result = 37
				* result
				+ (getStatperiod() == null ? 0 : this.getStatperiod()
						.hashCode());
		return result;
	}

}

<span style="color:blue">Table.hbm.xml代码下：</span>

<hibernate-mapping>
    <class name="com.Cust" table="CUST" schema="MyTables">
        <composite-id name="id" class="com.model.TableId">
            <key-property name="custid" type="long">
                <column name="CUSTID" precision="22" scale="0" />
            </key-property>
            <key-property name="sale" type="long">
                <column name="SALE" precision="22" scale="0" />
            </key-property>
            <key-property name="cons" type="long">
                <column name="CONS" precision="22" scale="0" />
            </key-property>
            <key-property name="bala" type="long">
                <column name="BALA" precision="22" scale="0" />
            </key-property>
            <key-property name="statperiod" type="string">
                <column name="STATPERIOD" length="7" />
            </key-property>
        </composite-id>
    </class>
</hibernate-mapping>

—————————————————————————————————————————

<span style="color:blue">刚遇到这种无主键表映射的文件时,可能不太习惯,</span><span style="color:red">甚至会手动他把改成有主键表的映射文件形式.这样的话.就会出错啦.就拿最基本的查询来说.如果你改成有主键表映射文件的形式,那么Table中的custid如果有相同值的话,你从Dao中查出来的数据.凡是custid相同的数据.都会显示同一条,这不是我们想要的结果.</span>
<span style="color:blue">下面是一个简单查询案例</span>


<span style="color:blue">Dao.java 代码下：</span>

public List<TableId> findTable(String statperiod) {
		List<TableId> tableList = new ArrayList(); // 结果
		Session session = null;
		try {
			// hql r.id.statperiod 先获取对象r的id属性,
			// id属性类型为table,所以可以直接r.id.statperiod(TableId中的属性)
			String hql = "from Table r where r.id.statperiod = ? order by r.id.custid";
			session = this.getSession();
			Query query = session.createQuery(hql); // 执行Hql
			query.setString(0, statperiod);
			List list = query.list();
			if (list.size() != 0) {
				for (int i = 0; i < list.size(); i++) {
					// 从list中迭带出Table(也就是多个TableId对象)
					Table table = (Table) list.get(i);
					// 然后再将迭带出来的Table付给TableId方便我们在其它地方使用
					TableId tableId = table.getId();
					// 最后添加到list中
					tableList.add(tableId);
				}// end for
			}// end if
			session.flush();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			session.close();
		}
		return tableList;
	}