One Java Model from the App to PostgreSQL
The annoying part of full stack development is finding the same rule in three places. The app checks a field, the server checks it slightly differently, and the database model has another idea about what it should contain. What is Codename One? Codename One is an open-source framework for building native iOS, Android, desktop, and web apps from a single Java or Kotlin codebase. Learn more at…
The Codename One framework enables developers to create native iOS, Android, desktop, and web applications from a single Java or Kotlin codebase. This post demonstrates building a reminder service that follows a single model from Java code into the database. The backend is experimental but already includes generated routing, pooled connections, an ORM, and transactions.
To get started, use a Codename One Initializr project or add the backend module to an existing project. Run the necessary commands from the project root. The backend is activated using the -Dcodename1.platform=backend flag to avoid entering an ordinary client build.
Create the Reminder.java model class with two fields: id (long) and title (String). Apply the com.codename1.annotations.Column, Entity, and Id annotations to specify the table name and field properties. Next, create the ReminderRules.java class that contains a static method for validating and formatting the title field according to Codename One's rules.
Finally, implement a controller (ReminderController.java) that communicates with the ORM to perform CRUD operations on the Reminder entity. Use @RestController, @RequestMapping, @GetMapping, and @PostMapping annotations to describe the controller and its routes. Inject the EntityManager into the controller's constructor and use DAO methods to interact with the database. This API follows familiar Spring conventions, making it easy for developers to understand and work with.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.