Student表与Score表是多对多的关系,这两张表有个中间表,因中间表没有主键,故不能直接查中间表。我现在需要查询Student中ScoreId>10的数据。
var hql="from Student s left join fetch s.Scores score where score.ScoreId>10";
这样写查询时报错。
Student实体
[Class(Table = "Student", OptimisticLock = OptimisticLockMode.Version, DynamicUpdate = true)] [Cache(Usage = CacheUsage.ReadWrite)] [Serializable] public class Student { [Id(0, TypeType = typeof(int), Name = "Id", UnsavedValue = "0")] [Column(1, Name = "sid", NotNull = true, SqlType = "number")] [Generator(2, Class = "sequence")] [Param(3, Name = "sequence", Content = "s_student")] public virtual int Id{get;set;} [Property(Column = "sname")] public virtual string SName{get;set;} [Bag(0, Name = "Scores", Table = "Score")] [Key(1, Column = "sid", NotNull = true)] [ManyToMany(2,Column = "scoreId",ClassType = typeof(Score),Fetch=FetchMode.Join,Lazy=RestrictedLaziness.Proxy)] public virtual IList<Score> Scores { get; set; } }
Score实体
[Class(Table = "Score", OptimisticLock = OptimisticLockMode.Version, DynamicUpdate = true)] [Cache(Usage = CacheUsage.ReadWrite)] [Serializable] public class Score { [Id(0, TypeType = typeof(int), Name = "Id", UnsavedValue = "0")] [Column(1, Name = "scoreId", NotNull = true, SqlType = "number")] [Generator(2, Class = "sequence")] [Param(3, Name = "sequence", Content = "s_score")] public virtual int Id{get;set;} [Property(Column = "scorename")] public virtual string ScoreName{get;set;} }