如何在grails中扩展Spring Security用户类?

发布于 2024-11-17 15:49:43 字数 783 浏览 0 评论 0原文

我在我的 grails 应用程序中使用 Spring Security 插件。我在自己生成的 Physician 类中扩展了 Springs User 类。现在,当我运行应用程序时,我没有在数据库中获取医师表,而是用户类仅具有医师域中定义的所有属性。我需要为医生准备单独的桌子。

当我尝试使用 User.findAll() 查找用户表中的所有用户时,我的输出是,

[com.HospitalManagement.User:1、com.HospitalManagement.User:2、com.HospitalManagement.User:3、com.HospitalManagement.User:4、com.HospitalManagement.User:5、com.HospitalManagement.User: 6、com.HospitalManagement.User:7、 com.HospitalManagement.User:8,com.HospitalManagement.User:9]

但我期待用户名和其他医生属性值。

问题可能是什么?

域类是:

package com.HospitalManagement

class Physician extends User{

    static constraints = {
    }

    String specilty;
    String MobileNo;
    String Physician_Address;
    String clinicals;


}

I am using Spring Security plugin in my grails app. I have extended Springs User class in my own generated Physician class. Now when I run app I am not getting physician table in database instead User class only has all properties defined in Physician Domain. I need to have separate table for Physician.

When I try to find all Users in User table with User.findAll() my output is,

[com.HospitalManagement.User : 1, com.HospitalManagement.User : 2, com.HospitalManagement.User : 3, com.HospitalManagement.User : 4, com.HospitalManagement.User : 5, com.HospitalManagement.User : 6, com.HospitalManagement.User : 7, com.HospitalManagement.User : 8, com.HospitalManagement.User : 9]

but I was expecting username and other physician properties values.

What could the problem be?

Domain Class is:

package com.HospitalManagement

class Physician extends User{

    static constraints = {
    }

    String specilty;
    String MobileNo;
    String Physician_Address;
    String clinicals;


}

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

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

发布评论

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

评论(2

一场春暖 2024-11-24 15:49:43

默认情况下,GORM 对具有继承的域类使用每层次结构表模型。父类中的所有字段和每个子类中的所有字段都将存储在一个表中。

如果要关闭此功能,可以使用 tablePerHierarchy 映射参数。将此参数设置为 false 会将父类字段放在一个公共父表中,并将每个子类的字段放在自己的表中。这可能会使您的查询效率稍低,因为查询将具有联接,但如果继承树很小,则差异应该可以忽略不计。下面是您的域类的样子:

package com.HospitalManagement

class Physician extends User {

    static constraints = {
    }

    String specilty;
    String MobileNo;
    String Physician_Address;
    String clinicals;


    static mapping = {
        tablePerHierarchy false
    }
}

请参阅 grails 文档以获取更多信息:

如果你想要每个子类拥有自己的表,其中包含父类的所有字段和子类的所有字段,那么您可以将父类定义为“抽象”,这应该可以防止 grails为它制作一个单独的表。 Grails 应该只为具体域类创建表,而不是抽象域类。 [来源]

您的用户类将如下所示

abstract class User {
    String username
    String password
    //etc...
}

:正确构建表,尽管我不确定它可能对 Spring Security 产生什么影响。如果您在将 User 类抽象化后看到任何 Spring Security 错误,我会回退到禁用每个层次结构表并处理连接。

By default, GORM uses a table-per-hierarchy model for domain classes with inheritance. All fields in the parent class and all fields in each subclass will be stored in a single table.

If you want to turn off this functionality, you can use the tablePerHierarchy mapping parameter. Setting this parameter to false will put the parent class fields in a common parent table, and put the fields for each subclass in their own table. This can make your queries slightly less efficient because the queries will will have joins, but if the inheritance tree is small, the difference should be negligible. Here's what it would look like with your domain class:

package com.HospitalManagement

class Physician extends User {

    static constraints = {
    }

    String specilty;
    String MobileNo;
    String Physician_Address;
    String clinicals;


    static mapping = {
        tablePerHierarchy false
    }
}

See The grails documentation for more information:

If you want each subclass to have it's own table which contains all fields from the parent class and all fields from the subclass, then you can define the parent class as 'abstract' and that should prevent grails from making a separate table for it. Grails should only create tables for concrete domain classes, not abstract domain classes. [Source]

Your user class would look then look something like this:

abstract class User {
    String username
    String password
    //etc...
}

This will build the tables correctly, though I'm not sure what effect it might have on Spring Security. If you see any Spring Security errors after making the User class abstract, I'd fall back to disabling table-per-hierarchy and dealing with the joins.

可可 2024-11-24 15:49:43

听起来您正在尝试显示对象的属性。也许您只想为您的 Physician 类重写 toString()

package com.HospitalManagement

    class Physician extends User{

        static constraints = {
        }

        String specilty;
        String MobileNo;
        String Physician_Address;
        String clinicals;

        String toString() {
            "specilty: $specilty, MobileNo: $MobileNo, Physician_Address: $Physician_Address, clinicals: $clinicals"
        }
    }

或类似的内容,具体取决于您希望如何格式化输出。

It sounds like you're trying to display the attributes of an object. Perhaps you just want to override toString() for your Physician class:

package com.HospitalManagement

    class Physician extends User{

        static constraints = {
        }

        String specilty;
        String MobileNo;
        String Physician_Address;
        String clinicals;

        String toString() {
            "specilty: $specilty, MobileNo: $MobileNo, Physician_Address: $Physician_Address, clinicals: $clinicals"
        }
    }

or something like that, depending on how you want the output to be formatted.

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