-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: one-to-many relationship supporting traversal from one side
PostPosted: Tue Feb 01, 2005 4:28 pm 
Newbie

Joined: Fri Oct 22, 2004 10:55 am
Posts: 17
[b]Hibernate version: 2.1.6[/b]

We have one-to-many relationship between two tables.

Transaction can have many Alternate_transaction_id

We want to set up the relationship so that
1. Traversal from transaction to alternate_transaction_id is supported.
2. Traversal from alternate_transaction_id to transaction is NOT supported. When alternate transaction id is retrieved
directly there should not be any retrieval of transaction as this object is very heavy because it has relationship with
lot of other tables and all those associations are loaded eagerly.
3. Changes to the alternate_transaction_id is persisted along with transaction.

We have set it up as follows which seems to work. I just want to make sure that it the best way to accomplish it. I would like to simplify the POJO AlternateTransactionId if possible. I will appreciate any comments or suggestion to improve it.

Mapping for transaction table
=============================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="com.les.ts.core.persistence2">
<class name="Transaction" table="TRANSACTION">
<!-- Id -->
<id name="transactionId" type="big_decimal" column="TRANSACTION_ID">
<generator class="com.les.ts.core.persistence2.HibernateIdGenerator"/>
</id>
<!-- Properties -->
<property name="transactionBusinessEventNm" type="string" column="TRANSACTION_BUSINESS_EVENT_NM"/>
<!-- one-to-many (parent/child) association to AlternateTransactionId -->
<set name="alternateTransactionIds" lazy="false" inverse="true" cascade="all-delete-orphan">
<key column="PRIMARY_TRANSACTION_ID"/>
<one-to-many class="AlternateTransactionId"/>
</set>
</class>
</hibernate-mapping>


Mapping for alternate transaction id
=======================================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="com.les.ts.core.persistence2">
<class name="AlternateTransactionId" table="ALTERNATE_TRANSACTION_ID">
<!-- Id -->
<id name="alternateTransactionId" type="big_decimal" column="ALTERNATE_TRANSACTION_ID">
<generator class="com.les.ts.core.persistence2.HibernateIdGenerator"/>
</id>
<!-- primaryTransactionId is foreign key to transaction -->
<property name="primaryTransactionId" type="big_decimal" column="PRIMARY_TRANSACTION_ID"/>
<property name="alternateTransIdValueTxt" type="string" column="ALTERNATE_TRANS_ID_VALUE_TXT"/>
</class>
</hibernate-mapping>


public class Transaction
{
private BigDecimal transactionId;
private String transactionBusinessEventNm;
private Set alternateTransactionIds = new HashSet();

public BigDecimal getTransactionId()
{
return transactionId;
}

private void setTransactionId(BigDecimal transactionId)
{
this.transactionId = transactionId;
}


public String getTransactionBusinessEventNm()
{
return transactionBusinessEventNm;
}

public void setTransactionBusinessEventNm(String transactionBusinessEventNm)
{
this.transactionBusinessEventNm = transactionBusinessEventNm;
}


public Set getAlternateTransactionIds()
{
return this.alternateTransactionIds;
}

public void removeAlternateTransactionId(AlternateTransactionId alternateTransactionId)
{
if (alternateTransactionId == null)
return;
getAlternateTransactionIds().remove(alternateTransactionId);
}

public void addAlternateTransactionId(AlternateTransactionId alternateTransactionId)
{
if (alternateTransactionId == null)
return;
getAlternateTransactionIds().add(alternateTransactionId);
alternateTransactionId.setTransaction(this);
}

private void setAlternateTransactionIds(Set alternateTransactionIds)
{
this.alternateTransactionIds = alternateTransactionIds;
}
}



public class AlternateTransactionId
{

/** identifier field */
private BigDecimal alternateTransactionId;

/** nullable persistent field */
private Transaction transaction;

/** nullable persistent field */
private BigDecimal primaryTransactionId;

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


public BigDecimal getAlternateTransactionId()
{
return alternateTransactionId;
}

private void setAlternateTransactionId(BigDecimal alternateTransactionId)
{
this.alternateTransactionId = alternateTransactionId;
}

public BigDecimal getPrimaryTransactionId()
{
Transaction transaction = getTransaction();
if (transaction != null)
return transaction.getTransactionId();
else
return primaryTransactionId;
}

public void setPrimaryTransactionId(BigDecimal primaryTransactionId)
{
this.primaryTransactionId = primaryTransactionId;
}

private Transaction getTransaction()
{
return transaction;
}

void setTransaction(Transaction transaction)
{
this.transaction = transaction;
}


public String getAlternateTransIdValueTxt()
{
return alternateTransIdValueTxt;
}

public void setAlternateTransIdValueTxt(String alternateTransIdValueTxt)
{
this.alternateTransIdValueTxt = alternateTransIdValueTxt;
}

}


Top
 Profile  
 
 Post subject: Help
PostPosted: Thu Feb 03, 2005 11:26 am 
Newbie

Joined: Fri Oct 22, 2004 10:55 am
Posts: 17
Can somebody please comment on this. I have to set up lot of relationships this way.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.