Java 基本实体自定义列

Java 基本实体自定义列,java,spring,jpa,entity,base,Java,Spring,Jpa,Entity,Base,我有像这样的基本实体 @MappedSuperclass public class BaseEntityCore implements Serializable { @CreatedBy @Column(name = "olusturan", /* nullable = false, */ length = 50, updatable = false) private String createdBy;

我有像这样的基本实体

    @MappedSuperclass
    public class BaseEntityCore implements Serializable {
    
        @CreatedBy
        @Column(name = "olusturan", /* nullable = false, */ length = 50, updatable = false)
        private String createdBy;
    
        @CreatedDate
        //@NotNull
        @Column(name = "olusturma_tarihi", nullable = false, updatable = false)
        private LocalDateTime createdDate ;
    
        @LastModifiedBy
        @Column(name = "guncelleyen", length = 50)
        private String lastModifiedBy;
    
        @LastModifiedDate
        @Column(name = "guncelleme_tarihi")
        private LocalDateTime lastModifiedDate;
    
        @Column(name = "aktif")
        private int aktif;

// getter and setter
@Entity
@Table(name = "foo")
@EntityListeners(value = { AbstractEntityListenerCore.class })
public class foo extends BaseEntityCore {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name="foo_name")
private String fooName;
//getter and setter
}
一个实体扩展了这个基本实体,就像

    @MappedSuperclass
    public class BaseEntityCore implements Serializable {
    
        @CreatedBy
        @Column(name = "olusturan", /* nullable = false, */ length = 50, updatable = false)
        private String createdBy;
    
        @CreatedDate
        //@NotNull
        @Column(name = "olusturma_tarihi", nullable = false, updatable = false)
        private LocalDateTime createdDate ;
    
        @LastModifiedBy
        @Column(name = "guncelleyen", length = 50)
        private String lastModifiedBy;
    
        @LastModifiedDate
        @Column(name = "guncelleme_tarihi")
        private LocalDateTime lastModifiedDate;
    
        @Column(name = "aktif")
        private int aktif;

// getter and setter
@Entity
@Table(name = "foo")
@EntityListeners(value = { AbstractEntityListenerCore.class })
public class foo extends BaseEntityCore {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name="foo_name")
private String fooName;
//getter and setter
}
用spring,spring-jpa。我也有实体回购,比如

public interface FooRepository extends JpaRepository<Foo, Long> {
Optional<Foo> findByFooName(String name);
}

我不明白为什么总是抛出异常。

您不需要if-else子句。 只需始终搜索“Aktif”==1的实体

因此,使用其他方法扩展repo类

Optional<Foo> findByFooNameAndAktif(String name, int aktif);   
可选的findbyFoonameandakif(字符串名,int-aktif);
并且只搜索你想要的“aktif”


但你的问题是关于“Aktif”的两个属性,对吗?

实际上我可以更改回购方法并添加andAktif,但问题是两个相同的属性。另外,getAktif()返回0而不是1。为什么?我怎样才能选择正确的呢?