Spring Data JPA filter子类中的数据,带有自己的查询

时间:2016-12-04 19:18:18

标签: java spring spring-data

我正在开发一个小型JAVA项目,以改善我的密码工作。为此,我使用H2数据库将数据和Spring数据JPA存储为OR-Mapper。所有DML操作都可以正常工作,但我不想让用户从数据库中删除密码。为此,我在Bean中添加了一个名为DML_ART的字段。 DML_ART是一个字符串字段,它是行的状态。例如,如果用户在删除时单击JSF GUI,则该标志将设置为“DELETE”。

在数据加载时,此行将被忽略。 SQL Query适用于DBPasswordEntity,但子类也必须按相同的条件进行筛选。

但是书面陈述并不是真的。我不知道为什么。

DBPasswordEntity.java

pypy

DBExtendEntity.java

@Entity
@Table(name = "PasswordEntity",schema = "pwdmanager")
@PrimaryKeyJoinColumn(name = "password_id", referencedColumnName = "id")
@Getter
@Setter
public class DBPasswordEntity extends DBMetaDataEntity implements Serializable {

    @Basic
    @Column(name = "PASSWORD", nullable = false, length = 4000)
    private String password;

    @OneToMany(fetch = FetchType.EAGER,cascade = ALL, mappedBy = "password")
    private Set<DBExtendEntity> extend;

    @Transient
    public void addExtend(DBExtendEntity ex)
    {
        if(extend == null)
        {
            extend = new LinkedHashSet<>();
        }
        extend.add(ex);
    }

   }

DBMetaDataEntity

@Entity
@Table(name = "ExtendEntity",schema = "pwdmanager")
@PrimaryKeyJoinColumn(name = "extend_id", referencedColumnName = "id")
@Getter
@Setter
public class DBExtendEntity extends DBMetaDataEntity implements Serializable{

    @Basic
    @Column(name = "NAME", nullable = false, length = 255)
    private String name;

    @ManyToOne(optional = false)
    @JoinColumn(name = "password", referencedColumnName = "Password_ID")
    private DBPasswordEntity password;  
}

SQL-过滤 - 操作

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "pwd_type")
@Table(name = "MetaDataEntity",schema = "pwdmanager")
@Data
public abstract class DBMetaDataEntity {

    @Id
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Integer id;

    @Basic
    @Column(name = "DML_ART", nullable = false)
    private String dml_art ="INSERT";
}

1 个答案:

答案 0 :(得分:0)

我通过切换到Hibernate来解决它。

我的更新POM

  <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>jboss-logging</artifactId>
                    <groupId>org.jboss.logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>${hibernate-validator.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>${jta.version}</version>
        </dependency>

在子类中过滤

 FROM DBPasswordEntity pwd WHERE pwd.mdae_dml_art <> 'DELETE' AND pwd.usre_user.usre_username = 'ABC' AND pwd.id = (SELECT MAX(id) FROM DBPasswordEntity pwd2 WHERE pwd2.mdae_rev_ID = pwd.mdae_rev_ID)"

执行查询:

this.session.createSQLQuery(sql).executeUpdate();

包含Hibernate所有配置的完整示例: https://github.com/dominikLudwig/Passwordmanager