forked from iluwatar/java-design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed checkstyle for App Class, created package-info
- Loading branch information
1 parent
f6908f0
commit bc33aba
Showing
5 changed files
with
132 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,64 @@ | ||
package com.iluwatar.activerecord; | ||
/* | ||
* This project is licensed under the MIT license. | ||
* Module model-view-viewmodel is using ZK framework | ||
* licensed under LGPL (see lgpl-3.0.txt). | ||
* | ||
* The MIT License | ||
* Copyright © 2014-2022 Ilkka Seppälä | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package com.iluwatar.activerecord; | ||
import lombok.extern.slf4j.Slf4j; | ||
import java.sql.SQLException; | ||
import java.util.List; | ||
|
||
public class App { | ||
/** | ||
* The Active Record pattern is an architectural pattern that simplifies | ||
* database interactions by encapsulating database access logic within | ||
* the domain model. This pattern allows objects to be responsible for | ||
* their own persistence, providing methods for CRUD operations directly | ||
* within the model class. | ||
* | ||
* <p>In this example, we demonstrate the Active Record pattern | ||
* by creating, updating, retrieving, and deleting user records in | ||
* a SQLite database. The User class contains methods to perform | ||
* these operations, ensuring that the database interactions are | ||
* straightforward and intuitive. | ||
*/ | ||
|
||
@Slf4j | ||
public final class App { | ||
|
||
private App() { | ||
throw new UnsupportedOperationException("Utility class"); | ||
} | ||
|
||
/** | ||
* Entry Point. | ||
* | ||
* @param args the command line arguments - not used | ||
*/ | ||
|
||
public static void main(String[] args) { | ||
|
||
public static void main(final String[] args) { | ||
try { | ||
// Initialize the database and create the users table if it doesn't exist | ||
User.initializeTable(); | ||
|
@@ -14,12 +67,14 @@ public static void main(String[] args) { | |
// Create a new user and save it to the database | ||
User user1 = new User(null, "John Doe", "[email protected]"); | ||
user1.save(); | ||
System.out.println("New user saved: " + user1.getName() + " with ID " + user1.getId()); | ||
System.out.println("New user saved: " + user1.getName() | ||
+ " with ID " + user1.getId()); | ||
|
||
// Retrieve and display the user by ID | ||
User foundUser = User.findById(user1.getId()); | ||
if (foundUser != null) { | ||
System.out.println("User found: " + foundUser.getName() + " with email " + foundUser.getEmail()); | ||
System.out.println("User found: " + foundUser.getName() | ||
+ " with email " + foundUser.getEmail()); | ||
} else { | ||
System.out.println("User not found."); | ||
} | ||
|
@@ -29,22 +84,24 @@ public static void main(String[] args) { | |
foundUser.setName("John Updated"); | ||
foundUser.setEmail("[email protected]"); | ||
foundUser.save(); | ||
System.out.println("User updated: " + foundUser.getName() + " with email " + foundUser.getEmail()); | ||
System.out.println("User updated: " + foundUser.getName() | ||
+ " with email " + foundUser.getEmail()); | ||
|
||
// Retrieve all users | ||
List<User> users = User.findAll(); | ||
System.out.println("All users in the database:"); | ||
for (User user : users) { | ||
System.out.println("ID: " + user.getId() + ", Name: " + user.getName() + ", Email: " + user.getEmail()); | ||
System.out.println("ID: " + user.getId() | ||
+ ", Name: " + user.getName() | ||
+ ", Email: " + user.getEmail()); | ||
} | ||
|
||
// Delete the user | ||
try { | ||
System.out.println("Deleting user with ID: " + foundUser.getId()); | ||
foundUser.delete(); | ||
System.out.println("User successfully deleted!"); | ||
} | ||
catch (Exception e){ | ||
} catch (Exception e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
|
||
|
8 changes: 6 additions & 2 deletions
8
active-record/src/main/java/com/iluwatar/activerecord/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
active-record/src/main/java/com/iluwatar/activerecord/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* This project is licensed under the MIT license. | ||
* Module model-view-viewmodel is using | ||
* ZK framework licensed under LGPL (see lgpl-3.0.txt). | ||
* | ||
* The MIT License | ||
* Copyright © 2014-2022 Ilkka Seppälä | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
/** | ||
* Context and problem | ||
* Most applications require a way to interact with a database to perform | ||
* CRUD (Create, Read, Update, Delete) operations. | ||
* Traditionally, this involves writing a lot of boilerplate code | ||
* for database access, which can be error-prone and hard to maintain. | ||
* This is especially true in applications with | ||
* complex data models and relationships. | ||
* | ||
* <p>Often, developers need to write separate classes for database access and | ||
* domain models, leading to a separation of concerns | ||
* that can make the codebase more complex and harder to understand. | ||
* Additionally, changes to the database schema can require significant | ||
* changes to the data access code, increasing the maintenance burden. | ||
* | ||
* <p>Maintaining this separation can force the application to | ||
* adhere to at least some of the database's APIs or other semantics. | ||
* When these database features have quality issues, supporting them "corrupts" | ||
* what might otherwise be a cleanly designed application. | ||
* Similar issues can arise with any external system that your | ||
* development team doesn't control, not just databases. | ||
* | ||
* <p>Solution Simplify database interactions by using | ||
* the Active Record pattern. | ||
* This pattern encapsulates database access logic within the | ||
* domain model itself, allowing objects to be responsible | ||
* for their own persistence. | ||
* The Active Record pattern provides methods for | ||
* CRUD operations directly within the model class, | ||
* making database interactions straightforward and intuitive. | ||
*/ | ||
|
||
package com.iluwatar.activerecord; |
Binary file not shown.
Binary file not shown.