hi,
i want to have a sort of half bidirectional
association. that is one end of the association
should be in charge of managing the association
but i want to be able to retrieve the other
end of the association from the db and navigate
back to the managing association.
in principle this is possible with a bit of
code...but i would like to declare that via
mapping docs.
ok lets have an example
Code:
public class Session
{
public String getId(){...}
public void setId(String _id){...}
public List getTransactions(){...}
public void setTransactions(List _val){...}
}
public class Transaction
{
public String getId(){...}
public void setId(String _id){...}
public Session getParent(){...}
public void setParent(Session _ses){...}
}
so basically this is a parent/child association
between session and transactions. simple
enough!
Mapping documents:Code:
<hibernate-mapping>
<class name="Session"
table="SESSION"
schema="SCHEMA" >
<id name="id" column="ID" type="string" >
<generator class="uuid.hex">
</generator>
</id>
<list name="transactions"
schema="SCHEMA"
table="TX"
lazy="false"
cascade="all"
inverse="false">
<key column="SESSION_ID"/>
<index column="POS"/>
<one-to-many class="Transaction"/>
</list>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="Transaction"
table="TX"
schema="SCHEMA">
<id name="id" column="ID">
<generator class="uuid.hex">
</generator>
</id>
<many-to-one name="session"
column="SESSION_ID"/>
</class>
</hibernate-mapping>
now this will work fine for creating sessions
and adding transactions to them (which is what
i want to do most of the time)
Code:
Session source = new Session();
source.getTransactions().add(new Transaction());
source.getTransactions().add(new Transaction());
save(source);
close();
String id = source.getId();
Session target = (Session) load(id);
assertEquals(2, target.getTransactions().size());
like i said up to here everything works fine
and there are no problems. but want i want
to do is something like this.
Code:
Session sess1 = new Session();
Ttansaction tx = new Transaction();
sess1.getTransactions().add(tx);
save(sess1);
close();
String id = tx.getId();
Transaction target = (Transaction) load(id);
Session sess2 = target.getSession();
assertNotNull(sess2);
now this fails. of course as the association
has not be defined to be directional. now
i dont want to make it bidirectional as
transactions should not be able to manage
to which session they belong to *but* i would
like to be able to navigate from transaction
to session *without* writing an explicit
load within the transaction class.
any ideas, any hints. or is this simply not
possible via mappings.
thanks
ciao robertj