1

我正在尝试将 Seam 2 应用程序迁移到 CDI 并使用 PicketLink 来确保安全。经过所有的阅读和研究,似乎所有示例都在 PicketLink 模型和后端实体之间进行了一对一的映射。例如,Account 到 AccountEntity,Partition 到 PartitionEntity。由于我已经有了代表身份模型的实体,所以我一直试图将它们映射到 PicketLink。这是我所拥有的:

@MappedSuperClass
public class ModelEntityBase implement Serializable {
    @Id @Generated
    Long id;
    Date creationDate;
}

@Entity
public Account extends ModelEntityBase {
    String username;
    String passwordHash;
    @OneToOne(mappedBy = "account")
    Person person;
}

@Entity
public Person extends ModelEntityBase {
    String name;
    String email;
    @OneToOne
    @JoinColumn(name = "account_id")
    Account account;
}

在PicketLink 中表示单个身份模型的两个实体(加上一个超类),例如立体类型User。

基于这个为什么 IdentityType id is String not Long,我尝试在以下位置添加一个新实体:

@Entity
@IdentityManaged(BaseIdentityType.class);
public class IdentityTypeEntity implement Serializble {
    @Id @Identifier
    private String id;

    @OneToOne(optional = false, mappedBy = "identityType")
    @OwnerReference
    private Account account;

    @IdentityClass
    private String typeName;

    @ManyToOne @OwnerReference
    private PartitionEntity partition;
}

我尝试了几种不同的注释和模型类方法。但是当使用 IdentityManager.add(myUserModel) 时,我无法让它填充所有实体。这甚至可能吗?

4

1 回答 1

1

从 Pedro (PicketLink Dev) 获得帮助。在此处发布答案以帮助他人。这是我最终使用的模型类。

@IdentityStereotype(USER)
public class User extends AbstractAttributedType implements Account {
    @AttributeProperty
    private Account accountEntity;
    @AttributeProperty
    @StereotypeProperty(IDENTITY_USER_NAME)
    @Unique
    private String username;
    @AttributeProperty
    private boolean enabled;
    @AttributeProperty
    private Date createdDate;
    @AttributeProperty
    private Date expiryDate;
    @AttributeProperty
    private Partition partition;
    // getter and setter omitted
}

并创建了一个新实体来映射到这个模型:

public class IdentityTypeEntity implements Serializable {
    @Id
    @Identifier
    private String id;

    @OneToOne(optional = false, mappedBy = "identityType",
        cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @AttributeValue
    // @NotNull
    private HAccount accountEntity;

    @IdentityClass
    private String typeName;

    @ManyToOne
    @OwnerReference
    private PartitionEntity partition;

    @AttributeValue
    private String username;

    @AttributeValue
    // @Transient
    private boolean enabled;

    @AttributeValue
    private Date createdDate;

    @AttributeValue
    private Date expiryDate;
}

PL 可以将带有@AttributeProperty 的属性映射到带有@AttributeValue 的实体属性。但它只能映射到一个实体。因此,无法将 User 及其属性映射到 Account 和 Person。但是您可以在模型中拥有实体(在我的情况下为 accountEntity)。我还必须在新的 IdentityTypeEntity 和我现有的 Account 实体(用户名、eanbled、createdDate)中复制一些字段,因为 PL 需要这些字段。使用 @PrePersist 和类似的方法来同步它们。

于 2015-07-13T02:26:52.043 回答