【问题标题】:How to map existing JPA entities to PicketLink如何将现有 JPA 实体映射到 PicketLink
【发布时间】:2015-07-09 00:36:11
【问题描述】:

我正在尝试将 Seam 2 应用程序迁移到 CDI 并使用 PicketLink 来确保安全。经过所有的阅读和研究,似乎所有示例在 PicketLink 模型和后端实体之间都有一对一映射。例如帐户到 AccountEntity,分区到 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 中表示单个身份模型的两个实体(加上一个超类),例如立体类型用户。

基于这个why 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) 时,我无法让它填充所有实体。这甚至可能吗?

【问题讨论】:

    标签: jpa picketlink


    【解决方案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 和类似的方法来同步它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 2021-07-23
      • 1970-01-01
      • 2020-04-18
      • 2012-07-04
      • 2013-08-18
      • 2020-08-15
      相关资源
      最近更新 更多