`
kidd
  • 浏览: 179653 次
  • 性别: Icon_minigender_1
  • 来自: 河南
社区版块
存档分类
最新评论

hibernate映射无主键表

阅读更多
hibernate映射无主键表时会自动生成3个文件分别为:
Table.java 代码下:
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;
}

TableId.java代码下:
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;
}

}

Table.hbm.xml代码下:

<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>

—————————————————————————————————————————

刚遇到这种无主键表映射的文件时,可能不太习惯,甚至会手动他把改成有主键表的映射文件形式.这样的话.就会出错啦.就拿最基本的查询来说.如果你改成有主键表映射文件的形式,那么Table中的custid如果有相同值的话,你从Dao中查出来的数据.凡是custid相同的数据.都会显示同一条,这不是我们想要的结果.
下面是一个简单查询案例


Dao.java 代码下:

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;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics