Do I need getters and seters and additional object constructors?

I am trying to use ORMLite to save a database in an Android project. This is clearly seen from the examples. As soon as I start using it, I find that I do not quite understand its requirements and behavior.

Let's say I have a class called Bone that I would like to keep.

@DatabaseTable(tableName = "bones") public class Bone{ // user defined @DatabaseField(dataType = DataType.STRING) private String color; @DatabaseField private double length; @DatabaseField private int quantity; // db assigned @DatabaseField(generatedId = true) private int id; public Bone(){ } // constructors public Bone(String color, int quantity) { this(color, quantity, 0); } public Bone(String color, int quantity, int id) { this.color = color; this.quantity = quantity; this.id = id; } public Bone(Bone old, int id){ this.color = old.color; this.length = old.length; this.quantity = old.quantity; this.id = id; } public String getColor() { return color; } public double getLength() { return length; } public int getQuantity() { return quantity; } public void setLength(double length) { this.length = length; } public int getId() { return id; } } 
  • What are the requirements for the names of getters and setters of its fields? Do their names have no meaning? Can I use one without getter and setter?

  • Besides arg constructor, do you need any other constructor?

Please, help.

+4
source share
1 answer

1) What are the requirements for the names of getters and setters of its fields? Do their names have no meaning? Can I use one without getter and setter?

There are no requirements for getters and setters. By default, ORMLite uses reflection to directly create entity fields.

If you set the useGetSet = true field of the useGetSet = true annotation , then you will run the neet get / set methods. See Javadocs for format.

2) Besides the arg constructor, do you need any other constructor?

No. You only need the available no-arg constructor for ORMLite to create an object before the reflection works.

+6
source

Source: https://habr.com/ru/post/1483042/


All Articles