-
-
Notifications
You must be signed in to change notification settings - Fork 27k
Active record pattern implementation #206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
stephen-lazarionok
wants to merge
19
commits into
iluwatar:master
from
stephen-lazarionok:active-record
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
de9d111
Implement the active record pattern example
stephen-lazarionok 77b6e73
Add active-recotd module to the main pom.xml
stephen-lazarionok 677d061
Merge branch 'master' into active-record
stephen-lazarionok 0c6a5b0
Fix the tests and rename the class
stephen-lazarionok 7bc202c
Fix padding in pom.xml and change variable name
stephen-lazarionok 9a80bb0
Add the description of active record pattern
stephen-lazarionok 2b71444
Change the variable name
stephen-lazarionok 0045932
Use instance methods without this and fix typo
stephen-lazarionok 534b713
Fix typo
stephen-lazarionok d6c3e74
Fix typo and extract DB logic into a separate class
stephen-lazarionok c085690
Fix some small issues
stephen-lazarionok 10d73a9
Optimize imports and use double instead of Double for length
stephen-lazarionok 9f73cfe
Form try-catch blocks
stephen-lazarionok 6daddf9
Release all the resources in finally block and add the documentation …
stephen-lazarionok cfc63af
Reformat code according to Google style guidelines
stephen-lazarionok c01f49b
Remove the class diagram
stephen-lazarionok db208e9
Revert README.md
stephen-lazarionok ea4bf89
Fix readme
stephen-lazarionok 9129447
Update the version in pom.xml
stephen-lazarionok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Empty file.
This file contains hidden or 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,21 @@ | ||
--- | ||
layout: pattern | ||
title: Active Record | ||
folder: active-record | ||
permalink: /patterns/active-record/ | ||
categories: Architectural | ||
tags: Java | ||
--- | ||
|
||
**Intent:** Active record is an object that wraps a row in a database table or view, | ||
encapsulates the database access, and adds domain logic on that data. Active Record | ||
uses the most obvious approach, putting data access logic in the domain object. | ||
|
||
**Applicability:** Use active record pattern when | ||
|
||
* objects correspond directly to the database tables | ||
* business logic is not too complex | ||
|
||
**Real world examples:** | ||
|
||
* [ActiveJDBC](https://en.wikipedia.org/wiki/ActiveJDBC) |
This file contains hidden or 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,30 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>java-design-patterns</artifactId> | ||
<groupId>com.iluwatar</groupId> | ||
<version>1.6.0</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>active-record</artifactId> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
</dependency> | ||
|
||
<!-- Test dependencies --> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
</project> |
58 changes: 58 additions & 0 deletions
58
active-record/src/main/java/com/iluwatar/active/record/DB.java
This file contains hidden or 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,58 @@ | ||
package com.iluwatar.active.record; | ||
|
||
import org.h2.tools.DeleteDbFiles; | ||
|
||
import java.sql.*; | ||
|
||
/** | ||
* An utility class that holds and encapsulates all the DB related operations. | ||
* <p/> | ||
* Created by Stephen Lazarionok. | ||
*/ | ||
public class DB { | ||
|
||
static { | ||
DeleteDbFiles.execute("~", "test", true); | ||
try { | ||
Class.forName("org.h2.Driver"); | ||
final Connection connection = getConnection(); | ||
final Statement statement = connection.createStatement(); | ||
statement.execute( | ||
"create table wand(id BIGINT primary key, length_inches REAL, wood varchar(100), core varchar(100))"); | ||
statement.close(); | ||
connection.close(); | ||
} catch (final SQLException | ClassNotFoundException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Provides a connection to the database configured. | ||
* | ||
* @return | ||
* @throws SQLException | ||
*/ | ||
public static Connection getConnection() throws SQLException { | ||
return DriverManager.getConnection("jdbc:h2:~/test"); | ||
} | ||
|
||
public static void closeConnection(final Connection connection) { | ||
try { | ||
if (connection != null) { | ||
connection.close(); | ||
} | ||
} catch (SQLException e) { | ||
System.out.println("Failed to close a connection"); | ||
} | ||
} | ||
|
||
public static void closePreparedStatement(final PreparedStatement ps) { | ||
try { | ||
if (ps != null) { | ||
ps.close(); | ||
} | ||
} catch (SQLException e) { | ||
System.out.println("Failed to close a prepared statement"); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add javadoc explaining why this class is important. Mainly that it allows making the solution Multi-tenant. Switching DB will not require code change in
MagicWand
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the documentation to the DB class