将属性映射到索引列
我正在尝试使用索引列映射列表。这工作正常,但我还希望能够从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您根本不需要映射 Position 属性;如果需要查询集合的索引,可以按如下方式操作:
更多信息可以在 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:
More information can be found in the HQL Documentation.
尝试将
.ReadOnly()
添加到您的wp.Map(x => x.Position).Column("Position");
这使我可以映射多个属性之前的同一列,至少当其中一个是 References() 时。Try adding
.ReadOnly()
to yourwp.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().