简体   繁体   中英

Base Entity custom column

i have base entity like

    @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

and a entity extends this base entity like

@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
}

with spring , spring jpa. i also have entity repo like

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

now i can save entity with foo.setAktif(1). after saving foo i see on table aktif is 1. After that i run findByFooName method. this turns the object but this object has 2 aktif properties. first is aktif and value is 1 and the other is BaseEntityCore.aktif and value is 0. i check with if clause like

if(foo.getAktif()==1){
//do something
}
else {
//throws exception;
}

i cant get it why always throws exception.

You don't need your if else clause. Just search always for Entities with "Aktif" == 1.

So extend your repo class with an other method

Optional<Foo> findByFooNameAndAktif(String name, int aktif);   

and only search for the "aktif" you want.

But your question is about the 2 properties of "Aktif" right?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM