2010-08-27 112 views
1

我试图用索引列映射列表。这工作正常,但我也想能够从HQL查询索引列。当我这样做,HQL抛出异常:将属性映射到索引列

NHibernate.QueryException:无法解析属性:人的职位:成分[地点,时间]

为了能够从查询WayPoint.Position列HQL,我在WayPoint-class中创建了一个Position-property,我想将这个属性映射到Position-column。

我曾尝试用:

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

但这导致MappingException:

NHibernate.MappingException:重复列映射集合:Route.WayPoints柱:位置

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) 
) 

是否可以映射Position属性?还是有另一种方法让HQL知道位置索引字段?


回答

2

我不认为你需要映射位置属性的所有;如果你需要在一个集合的索引查询,你可以这样做如下:

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

更多信息可以在HQL Documentation找到。

+0

谢谢,正是我一直在寻找。 – 2010-08-30 09:44:36

0

尝试将.ReadOnly()添加到您的wp.Map(x => x.Position).Column("Position");这使我可以将多个属性映射到同一列之前,至少当其中一个是References()时。

+0

Readonly()不会为索引列执行此操作。仍然有冲突。 感谢您的回答。 – 2010-08-30 09:45:30

相关问题