Skip to content

Commit 69fd69f

Browse files
committed
Added SerializableBaseEntity
1 parent 2929cbf commit 69fd69f

File tree

11 files changed

+189
-114
lines changed

11 files changed

+189
-114
lines changed

.github/workflows/deploy-maven.yml

Lines changed: 0 additions & 22 deletions
This file was deleted.

.github/workflows/post-push.yml

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
name: Post-push actions
2+
3+
# Run workflow only on commits to `master`
4+
on:
5+
push:
6+
branches:
7+
- master
8+
9+
jobs:
10+
maven_deploy:
11+
name: Deploy to Maven
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Check out Git repository
15+
uses: actions/checkout@v1
16+
17+
- uses: actions/setup-java@v1
18+
with:
19+
java-version: '13'
20+
21+
- name: Release to Central Repository
22+
uses: samuelmeuli/action-maven-publish@v0.1.0
23+
with:
24+
gpg_private_key: ${{ secrets.gpg_private_key }}
25+
gpg_passphrase: ${{ secrets.gpg_passphrase }}
26+
nexus_username: ${{ secrets.nexus_username }}
27+
nexus_password: ${{ secrets.nexus_password }}
28+
29+
github_deploy:
30+
name: Deploy to GitHub Packages
31+
runs-on: ubuntu-latest
32+
steps:
33+
- name: Check out Git repository
34+
uses: actions/checkout@v1
35+
36+
- name: Setup JDK 13
37+
uses: actions/setup-java@v1
38+
with:
39+
java-version: '13'
40+
41+
- name: Release to GitHub Package Registry
42+
env:
43+
github_token: ${{ secrets.GITHUB_TOKEN }}
44+
gpg_private_key: ${{ secrets.gpg_private_key }}
45+
gpg_passphrase: ${{ secrets.gpg_passphrase }}
46+
run: |
47+
mkdir ~/.m2
48+
echo "${gpg_private_key}" | gpg --batch --import
49+
echo "<settings><servers><server><id>github</id><username>CollinAlpert</username><password>${github_token}</password></server></servers></settings>" > ~/.m2/settings.xml
50+
mvn clean deploy -B -e -Dmaven.wagon.http.pool=false -DaltDeploymentRepository=github::default::https://maven.pkg.github.com/CollinAlpert/TestMaven -Dgpg.passphrase=${gpg_passphrase}
51+
52+
github_release:
53+
name: Create GitHub release
54+
runs-on: ubuntu-latest
55+
steps:
56+
- name: Check out Git repository
57+
uses: actions/checkout@v1
58+
59+
- name: Setup JDK 13
60+
uses: actions/setup-java@v1
61+
with:
62+
java-version: '13'
63+
64+
- name: Build project
65+
run: mvn -B clean package
66+
67+
- name: Get project infos
68+
id: get-project-infos
69+
run: echo "::set-output name=maven_version::$(mvn -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive exec:exec -q)" && echo "::set-output name=maven_artifactId::$(mvn -Dexec.executable='echo' -Dexec.args='${project.artifactId}' --non-recursive exec:exec -q)"
70+
71+
- name: Create Release
72+
id: create_release
73+
uses: actions/create-release@v1.0.0
74+
env:
75+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
76+
with:
77+
tag_name: ${{ steps.get-project-infos.outputs.maven_version }}
78+
release_name: ${{ steps.get-project-infos.outputs.maven_version }}
79+
80+
- name: Upload JAR asset
81+
uses: actions/upload-release-asset@v1.0.1
82+
env:
83+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
84+
with:
85+
upload_url: ${{ steps.create_release.outputs.upload_url }}
86+
asset_path: ./target/${{ steps.get-project-infos.outputs.maven_artifactId }}-${{ steps.get-project-infos.outputs.maven_version }}.jar
87+
asset_name: ${{ steps.get-project-infos.outputs.maven_artifactId }}-${{ steps.get-project-infos.outputs.maven_version }}.jar
88+
asset_content_type: application/java-archive
89+
90+
- name: Upload JavaDoc asset
91+
uses: actions/upload-release-asset@v1.0.1
92+
env:
93+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
94+
with:
95+
upload_url: ${{ steps.create_release.outputs.upload_url }}
96+
asset_path: ./target/${{ steps.get-project-infos.outputs.maven_artifactId }}-${{ steps.get-project-infos.outputs.maven_version }}-javadoc.jar
97+
asset_name: ${{ steps.get-project-infos.outputs.maven_artifactId }}-${{ steps.get-project-infos.outputs.maven_version }}-javadoc.jar
98+
asset_content_type: application/java-archive
99+
100+
- name: Upload Sources asset
101+
uses: actions/upload-release-asset@v1.0.1
102+
env:
103+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
104+
with:
105+
upload_url: ${{ steps.create_release.outputs.upload_url }}
106+
asset_path: ./target/${{ steps.get-project-infos.outputs.maven_artifactId }}-${{ steps.get-project-infos.outputs.maven_version }}-sources.jar
107+
asset_name: ${{ steps.get-project-infos.outputs.maven_artifactId }}-${{ steps.get-project-infos.outputs.maven_version }}-sources.jar
108+
asset_content_type: application/java-archive

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Include the Maven artifact:
2020
<dependency>
2121
<groupId>com.github.collinalpert</groupId>
2222
<artifactId>java2db</artifactId>
23-
<version>5.2</version>
23+
<version>5.2.1</version>
2424
</dependency>
2525
```
2626
Or include the [JAR](https://github.com/CollinAlpert/Java2DB/releases/latest) in your project.
@@ -104,7 +104,7 @@ Every service *must* extend `BaseService`.
104104
That's it! Now we can get data from the database using the services using simple methods like `getById` and so on.\
105105
As you can see from the example, custom methods can be defined in the respective service using the `getSingle` or `getMultiple` methods provided by the `BaseService`.
106106

107-
The last thing you need to do is give Java2DB access to your database. Set the static variables `HOST`, `DATABASE`, `USERNAME`, `PASSWORD` and optionally `PORT` of the `DBConnection` class to achieve possibility of connection.
107+
The last thing you need to do is give Java2DB access to your database. Set the static variables `HOST`, `DATABASE`, `USERNAME`, `PASSWORD`, `TIMEOUT` (in seconds) and optionally `PORT` of the `DBConnection` class to achieve possibility of connection.
108108

109109
## Features
110110

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.collinalpert</groupId>
88
<artifactId>java2db</artifactId>
9-
<version>5.2</version>
9+
<version>5.2.1</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Java2DB</name>

src/main/java/com/github/collinalpert/java2db/database/DBConnection.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.collinalpert.java2db.database;
22

33
import com.github.collinalpert.java2db.exceptions.ConnectionFailedException;
4-
import com.github.collinalpert.java2db.modules.LoggingModule;
54
import com.mysql.cj.exceptions.CJCommunicationsException;
65
import com.mysql.cj.jdbc.exceptions.CommunicationsException;
76

@@ -14,15 +13,9 @@
1413

1514
/**
1615
* @author Collin Alpert
17-
* @see <a href="https://github.com/CollinAlpert/APIs/blob/master/de/collin/DBConnection.java">GitHub</a>
1816
*/
1917
public class DBConnection implements Closeable {
2018

21-
/**
22-
* The logger used to log queries and messages to the console.
23-
*/
24-
private static final LoggingModule logger;
25-
2619
/**
2720
* Specifies the hostname/ip address of the database.
2821
*/
@@ -49,25 +42,26 @@ public class DBConnection implements Closeable {
4942
*/
5043
public static int PORT = 3306;
5144

45+
/**
46+
* Specifies the login timeout to the database in seconds.
47+
*/
48+
public static int TIMEOUT = 5;
49+
5250
/**
5351
* Constant which determines if the queries generated by Java2DB will be logged in the console.
5452
*/
5553
public static boolean LOG_QUERIES = true;
5654

57-
static {
58-
DriverManager.setLoginTimeout(5);
59-
logger = LoggingModule.getInstance();
60-
}
61-
6255
private Connection connection;
6356
private boolean isConnectionValid;
6457

6558
public DBConnection() {
6659
try {
67-
var connectionString = String.format("jdbc:mysql://%s:%d/%s?serverTimezone=UTC", HOST, PORT, DATABASE);
60+
var connectionString = String.format("jdbc:mysql://%s:%d/%s?rewriteBatchedStatements=true", HOST, PORT, DATABASE);
6861
Class.forName("com.mysql.cj.jdbc.Driver");
6962
System.setProperty("user", USERNAME);
7063
System.setProperty("password", PASSWORD);
64+
DriverManager.setLoginTimeout(TIMEOUT);
7165
connection = DriverManager.getConnection(connectionString, System.getProperties());
7266
isConnectionValid = true;
7367
} catch (CJCommunicationsException | CommunicationsException e) {
@@ -97,10 +91,11 @@ public boolean isValid() {
9791
* @throws SQLException if the query is malformed or cannot be executed.
9892
*/
9993
public ResultSet execute(String query) throws SQLException {
100-
Statement statement = this.connection.createStatement();
101-
logger.log(query);
94+
var statement = this.connection.createStatement();
95+
log(query);
10296
var set = statement.executeQuery(query);
10397
statement.closeOnCompletion();
98+
10499
return set;
105100
}
106101

@@ -118,7 +113,7 @@ public ResultSet execute(String query, Object... params) throws SQLException {
118113
statement.setObject(i + 1, params[i]);
119114
}
120115

121-
logger.log(query);
116+
log(query);
122117
var set = statement.executeQuery();
123118
statement.closeOnCompletion();
124119
return set;
@@ -133,7 +128,7 @@ public ResultSet execute(String query, Object... params) throws SQLException {
133128
*/
134129
public long update(String query) throws SQLException {
135130
var statement = this.connection.createStatement();
136-
logger.log(query);
131+
log(query);
137132
statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
138133
return updateHelper(statement);
139134
}
@@ -152,7 +147,7 @@ public long update(String query, Object... params) throws SQLException {
152147
statement.setObject(i + 1, params[i]);
153148
}
154149

155-
logger.log(query);
150+
log(query);
156151
statement.executeUpdate();
157152
return updateHelper(statement);
158153
}
@@ -198,4 +193,15 @@ public void close() {
198193
this.isConnectionValid = false;
199194
}
200195
}
196+
197+
/**
198+
* Prints queries to the console, while considering the {@link DBConnection#LOG_QUERIES} constant.
199+
*
200+
* @param text The message to print.
201+
*/
202+
private void log(String text) {
203+
if (DBConnection.LOG_QUERIES) {
204+
System.out.println(text);
205+
}
206+
}
201207
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.github.collinalpert.java2db.entities;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* @author Collin Alpert
7+
*/
8+
public class SerializableBaseEntity extends BaseEntity implements Serializable {
9+
10+
private long id;
11+
12+
public long getId() {
13+
return id;
14+
}
15+
16+
/**
17+
* This setter only exists for frameworks like Spring, where a form needs to set this id.
18+
* It is <b>greatly</b> discouraged from using this setter directly and it's effects will not be considered with any of the CRUD operations.
19+
*
20+
* @param id The id of the entity.
21+
*/
22+
@Deprecated
23+
public void setId(long id) {
24+
this.id = id;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return "Id: " + this.id;
30+
}
31+
32+
@Override
33+
public boolean equals(Object o) {
34+
if (this == o) {
35+
return true;
36+
}
37+
38+
if (!(o instanceof SerializableBaseEntity)) {
39+
return false;
40+
}
41+
42+
SerializableBaseEntity that = (SerializableBaseEntity) o;
43+
44+
return this.id == that.id;
45+
}
46+
47+
@Override
48+
public int hashCode() {
49+
return (int) (this.id ^ (this.id >>> 32));
50+
}
51+
}

src/main/java/com/github/collinalpert/java2db/modules/LoggingModule.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/main/java/com/github/collinalpert/java2db/pagination/CacheablePaginationResult.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,6 @@ public void invalidateCaches() {
108108
public void invalidateCache(String name) {
109109
listCache.getValue().invalidate(name);
110110
streamCache.getValue().invalidate(name);
111+
arrayCache.getValue().invalidate(name);
111112
}
112113
}

src/main/java/com/github/collinalpert/java2db/pagination/PaginationResult.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public final PaginationResult<T> orderBy(SqlFunction<T, ?>... orderFunctions) {
142142
public final PaginationResult<T> orderBy(OrderTypes orderType, SqlFunction<T, ?>... orderFunctions) {
143143
this.orderFunctions = orderFunctions;
144144
this.orderType = orderType;
145+
145146
return this;
146147
}
147148
}

0 commit comments

Comments
 (0)