3

我是hibernate和java的新手,我正在尝试在playframework中定义一个简单的用户->组->权限模型,我希望在数据库级别强制执行删除级联规则,我不想让orm关心级联(类似于 python-sqlalchemy 被动删除),这是我的模型:

用户型号:

@Entity
@Table(name="auth_user")

public class User extends Model {

    @Column(name="username",nullable=false,length=40)
    @org.hibernate.annotations.Index(name = "idx_username")
    public String username;
    @Column(nullable=false,length=255)
    public String password;
    @Column(nullable=true,length=150)
    public String email;
    @Column(nullable=true,length=150)
    public String fullname;
    @Column(nullable=true)
    public boolean isAdmin;

    @OneToMany(mappedBy="group")
    public List<UserGroup> user_groups;
}

团体型号:

@Entity
@Table(name="auth_group")

public class Group extends Model {

    @Column(nullable=false,length=40)
    public String name;

    @OneToMany(mappedBy="group")
    public List<GroupPermission> group_permissions;

    @OneToMany(mappedBy="user")
    public List<UserGroup> user_groups;

权限模型:

@Entity
@Table(name="auth_permission")

public class Permission extends Model {

    @Column(nullable=false,length=40)
    public String name;
    @Column(nullable=false)
    public Integer code;

    @OneToMany(mappedBy="permission")
    public List<GroupPermission> permission_groups;

GroupPermission 模型:

@Entity
@Table(name="group_permissions")

public class GroupPermission extends Model {

    @ManyToOne
    @org.hibernate.annotations.OnDelete(action=org.hibernate.annotations.OnDeleteAction.CASCADE)
    public Group group;

    @ManyToOne
    @org.hibernate.annotations.OnDelete(action=org.hibernate.annotations.OnDeleteAction.CASCADE)
    public  Permission permission;

用户组模型:

@Entity
@Table(name="user_groups")
public class UserGroup extends Model {

    @ManyToOne
    @org.hibernate.annotations.OnDelete(action=org.hibernate.annotations.OnDeleteAction.CASCADE)
    public User user;

    @ManyToOne
    @org.hibernate.annotations.OnDelete(action=org.hibernate.annotations.OnDeleteAction.CASCADE)
    public Group group;

这会在 postgres 中生成以下 sql:

CREATE TABLE auth_user
(
id bigint NOT NULL,
email character varying(150),
fullname character varying(150),
isadmin boolean,
"password" character varying(255) NOT NULL,
username character varying(40) NOT NULL,
CONSTRAINT auth_user_pkey PRIMARY KEY (id)
)
WITH (
    OIDS=FALSE
);

ALTER TABLE auth_user OWNER TO postgres;
CREATE TABLE auth_group
(
id bigint NOT NULL,
"name" character varying(40) NOT NULL,
CONSTRAINT auth_group_pkey PRIMARY KEY (id)
)
WITH (
    OIDS=FALSE
);

ALTER TABLE auth_group OWNER TO postgres;

CREATE TABLE auth_permission
(id bigint NOT NULL,
 code integer NOT NULL,
 "name" character varying(40) NOT NULL,
 CONSTRAINT auth_permission_pkey PRIMARY KEY (id)
)
WITH (
    OIDS=FALSE
 );

ALTER TABLE auth_permission OWNER TO postgres;

CREATE TABLE group_permissions
(id bigint NOT NULL,
group_id bigint,
permission_id bigint,
CONSTRAINT group_permissions_pkey PRIMARY KEY (id),
CONSTRAINT fk8f9f82c43494357e FOREIGN KEY (permission_id)
    REFERENCES auth_permission (id) MATCH SIMPLE
    ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk8f9f82c4c8b3dcb6 FOREIGN KEY (group_id)
    REFERENCES auth_group (id) MATCH SIMPLE
    ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);

ALTER TABLE group_permissions OWNER TO postgres;

CREATE TABLE user_groups
(id bigint NOT NULL,
 group_id bigint,
 user_id bigint,
 CONSTRAINT user_groups_pkey PRIMARY KEY (id),
 CONSTRAINT fke27720c847140efe FOREIGN KEY (user_id)
    REFERENCES auth_user (id) MATCH SIMPLE
    ON UPDATE NO ACTION ON DELETE CASCADE,
 CONSTRAINT fke27720c86e0797ca FOREIGN KEY (group_id)
    REFERENCES auth_user (id) MATCH SIMPLE
    ON UPDATE NO ACTION ON DELETE NO ACTION,
 CONSTRAINT fke27720c8a1c053ea FOREIGN KEY (user_id)
    REFERENCES auth_group (id) MATCH SIMPLE
    ON UPDATE NO ACTION ON DELETE NO ACTION,
 CONSTRAINT fke27720c8c8b3dcb6 FOREIGN KEY (group_id)
    REFERENCES auth_group (id) MATCH SIMPLE
    ON UPDATE NO ACTION ON DELETE CASCADE
 )

 WITH (
    OIDS=FALSE
 );

 ALTER TABLE user_groups OWNER TO postgres; 

如您所见,未创建 tabe auth_user 中的用户名字段上的索引,表 user_groups 中的约束是重复的,我有 ON DELETE CASCADE 和 ON DELETE NO ACTION,我只想要表 group_permissions 中的级联版本未创建级联规则,

怎么了?

谢谢尼古拉

4

2 回答 2

0

如您所见,未创建 tabe auth_user 中的用户名字段上的索引(...)

的确。它显然应该被创建,注释部分看起来是正确的。作为替代方案,您能否尝试在 Hibernate 特定@Table注释中定义索引。像这样:

@Entity
@org.hibernate.annotations.Table(
    name="auth_user",
    indexes = { @Index(name="idx_username", columnNames = { "username" } ) }
    )
public class User { ... }

如果这可行,这只是一种解决方法,@org.hibernate.annotations.Index属性级别的注释应该可以工作。

表 user_groups 中的约束是重复的,我有 ON DELETE CASCADE 和 ON DELETE NO ACTION,我只想要级联版本,在表 group_permissions 中没有创建级联规则

不能说,不能测试,对相关注解没有太多经验。也许在 Hibernate 的 Jira 中搜索现有问题。如果找不到任何东西,请设置测试用例并创建问题。抱歉,我没有更好的建议。

于 2010-09-12T09:49:12.763 回答
0

不确定,但它让我想起了我曾经遇到的一个问题。

我在第一次运行时使用“jpa.ddl=create”创建了索引(并立即切换回更新)。

在您的 application.conf 文件中:

# JPA Configuration (Hibernate)
# ~~~~~
#
# Specify the custom JPA dialect to use here (default to guess):
# jpa.dialect=org.hibernate.dialect.PostgreSQLDialect
#
# Specify the ddl generation pattern to use (default to update, set to none to disable it):
# jpa.ddl=update

# can you try this?
jpa.ddl=create

你可以试试这个配置,看看索引是否被创建?

只是我的2美分...

于 2010-09-23T19:20:51.767 回答