hibernate映射無主鍵表

hibernate映射無主鍵表時會自動生成3個文件分別爲:
[color=blue]Table.java 代碼下:[/color]
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;
}

[color=blue]TableId.java代碼下:[/color]
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;
}

}

[color=blue]Table.hbm.xml代碼下:[/color]

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

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

[color=blue]剛遇到這種無主鍵表映射的文件時,可能不太習慣,[/color][color=red]甚至會手動他把改成有主鍵表的映射文件形式.這樣的話.就會出錯啦.就拿最基本的查詢來說.如果你改成有主鍵表映射文件的形式,那麼Table中的custid如果有相同值的話,你從Dao中查出來的數據.凡是custid相同的數據.都會顯示同一條,這不是我們想要的結果.[/color]
[color=blue]下面是一個簡單查詢案例[/color]


[color=blue]Dao.java 代碼下:[/color]

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章