问题描述
我有类似的基本实体
@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);
}
现在我可以使用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的实体。
所以用其他方法扩展您的repo类
Optional<Foo> findByFooNameAndAktif(String name,int aktif);
,仅搜索所需的“ aktif”。
但是您的问题是关于“ Aktif”的2个属性的吗?