您的位置:首页 > 其它

Hibernate 调用带有复合主键的stored procedure

2006-03-29 12:52 471 查看
Hibernate 调用带有复合主键的stored procedure
Mapping file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/ http://www.hibernate.org/
-->

<class
name="com.bgi.fia.hibernate.TestTable"
table="TestTable"
>
<meta attribute="class-description" inherit="false">
@hibernate.class
table="TestTable"
</meta>

<composite-id name="comp_id" class="com.bgi.fia.hibernate.TestTablePK">
<meta attribute="field-description" inherit="false">
@hibernate.id
generator-class="assigned"
</meta>
<key-property
name="keyDate"
column="keyDate"
type="java.sql.Timestamp"
length="23"
>
<meta attribute="field-description">
@hibernate.property
column="keyDate"
</meta>
</key-property>
<key-property
name="keyString"
column="keyString"
type="java.lang.String"
length="50"
>
<meta attribute="field-description">
@hibernate.property
column="keyString"
</meta>
</key-property>
<key-property
name="keyInt"
column="keyInt"
type="java.lang.Integer"
length="10"
>
<meta attribute="field-description">
@hibernate.property
column="keyInt"
</meta>
</key-property>
</composite-id>

<property
name="conteng"
type="java.lang.String"
column="conteng"
length="200"
>
<meta attribute="field-description">
@hibernate.property
column="conteng"
length="200"
</meta>
</property>

<!-- Associations -->
<!-- derived association(s) for compound key -->
<!-- end of derived association(s) -->

<loader query-ref="selectAllValues_SP"></loader>
</class>
<sql-query name="selectAllValues_SP" callable="true">
<return alias="testTable" class="com.bgi.fia.hibernate.TestTable">
<return-property name="comp_id">
<return-column name="keyDate"/>
<return-column name="keyString"/>
<return-column name="keyInt"/>
</return-property>
<return-property name="conteng" column="conteng"/>
</return>
{call selectAllValues}
</sql-query>
</hibernate-mapping>
java class:
TestTable.java
package com.bgi.fia.hibernate;

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
* @hibernate.class
* table="TestTable"
*
*/
public class TestTable implements Serializable {

/** identifier field */
private com.bgi.fia.hibernate.TestTablePK comp_id;

/** nullable persistent field */
private String conteng;

/** full constructor */
public TestTable(com.bgi.fia.hibernate.TestTablePK comp_id, String conteng) {
this.comp_id = comp_id;
this.conteng = conteng;
}

/** default constructor */
public TestTable() {
}

/** minimal constructor */
public TestTable(com.bgi.fia.hibernate.TestTablePK comp_id) {
this.comp_id = comp_id;
}

/**
* @hibernate.id
* generator-class="assigned"
*
*/
public com.bgi.fia.hibernate.TestTablePK getComp_id() {
return this.comp_id;
}

public void setComp_id(com.bgi.fia.hibernate.TestTablePK comp_id) {
this.comp_id = comp_id;
}

/**
* @hibernate.property
* column="conteng"
* length="200"
*
*/
public String getConteng() {
return this.conteng;
}

public void setConteng(String conteng) {
this.conteng = conteng;
}

public String toString() {
return new ToStringBuilder(this)
.append("comp_id", getComp_id())
.toString();
}

public boolean equals(Object other) {
if ( !(other instanceof TestTable) ) return false;
TestTable castOther = (TestTable) other;
return new EqualsBuilder()
.append(this.getComp_id(), castOther.getComp_id())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getComp_id())
.toHashCode();
}

}
TestTablePK.java:
package com.bgi.fia.hibernate;

import java.io.Serializable;
import java.util.Date;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/** @author Hibernate CodeGenerator */
public class TestTablePK implements Serializable {

/** identifier field */
private Date keyDate;

/** identifier field */
private String keyString;

/** identifier field */
private Integer keyInt;

/** full constructor */
public TestTablePK(Date keyDate, String keyString, Integer keyInt) {
this.keyDate = keyDate;
this.keyString = keyString;
this.keyInt = keyInt;
}

/** default constructor */
public TestTablePK() {
}

/**
* @hibernate.property
* column="keyDate"
*
*/
public Date getKeyDate() {
return this.keyDate;
}

public void setKeyDate(Date keyDate) {
this.keyDate = keyDate;
}

/**
* @hibernate.property
* column="keyString"
*
*/
public String getKeyString() {
return this.keyString;
}

public void setKeyString(String keyString) {
this.keyString = keyString;
}

/**
* @hibernate.property
* column="keyInt"
*
*/
public Integer getKeyInt() {
return this.keyInt;
}

public void setKeyInt(Integer keyInt) {
this.keyInt = keyInt;
}

public String toString() {
return new ToStringBuilder(this)
.append("keyDate", getKeyDate())
.append("keyString", getKeyString())
.append("keyInt", getKeyInt())
.toString();
}

public boolean equals(Object other) {
if ( !(other instanceof TestTablePK) ) return false;
TestTablePK castOther = (TestTablePK) other;
return new EqualsBuilder()
.append(this.getKeyDate(), castOther.getKeyDate())
.append(this.getKeyString(), castOther.getKeyString())
.append(this.getKeyInt(), castOther.getKeyInt())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getKeyDate())
.append(getKeyString())
.append(getKeyInt())
.toHashCode();
}

}
TestHibernateSP.java:
package com.bgi.fia.test;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.bgi.fia.hibernate.HibernateSessionFactory;

public class TestHibernateSP {

/**
* @param args
*/
public static void main(String[] args) {
Session session = HibernateSessionFactory.currentSession();
Query query = session.getNamedQuery("selectAllValues_SP");
List list = query.list();
System.out.println(list);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: