I am having two tables in my database named BlockOrder and Security with many-to-one relation between them, that is many block orders can have same security.
Now, I have to add a new BlockOrder in my database, for an existing security. Security table is read only for me.
[b]blockorder.hbm.xml[/b]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--dbishn,mjai10,agarg5 -->
<hibernate-mapping default-cascade="none">
<class name="com.sapient.sapetrm.common.domain.BlockOrder" table="TRM_EQUITY_BLOCK_ORDERS" dynamic-insert="false" dynamic-update="false">
<id name="id" type="java.lang.Long" unsaved-value="null">
<column name="EQUITY_BLOCK_ORDERS_ID" />
<generator class="native"/>
</id>
<timestamp name="version" column="TSTAMP"/>
<property name="side" type="java.lang.String">
<column name="SIDE" not-null="true" unique="false" length="5" />
</property>
<property name="status" type="java.lang.String">
<column name="STATUS" not-null="true" unique="false" length="30" />
</property>
<property name="limitPrice" type="java.lang.Double">
<column name="LIMIT_PRICE" not-null="true" unique="false" />
</property>
<property name="stopPrice" type="java.lang.Double">
<column name="STOP_PRICE" not-null="true" unique="false" />
</property>
<property name="totalQuantity" type="java.lang.Double">
<column name="TOTAL_QUANTITY" not-null="true" unique="false" />
</property>
<property name="executedQuantity" type="java.lang.Double">
<column name="EXECUTED_QUANTITY" not-null="true" unique="false" />
</property>
<property name="openQuantity" type="java.lang.Double">
<column name="OPEN_QUANTITY " not-null="true" unique="false" />
</property>
<property name="isMarketPriced" type="java.lang.Character">
<column name="IS_MARKET_PRICED" not-null="true" unique="false" length="1" />
</property>
<many-to-one name="security" class="com.sapient.sapetrm.common.domain.Security" lazy="proxy" fetch="select">
<column name="SECURITIES_ID" not-null="true" unique = "true" />
</many-to-one>
<many-to-one name="trader" class="com.sapient.sapetrm.common.domain.Trader" lazy="proxy" fetch="select">
<column name="TRADERS_ID" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
[b]security.hbm.xml[/b]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- mjai10,agarg5,dbishn -->
<hibernate-mapping default-cascade="none">
<class name="com.sapient.sapetrm.common.domain.Security" table="TRM_SECURITIES" dynamic-insert="false" dynamic-update="false">
<id name="id" type="java.lang.Long" unsaved-value="null">
<column name="SECURITIES_ID" />
<generator class="native"/>
</id>
<timestamp name="version" column="TSTAMP"/>
<property name="type" type="java.lang.String" >
<column name="TYPE" not-null="true" length="30" />
</property>
<property name="symbol" type="java.lang.String">
<column name="SYMBOL" not-null="true" length="30" />
</property>
<property name="name" type="java.lang.String" >
<column name="NAME" not-null="true" length="30" />
</property>
<property name="description" type="java.lang.String">
<column name="DESCRIPTION" not-null="true" length="256" />
</property>
</class>
</hibernate-mapping>
code I am writing is:
public Long addOrUpdateBlockOrder(BlockOrder blockOrder) {
Session session = sessionFactory.openSession();
session.saveOrUpdate(blockOrder);
if(blockOrder.getId()!=null)
return blockOrder.getId();
else{
Query query = session.createQuery("select max(equity_block_orders_id) from trm_equity_block_orders");
List list = query.list();
return (Long)list.get(0);
}
}
|