Search code examples
quarkusquarkus-panache

Why the class attributes in the quarku panache example are PUBLIC instead of PRIVATE


Referring to the getting started link below https://quarkus.io/guides/hibernate-orm-panache

The example uses a Entity class with public attributes.

class Person{
 public String name;
}

and used as

person.name = "Synd";

so is it simply a lazy example (!! in official doc ? ) or it means something else.


Solution

  • According to the documentation, it might be related to a single difference (extending PanacheEntityBase)

    If you don’t want to bother defining getters/setters for your entities, you can make them extend PanacheEntityBase and Quarkus will generate them for you. You can even extend PanacheEntity and take advantage of the default ID it provides.
    

    Therefore they are making them Public for Quarkus to generate getters/setters for you automatically.

    @Entity
    public class Person extends PanacheEntity {
        public String name;
        public LocalDate birth;
        public Status status;
    
        public static Person findByName(String name){
            return find("name", name).firstResult();
        }
    
        public static List<Person> findAlive(){
            return list("status", Status.Alive);
        }
    
        public static void deleteStefs(){
            delete("name", "Stef");
        }
    }
    

    vs

    @Entity
    public class Person {
        @Id @GeneratedValue private Long id;
        private String name;
        private LocalDate birth;
        private Status status;
    
        public Long getId(){
            return id;
        }
        public void setId(Long id){
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public LocalDate getBirth() {
            return birth;
        }
        public void setBirth(LocalDate birth) {
            this.birth = birth;
        }
        public Status getStatus() {
            return status;
        }
        public void setStatus(Status status) {
            this.status = status;
        }
    }