如何在grails中扩展Spring Security用户类?
我在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,GORM 对具有继承的域类使用每层次结构表模型。父类中的所有字段和每个子类中的所有字段都将存储在一个表中。
如果要关闭此功能,可以使用 tablePerHierarchy 映射参数。将此参数设置为 false 会将父类字段放在一个公共父表中,并将每个子类的字段放在自己的表中。这可能会使您的查询效率稍低,因为查询将具有联接,但如果继承树很小,则差异应该可以忽略不计。下面是您的域类的样子:
请参阅 grails 文档以获取更多信息:
如果你想要每个子类拥有自己的表,其中包含父类的所有字段和子类的所有字段,那么您可以将父类定义为“抽象”,这应该可以防止 grails为它制作一个单独的表。 Grails 应该只为具体域类创建表,而不是抽象域类。 [来源]
您的用户类将如下所示
:正确构建表,尽管我不确定它可能对 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:
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:
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.
听起来您正在尝试显示对象的属性。也许您只想为您的 Physician 类重写 toString()
或类似的内容,具体取决于您希望如何格式化输出。
It sounds like you're trying to display the attributes of an object. Perhaps you just want to override
toString()
for yourPhysician
class:or something like that, depending on how you want the output to be formatted.