|
Hibernate example source code file (Mappings.hbm.xml)
The Hibernate Mappings.hbm.xml source code<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- This mapping demonstrates the use of Hibernate with all-handwritten SQL! NOTE: this version is for Sybase --> <hibernate-mapping package="org.hibernate.test.sql.hand" default-access="field"> <class name="Organization" table="ORGANIZATION"> <id name="id" unsaved-value="0" column="ORGID"> <generator class="increment"/> </id> <property name="name" not-null="true" column="NAME"/> <set name="employments" inverse="true" order-by="DUMMY"> <key column="EMPLOYER"/> <one-to-many class="Employment"/> <loader query-ref="organizationEmployments"/> </set> <!-- query-list name="currentEmployments" query-ref="organizationCurrentEmployments"--> <loader query-ref="organization"/> <sql-insert>INSERT INTO ORGANIZATION (NAME, ORGID) VALUES ( UPPER(?), ? ) <sql-update>UPDATE ORGANIZATION SET NAME=UPPER(?) WHERE ORGID=? <sql-delete>DELETE FROM ORGANIZATION WHERE ORGID=? </class> <class name="Person" table="PERSON"> <id name="id" unsaved-value="0" column="PERID"> <generator class="increment"/> </id> <property name="name" not-null="true" column="NAME"/> <loader query-ref="person"/> <sql-insert>INSERT INTO PERSON (NAME, PERID) VALUES ( UPPER(?), ? ) <sql-update>UPDATE PERSON SET NAME=UPPER(?) WHERE PERID=? <sql-delete>DELETE FROM PERSON WHERE PERID=? </class> <class name="Employment" table="EMPLOYMENT"> <id name="employmentId" unsaved-value="0" column="EMPID"> <generator class="increment"/> </id> <many-to-one name="employee" column="EMPLOYEE" not-null="true" update="false"/> <many-to-one name="employer" column="EMPLOYER" not-null="true" update="false"/> <property name="startDate" column="STARTDATE" not-null="true" update="false" insert="false"/> <property name="endDate" column="ENDDATE" insert="false"/> <property name="regionCode" column="REGIONCODE" update="false"/> <property name="salary" type="org.hibernate.test.sql.hand.MonetaryAmountUserType"> <column name="VALUE" sql-type="float"/> <column name="CURRENCY"/> </property> <loader query-ref="employment"/> <sql-insert> INSERT INTO EMPLOYMENT (EMPLOYEE, EMPLOYER, STARTDATE, REGIONCODE, VALUE, CURRENCY, EMPID) VALUES (?, ?, getdate(), UPPER(?), ?, ?, ?) </sql-insert> <sql-update>UPDATE EMPLOYMENT SET ENDDATE=?, VALUE=?, CURRENCY=? WHERE EMPID=? <sql-delete>DELETE FROM EMPLOYMENT WHERE EMPID=? </class> <class name="TextHolder"> <id name="id" column="id"> <generator class="increment"/> </id> <property name="description" type="text" length="15000"/> <loader query-ref="textholder"/> <sql-insert> INSERT INTO TEXTHOLDER (DESCRIPTION, ID) VALUES (?, ?) </sql-insert> <sql-update>UPDATE TEXTHOLDER SET DESCRIPTION=? WHERE ID=? <sql-delete>DELETE FROM TEXTHOLDER WHERE ID=? </class> <class name="ImageHolder"> <id name="id" column="id"> <generator class="increment"/> </id> <property name="photo" type="image" length="15000"/> <loader query-ref="imageholder"/> <sql-insert> INSERT INTO IMAGEHOLDER (PHOTO, ID) VALUES (?, ?) </sql-insert> <sql-update>UPDATE IMAGEHOLDER SET PHOTO=? WHERE ID=? <sql-delete>DELETE FROM IMAGEHOLDER WHERE ID=? </class> <resultset name="org-emp-regionCode"> <return-scalar column="regionCode" type="string"/> <return alias="org" class="Organization"/> <return-join alias="emp" property="org.employments"/> </resultset> <resultset name="org-emp-person"> <return alias="org" class="Organization"/> <return-join alias="emp" property="org.employments"/> <return-join alias="pers" property="emp.employee"/> </resultset> <sql-query name="person"> <return alias="p" class="Person" lock-mode="upgrade"/> SELECT NAME AS {p.name}, PERID AS {p.id} FROM PERSON WHERE PERID=? /*FOR UPDATE*/ </sql-query> <sql-query name="organization"> <return alias="org" class="Organization"/> <return-join alias="emp" property="org.employments"/> SELECT {org.*}, {emp.*} FROM ORGANIZATION org LEFT OUTER JOIN EMPLOYMENT emp ON org.ORGID = emp.EMPLOYER WHERE org.ORGID=? </sql-query> <!--sql-query name="organization"> <return alias="org" class="Organization"/> SELECT NAME AS {org.name}, ORGID AS {org.id} FROM ORGANIZATION WHERE ORGID=? </sql-query--> <sql-query name="allOrganizationsWithEmployees" flush-mode="never"> <return alias="org" class="Organization"/> SELECT DISTINCT org.NAME AS {org.name}, org.ORGID AS {org.id} FROM ORGANIZATION org INNER JOIN EMPLOYMENT e ON e.EMPLOYER = org.ORGID </sql-query> <sql-query name="employment"> <return alias="emp" class="Employment"/> SELECT EMPLOYEE AS {emp.employee}, EMPLOYER AS {emp.employer}, STARTDATE AS {emp.startDate}, ENDDATE AS {emp.endDate}, REGIONCODE as {emp.regionCode}, EMPID AS {emp.id} FROM EMPLOYMENT WHERE EMPID = ? </sql-query> <sql-query name="organizationEmployments"> <load-collection alias="empcol" role="Organization.employments"/> SELECT {empcol.*} FROM EMPLOYMENT empcol WHERE EMPLOYER = :id ORDER BY STARTDATE ASC, EMPLOYEE ASC </sql-query> <sql-query name="organizationCurrentEmployments"> <return alias="emp" class="Employment"> <return-property name="salary"> <!-- as multi column properties are not supported via the {}-syntax, we need to provide an explicit column list for salary via <return-property> --> <return-column name="VALUE"/> <return-column name="CURRENCY"/> </return-property> <!-- Here we are remapping endDate. Notice that we can still use {emp.endDate} in the SQL. --> <return-property name="endDate" column="myEndDate"/> </return> <synchronize table="EMPLOYMENT"/> SELECT EMPLOYEE AS {emp.employee}, EMPLOYER AS {emp.employer}, STARTDATE AS {emp.startDate}, ENDDATE AS {emp.endDate}, REGIONCODE as {emp.regionCode}, EMPID AS {emp.id}, VALUE, CURRENCY FROM EMPLOYMENT WHERE EMPLOYER = :id AND ENDDATE IS NULL ORDER BY STARTDATE ASC </sql-query> <sql-query name="simpleScalar" callable="true"> <return-scalar column="name" type="string"/> <return-scalar column="value" type="long"/> { call simpleScalar(:number) } </sql-query> <sql-query name="paramhandling" callable="true"> <return-scalar column="value" type="long"/> <return-scalar column="value2" type="long"/> { call paramHandling(?,?) } </sql-query> <sql-query name="paramhandling_mixed" callable="true"> <return-scalar column="value" type="long" /> <return-scalar column="value2" type="long" /> { call paramHandling(?,:second) } </sql-query> <sql-query name="selectAllEmployments" callable="true"> <return alias="" class="Employment"> <return-property name="employee" column="EMPLOYEE"/> <return-property name="employer" column="EMPLOYER"/> <return-property name="startDate" column="STARTDATE"/> <return-property name="endDate" column="ENDDATE"/> <return-property name="regionCode" column="REGIONCODE"/> <return-property name="id" column="EMPID"/> <return-property name="salary"> <!-- as multi column properties are not supported via the {}-syntax, we need to provide an explicit column list for salary via <return-property> --> <return-column name="VALUE"/> <return-column name="CURRENCY"/> </return-property> </return> { call selectAllEmployments() } </sql-query> <sql-query name="textholder"> <return alias="h" class="TextHolder" lock-mode="upgrade"/> SELECT ID AS {h.id}, DESCRIPTION AS {h.description} FROM TEXTHOLDER WHERE ID=? /*FOR UPDATE*/ </sql-query> <sql-query name="imageholder"> <return alias="h" class="ImageHolder" lock-mode="upgrade"/> SELECT ID AS {h.id}, PHOTO AS {h.photo} FROM IMAGEHOLDER WHERE ID=? /*FOR UPDATE*/ </sql-query> <database-object> <create> CREATE PROCEDURE selectAllEmployments AS SELECT EMPLOYEE, EMPLOYER, STARTDATE, ENDDATE, REGIONCODE, EMPID, VALUE, CURRENCY FROM EMPLOYMENT </create> <drop> DROP PROCEDURE selectAllEmployments </drop> </database-object> <!-- The following is a workaround for HHH-3685 so that SchemaExport --> <!-- changes the transaction mode of selectAllEmployments to "chained" --> <database-object> <create> sp_procxmode selectAllEmployments, 'chained' </create> <!-- no drop command corresponding to "sp_procxmode ..." --> <drop/> </database-object> <database-object> <create> CREATE PROCEDURE paramHandling @j int, @i int AS SELECT @j as value, @i as value2 </create> <drop> DROP PROCEDURE paramHandling </drop> </database-object> <!-- The following is a workaround for HHH-3685 so that SchemaExport --> <!-- changes the transaction mode of paramHandling to "chained" --> <database-object> <create> sp_procxmode paramHandling, 'chained' </create> <!-- no drop command corresponding to "sp_procxmode ..." --> <drop/> </database-object> <database-object> <create> CREATE PROCEDURE simpleScalar @number int AS SELECT @number as value, 'getAll' as name </create> <drop> DROP PROCEDURE simpleScalar </drop> </database-object> <!-- The following is a workaround for HHH-3685 so that SchemaExport --> <!-- changes the transaction mode of simpleScalar to "chained" --> <database-object> <create> sp_procxmode simpleScalar, 'chained' </create> <!-- no drop command corresponding to "sp_procxmode ..." --> <drop/> </database-object> </hibernate-mapping> Other Hibernate examples (source code examples)Here is a short list of links related to this Hibernate Mappings.hbm.xml source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.