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.  [ 4 posts ] 
Author Message
 Post subject: A multiple, one-to-many mapping..(one-to-many-properties?)
PostPosted: Mon Aug 25, 2008 10:22 am 
Newbie

Joined: Mon Aug 25, 2008 10:01 am
Posts: 2
Hibernate version: 2.0.0GA

Mapping documents:

Line Mapping:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Example.Domain" assembly="Example">
<class name="Line" table="LINES">
<id name="Id" column="ID">
<generator class="native" />
</id>
<many-to-one name="Start" class="EndPoint" column="START" cascade="save-update" not-found="ignore" />
<many-to-one name="End" class="EndPoint" column="END" cascade="save-update" not-found="ignore" />
</class>
</hibernate-mapping>

EndPoint Mapping:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Example.Domain" assembly="Example">
<class name="EndPoint" table="ENDPOINTS">
<id name="Id" column="ID" >
<generator class="native" />
</id>
<bag name="Lines" inverse="true">
<!-- THIS IS WHERE I'M STUCK
it contains ALL lines that use this EndPoint
as either a Start or an End !>
</bag>
</class>
</hibernate-mapping>

Name and version of the database you are using: SQLite

What I'm looking for (and can't find in the docs or online) is how to map a collection on the EndPoint class to ALL lines that reference it. No matter if the reference is as the Line.Start or Line.End. So essentially I need a many-to-one-properties type association. Is this possible in NHibernate

Now, I'm fairly confident that if I wanted to change my existing code to meet the needs of my ORM I could change the EndPoint to have two separate lists, one for lines that use the EndPoint in Start and one for the Lines that use the EndPoint for an End. But, I have quite a bit of code to change and, while it's better than having dependencies in my code, I'd rather not start changing code to meet the needs of the ORM?

Any hints/insights on how to bend NHibernate to my will are greatly appreciated! Thanks in advance!!

edit: removed the link at the top to the 'read first' link


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 26, 2008 8:20 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can use custom sql for loading the collection. Have a look at http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/querysql.html#querysql-load.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2008 3:41 pm 
Newbie

Joined: Mon Aug 25, 2008 10:01 am
Posts: 2
While it appears that this is exactly what the doctor ordered, the documentation for the whole process is rather hazy. I never did get it working but it's not a problem now as I realized that I didn't need to populate that particular field from the database, it was being generated during run-time.

For future reference, how does the parameter part of it work? I know how to use saved queries, both HQL and SQL based with parameters and the whole deal but in the 'loader' element has no way that I can see to specify any parameters? So in my case I wanted to be able to say "FROM Lines line where line.End=:ep OR line.Start=:ep" where ep is the parent EndPoint. In the examples one has a mystery ":id" and the other has a mystery "?". Both appear to be used in an ID context but it's never said in the writing. I read through the docs on the custom sql in general and it seems like the parameter thing just 'automagically' works all the time?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 01, 2008 3:09 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I'm not absolutely but if I remember it correctly there is only on parameter and that's the id. You can either use :id or ? I think. And I think the custom SQL is only used when you Get/Load the object.

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.