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.
extend the logic of the RecordBase + manual test (iluwatar#79)
- Loading branch information
1 parent
fe908cc
commit 29ed0f9
Showing
4 changed files
with
132 additions
and
30 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
72 changes: 72 additions & 0 deletions
72
active-record/src/main/java/com/iluwatar/activerecord/App.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,72 @@ | ||
package com.iluwatar.activerecord; | ||
|
||
import java.sql.SQLException; | ||
import javax.sql.DataSource; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.h2.jdbcx.JdbcDataSource; | ||
|
||
@Slf4j | ||
public class App { | ||
|
||
private static final String DB_URL = "jdbc:h2:mem:dao;DB_CLOSE_DELAY=-1"; | ||
|
||
private static final String CREATE_SCHEMA_SQL = "CREATE TABLE customer\n" | ||
+ "(\n" | ||
+ " id BIGINT NOT NULL,\n" | ||
+ " customer_number VARCHAR(15) NOT NULL,\n" | ||
+ " first_name VARCHAR(45) NOT NULL,\n" | ||
+ " last_name VARCHAR(45) NOT NULL,\n" | ||
+ " CONSTRAINT customer_pkey PRIMARY KEY (id)\n" | ||
+ ");\n" | ||
+ "\n" | ||
+ "CREATE TABLE \"order\"\n" | ||
+ "(\n" | ||
+ " id BIGINT NOT NULL,\n" | ||
+ " order_number VARCHAR(15) NOT NULL,\n" | ||
+ " customer_id BIGINT NOT NULL,\n" | ||
+ " CONSTRAINT order_pkey PRIMARY KEY (id),\n" | ||
+ " CONSTRAINT customer_id_fk FOREIGN KEY (customer_id) REFERENCES customer (id)\n" | ||
+ ")"; | ||
|
||
|
||
public static void main(final String[] args) throws Exception { | ||
final var dataSource = createDataSource(); | ||
createSchema(dataSource); | ||
RecordBase.setDataSource(dataSource); | ||
executeOperation(); | ||
} | ||
|
||
private static void executeOperation() { | ||
LOGGER.info("saving the customer data..."); | ||
Customer customer = new Customer(); | ||
customer.setId(1L); | ||
customer.setCustomerNumber("C123"); | ||
customer.setFirstName("John"); | ||
customer.setLastName("Smith"); | ||
|
||
Order order = new Order(); | ||
order.setId(1L); | ||
order.setOrderNumber("O123"); | ||
|
||
// customer.addOrder(order); | ||
customer.save(); | ||
|
||
LOGGER.info("The customer data by ID={}", customer.findById(1L)); | ||
|
||
LOGGER.info("find all the customers={}", customer.findAll()); | ||
} | ||
|
||
private static void createSchema(DataSource dataSource) throws SQLException { | ||
try (var connection = dataSource.getConnection(); | ||
var statement = connection.createStatement()) { | ||
statement.execute(CREATE_SCHEMA_SQL); | ||
} | ||
} | ||
|
||
private static DataSource createDataSource() { | ||
var dataSource = new JdbcDataSource(); | ||
dataSource.setURL(DB_URL); | ||
return dataSource; | ||
} | ||
|
||
} |
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
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