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 W开发者_JS百科ayPoint-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?
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.
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().
精彩评论