Skip to content

Mapping to Objects

ZorT edited this page Apr 18, 2023 · 11 revisions

That's right! You can save and load objects from database using queries and map them automatically to Java objects. This section will show you how to do that.

Create model

public class User {
    @PrimaryKey
    private String firstname;
    private String lastname;

    public User(String firstname, String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }
}

⚠️ The model needs to have at least one of:

  • No args constructor (Recommended!)
  • All args constructor

Create table

connection.buildEntitySchema("users", User.class);

Migrations are planned feature

Make operations

public void saveUser() {
    User user = new User("John", "Doe");
    QueryResult result = connection.save("users", user);
}

public void loadUser() {
    User user = connection.select()
            .from("users")
            .where().isEqual("firstname", "John")
            .obtainOne(User.class)
            .orElse(null);
}

Let's learn more

Now, we present to you proxy gates! Please head to Proxy Gates to learn how to create repositories for performing queries.

Clone this wiki locally