当表具有一对多关系时,流畅的 nhibernate 会生成错误的 SQL

发布于 2024-10-31 13:07:33 字数 2491 浏览 3 评论 0原文

我有两张表,一张是 UserDetails,另一张是 UserRoles。我按照以下方式映射它们。通过此插入操作,一切顺利,但当我尝试从用户对象获取角色列表时,抛出异常。我发现,Hibernate 生成的 SQL 在 ware 子句中将 USERCODE 列作为 User_id 。如何克服这个问题。请帮我。

用户类:

public virtual string Code { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string MiddleName { get; set; }
    .......
    public virtual IList<UserRole> Roles { get; set; }
    public virtual IList<UserResource> Resources { get; set; }

用户映射:

 Table("USER_DETAILS");
        Schema("PROJECTDOCS");
        Id(x => x.Code).Column("CODE").GeneratedBy.Assigned();
        Map(x => x.FirstName).Column("FIRSTNAME").Not.Nullable().Length(100);
        Map(x => x.LastName).Column("LASTNAME").Length(100).Not.Nullable();
        Map(x => x.MiddleName).Column("MIDDLENAME").Length(100);
        ...
        HasMany(x => x.Roles).Inverse().Cascade.All();

UserRole 类:

 public virtual long UserRoleID { get; set; }
    public virtual User UserCode { get; set; }
    public virtual long RoleID { get; set; }
    public virtual string CreatedBy { get; set; }
    public virtual DateTime CreatedDate { get; set; }
    public virtual string UpdatedBy { get; set; }
    public virtual DateTime UpdatedDate { get; set; }

UserRole 映射:

Table("USER_ROLES");
        Schema("PROJECTDOCS");
        Id(x => x.UserRoleID).Column("URID").GeneratedBy.Increment();
        Map(x => x.RoleID).Column("ROLEID").Not.Nullable();
        Map(x => x.CreatedBy).Column("CREATEDBY").Not.Nullable().Length(36);
        Map(x => x.CreatedDate).Column("CREATEDDATE").Not.Nullable().Default(DateTime.Now.ToString());
        Map(x => x.UpdatedBy).Column("UPDATEDBY").Not.Nullable().Length(36);
        Map(x => x.UpdatedDate).Column("UPDATEDDATE").Not.Nullable().Default(DateTime.Now.ToString());
        References(x => x.UserCode).Column("USERCODE");           

最终生成的 SQL 为:

[SQL:选择角色0_.User_id为User8_1_、角色0_.URID为URID1_、角色0_.URID为URID2_0_、角色0_.ROLEID为ROLEID2_0_、角色0_.CREATEDBY为CREATEDBY2_0_、角色0_.CREATEDDATE为CREATEDD4_2_0_、角色0_.UPDATEDBY为UPDATEDBY2 _0_, Roles0_.UPDATEDDATE 为 UPDATEDD6_2_0_,roles0_.USERCODE 为 USERCODE2_0_ FROM PROJECTDOCS.USER_ROLES Roles0_ WHERE roles0_.User_id=?]

粗体列名称是磨损的内容。

提前致谢, 普拉迪普

I have two tables, one UserDetails and another UserRoles. I mapped them in the following way. With this insert operation went fine but when i try to get the list of roles from user object, an exception is throwing. What i identified is, the SQL generated by Hibernate has USERCODE column as User_id in whare clause. How to over come this problem. Please help me.

User Class:

public virtual string Code { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string MiddleName { get; set; }
    .......
    public virtual IList<UserRole> Roles { get; set; }
    public virtual IList<UserResource> Resources { get; set; }

User Map:

 Table("USER_DETAILS");
        Schema("PROJECTDOCS");
        Id(x => x.Code).Column("CODE").GeneratedBy.Assigned();
        Map(x => x.FirstName).Column("FIRSTNAME").Not.Nullable().Length(100);
        Map(x => x.LastName).Column("LASTNAME").Length(100).Not.Nullable();
        Map(x => x.MiddleName).Column("MIDDLENAME").Length(100);
        ...
        HasMany(x => x.Roles).Inverse().Cascade.All();

UserRole Class:

 public virtual long UserRoleID { get; set; }
    public virtual User UserCode { get; set; }
    public virtual long RoleID { get; set; }
    public virtual string CreatedBy { get; set; }
    public virtual DateTime CreatedDate { get; set; }
    public virtual string UpdatedBy { get; set; }
    public virtual DateTime UpdatedDate { get; set; }

UserRole Map:

Table("USER_ROLES");
        Schema("PROJECTDOCS");
        Id(x => x.UserRoleID).Column("URID").GeneratedBy.Increment();
        Map(x => x.RoleID).Column("ROLEID").Not.Nullable();
        Map(x => x.CreatedBy).Column("CREATEDBY").Not.Nullable().Length(36);
        Map(x => x.CreatedDate).Column("CREATEDDATE").Not.Nullable().Default(DateTime.Now.ToString());
        Map(x => x.UpdatedBy).Column("UPDATEDBY").Not.Nullable().Length(36);
        Map(x => x.UpdatedDate).Column("UPDATEDDATE").Not.Nullable().Default(DateTime.Now.ToString());
        References(x => x.UserCode).Column("USERCODE");           

The final SQL generated was:

[SQL: SELECT roles0_.User_id as User8_1_, roles0_.URID as URID1_, roles0_.URID as URID2_0_, roles0_.ROLEID as ROLEID2_0_, roles0_.CREATEDBY as CREATEDBY2_0_, roles0_.CREATEDDATE as CREATEDD4_2_0_, roles0_.UPDATEDBY as UPDATEDBY2_0_,
roles0_.UPDATEDDATE as UPDATEDD6_2_0_, roles0_.USERCODE as USERCODE2_0_ FROM PROJECTDOCS.USER_ROLES roles0_ WHERE roles0_.User_id=?]

The bolded column name is what going worng.

Thanks in advance,
Pradeep

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

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

发布评论

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

评论(1

川水往事 2024-11-07 13:07:33

看来您缺少 UserClass 中 HasMany 到 UserRoles 的关键列。你可能需要这样做:

HasMany(x => x.Roles).KeyColumn("URID").Inverse().Cascade.All();

It looks like you are missing the key column on your HasMany to UserRoles in your UserClass. You need to probably do this:

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