我有类似的基本实体
@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
}
与春天,春天jpa。我也有类似的实体回购
public interface FooRepository extends JpaRepository<Foo, Long> {
Optional<Foo> findByFooName(String name);
}
现在我可以用foo.setAktif(1(保存实体。保存foo后,我在表上看到的aktif是1。之后,我运行findByFooName方法。这将翻转对象,但此对象具有2个aktif属性。第一个是aktif,值为1,另一个是BaseEntityCore.aktif和值为0。我核对一下类似的if条款
if(foo.getAktif()==1){
//do something
}
else {
//throws exception;
}
我不明白为什么总是抛出异常。
您不需要if-else子句。只需始终搜索带有"的实体;Aktif"==1.
因此,使用其他方法扩展您的回购类
Optional<Foo> findByFooNameAndAktif(String name, int aktif);
并且仅搜索";aktif";你想要的。
但是你的问题是关于";Aktif";正确的