I found myself in the need to override the static method, simply because it makes the most sense, but I also know that this is not possible.
Superclass, Entity.java:
abstract public class Entity<T> { public Entity() { //set up database connection } abstract public static Map<Object, T> getAll(); abstract public void insert(); abstract public void update(); protected void getData(final String query) { //get data via database } protected void executeQuery(final String query) { //execute sql query on database } }
One of many specific implementations, Account.java:
public class Account extends Entity<Account> { private final static String ALL_QUERY = "SELECT * FROM accounts"; private final static String INSERT_QUERY = "INSERT INTO accounts (username, password) VALUES(?, ?)"; private final static String UPDATE_QUERY = "UPDATE accounts SET password=? WHERE username=?"; private String username; private String password; public Account(final String username, final String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public void setUsername(final String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(final String password) { this.password = password; } @Override public static Map<Object, Account> getAll() {
I did not delve into the explanation of the code, but any general feedback on this would also be appreciated, I hope the comments explain enough.
Basically, I think we can all agree that using Account.getAll() makes more sense with respect to new Account().getAll() (if I were to introduce dummy syntax for it). However, I want it to extend the Entity class, currently it is intended only for convenience, but later I may have to use Entity sets / lists / multisets and execute the update() action for all of them, for example, if I built some queue that performs all updates every minute.
So good, is there a way to build getAll() correctly?
Sincerely.
skiwi source share