将属性映射到索引列

发布于 2024-09-16 07:43:31 字数 2024 浏览 5 评论 0原文

我正在尝试使用索引列映射列表。这工作正常,但我还希望能够从 HQL 查询索引列。当我这样做时 HQL 抛出异常:

NHibernate.QueryException:无法解析属性:位置:组件[位置,时间]

为了能够从 HQL 查询 WayPoint.Position 列,我在 WayPoint 类中创建了一个位置属性,并且我想映射将此属性添加到 Position 列。

我尝试过:

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 知道 Position-index-field 吗?


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?


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难理解 2024-09-23 07:43:31

我认为您根本不需要映射 Position 属性;如果需要查询集合的索引,可以按如下方式操作:

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

更多信息可以在 HQL 文档

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.

巴黎盛开的樱花 2024-09-23 07:43:31

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文