开发者

How to extend Spring Security User Class in grails?

开发者 https://www.devze.com 2023-03-16 22:33 出处:网络
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

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.Hos开发者_运维百科pitalManagement.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;


}


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:

  • Inheritance Strategies
  • Inheritance in GORM

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消