views:

31

answers:

2

I'm trying to map a List with an index column. This works fine, but i would also like to be able to query the index column from HQL. When i do that HQL throws an exception:

NHibernate.QueryException: could not resolve property: Position of: component[Location,Time]

To be able to query the WayPoint.Position column from HQL, i have created a Position-property in the WayPoint-class and i would like to map this property to the Position-column.

I have tried with:

wp.Map(x => x.Position).Column("Position");

But this results in a MappingException: NHibernate.MappingException: Repeated column in mapping for collection: Route.WayPoints column: Position

public class RouteMap : ClassMap<Route>
{
    private const string LocationKey = "LocationIndex";
    public RouteMap()
    {
        Not.LazyLoad();
        ReadOnly();
        Id(x => x.Id).GeneratedBy.Assigned();
        Version(x => x.Version);
        Map(x => x.Time);
        References(x => x.StartingPoint).UniqueKey(LocationKey);
        References(x => x.Destination).UniqueKey(LocationKey);
        HasMany(x => x.WayPoints).Component( wp =>
                {
                    wp.Map(x => x.Time);
                    //wp.Map(x => x.Position).Column("Position");
                    wp.Component( wp2 => wp2.Location, gl =>
                                     {

                                         gl.Map(x => x.Latitude);
                                         gl.Map(x => x.Longitude);
                                     }
                        );
                }

            ).AsList(index => index.Column("Position")).Cascade.All();
    }
}


create table `Route` (
    Id VARCHAR(40) not null,
   Version INTEGER not null,
   Time BIGINT,
   StartingPoint_id VARCHAR(40),
   Destination_id VARCHAR(40),
   primary key (Id),
  unique (StartingPoint_id, Destination_id)
)

create table WayPoints (
    Route_id VARCHAR(40) not null,
   Latitude DOUBLE,
   Longitude DOUBLE,
   Time BIGINT,
   Position INTEGER not null,
   primary key (Route_id, Position)
)

Is it possible to map the Position property? Or is there another approach to make HQL aware of the Position-index-field? Thanks for the help!


+2  A: 

I don't think you need to map the Position property at all; if you need to query on the index of a collection, you can do so as follows:

select item, index(item) from Order order 
    join order.Items item
where index(item) < 5

More information can be found in the HQL Documentation.

DanP
Thanks, exactly what i was looking for.
Søren Randrup
A: 

Try adding .ReadOnly() to your wp.Map(x => x.Position).Column("Position"); That has allowed me to map multiple properties to the same column before, at least when one of them was a References().

Tahbaza
Readonly() doesn't do it for index columns. There is still a clash. Thanks for the answer.
Søren Randrup