Skip to content

Commit 6599a33

Browse files
committed
Use ORMLite as ORM mapping tool
1 parent a43d832 commit 6599a33

File tree

6 files changed

+67
-9
lines changed

6 files changed

+67
-9
lines changed

database_creation.sql

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
-- rim of database
22
CREATE TABLE owner (
3-
id INT NOT NULL PRIMARY KEY,
3+
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
44
name VARCHAR(255) NOT NULL
55
);
66

77
CREATE TABLE device (
8-
id INT NOT NULL PRIMARY KEY,
8+
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
99
name VARCHAR(255) NOT NULL,
10-
owner_id INT NOT NULL,
10+
owner_id INTEGER NOT NULL,
1111
FOREIGN KEY (owner_id) REFERENCES owner (id)
1212
ON UPDATE CASCADE
1313
ON DELETE CASCADE
1414
);
1515

1616
CREATE TABLE key (
17-
id INT NOT NULL PRIMARY KEY,
17+
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
1818
license_key VARCHAR(255) NOT NULL,
1919
in_use BOOLEAN NOT NULL DEFAULT 0,
20-
device_id INT DEFAULT NULL,
20+
device_id INTEGER DEFAULT NULL,
2121
FOREIGN KEY (device_id) REFERENCES device (id)
2222
ON UPDATE CASCADE
2323
ON DELETE SET DEFAULT
@@ -26,4 +26,3 @@ CREATE TABLE key (
2626
CREATE UNIQUE INDEX owner_name ON owner (name);
2727
CREATE UNIQUE INDEX device_name ON device (name);
2828
CREATE UNIQUE INDEX license_key_id ON key (license_key);
29-

pom.xml

+11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@
1414
<artifactId>sqlite-jdbc</artifactId>
1515
<version>3.8.7</version>
1616
</dependency>
17+
<dependency>
18+
<groupId>com.j256.ormlite</groupId>
19+
<artifactId>ormlite-core</artifactId>
20+
<version>4.48</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>com.j256.ormlite</groupId>
24+
<artifactId>ormlite-jdbc</artifactId>
25+
<version>4.48</version>
26+
</dependency>
27+
1728
</dependencies>
1829

1930

src/main/java/nl/bascoder/keymanager/DatabaseManager.java

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package nl.bascoder.keymanager;
22

3+
import com.j256.ormlite.jdbc.JdbcConnectionSource;
4+
import com.j256.ormlite.support.ConnectionSource;
5+
36
import org.sqlite.SQLiteJDBCLoader;
47

58
import java.sql.Connection;
@@ -52,6 +55,15 @@ public static DatabaseManager getInstance() {
5255
return instance;
5356
}
5457

58+
/**
59+
* Returns ConnectionSource object for ormlite
60+
* @return ConnectionSource for ormlite
61+
* @throws SQLException if a error occured with SQL
62+
*/
63+
public ConnectionSource getConnectionSource() throws SQLException {
64+
return new JdbcConnectionSource(CONNECTION_URL);
65+
}
66+
5567
public synchronized Connection getConnection() throws SQLException {
5668
try {
5769
return DriverManager.getConnection(CONNECTION_URL);

src/main/java/nl/bascoder/keymanager/entity/Device.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package nl.bascoder.keymanager.entity;
22

3+
import com.j256.ormlite.field.DatabaseField;
4+
import com.j256.ormlite.table.DatabaseTable;
5+
36
/**
47
* @author Bas van Marwijk
5-
* @since 9-11-14
68
* @version 1.0 - creation
9+
* @since 9-11-14
710
*/
11+
@DatabaseTable(tableName = "device")
812
public class Device {
13+
@DatabaseField(generatedId = true)
914
private long id;
15+
@DatabaseField
1016
private String name;
17+
@DatabaseField(foreign = true, canBeNull = false)
1118
private Owner owner;
1219

1320
/**
@@ -63,5 +70,10 @@ public Owner getOwner() {
6370
public void setOwner(Owner owner) {
6471
this.owner = owner;
6572
}
73+
74+
@Override
75+
public String toString() {
76+
return name;
77+
}
6678
}
6779

src/main/java/nl/bascoder/keymanager/entity/Key.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
package nl.bascoder.keymanager.entity;
22

3+
import com.j256.ormlite.field.DatabaseField;
4+
import com.j256.ormlite.table.DatabaseTable;
5+
36
/**
47
* Entity containing license licenseKey.
58
*
69
* @author Bas van Marwijk
7-
* @since 9-11-14
810
* @version 1.0 - creation
11+
* @since 9-11-14
912
*/
13+
@DatabaseTable(tableName = "key")
1014
public class Key {
15+
@DatabaseField(generatedId = true)
1116
private long id;
17+
@DatabaseField
1218
private String licenseKey;
19+
@DatabaseField
1320
private boolean inUse;
21+
@DatabaseField(foreign = true, canBeNull = false)
1422
private Device device;
1523

1624
/**
@@ -84,5 +92,10 @@ public Device getDevice() {
8492
public void setDevice(Device device) {
8593
this.device = device;
8694
}
95+
96+
@Override
97+
public String toString() {
98+
return licenseKey;
99+
}
87100
}
88101

src/main/java/nl/bascoder/keymanager/entity/Owner.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package nl.bascoder.keymanager.entity;
22

3+
import com.j256.ormlite.field.DatabaseField;
4+
import com.j256.ormlite.table.DatabaseTable;
5+
36
/**
47
* @author Bas van Marwijk
5-
* @since 9-11-14
68
* @version 1.0 - creation
9+
* @since 9-11-14
710
*/
11+
@DatabaseTable(tableName = "owner")
812
public class Owner {
13+
@DatabaseField(generatedId = true)
914
private long id;
15+
@DatabaseField
1016
private String name;
1117

1218
/**
@@ -44,5 +50,10 @@ public String getName() {
4450
public void setName(String name) {
4551
this.name = name;
4652
}
53+
54+
@Override
55+
public String toString() {
56+
return name;
57+
}
4758
}
4859

0 commit comments

Comments
 (0)