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);
}
}