Compare commits
1 Commits
all-contri
...
event-aggr
Author | SHA1 | Date | |
---|---|---|---|
2ddb24e698 |
@ -1776,15 +1776,6 @@
|
|||||||
"contributions": [
|
"contributions": [
|
||||||
"code"
|
"code"
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"login": "castleKing1997",
|
|
||||||
"name": "DragonDreamer",
|
|
||||||
"avatar_url": "https://avatars.githubusercontent.com/u/35420129?v=4",
|
|
||||||
"profile": "http://rosaecrucis.cn",
|
|
||||||
"contributions": [
|
|
||||||
"code"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"contributorsPerLine": 7,
|
"contributorsPerLine": 7,
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
||||||
[](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
[](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
||||||
[](#contributors-)
|
[](#contributors-)
|
||||||
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
||||||
|
|
||||||
<br/>
|
<br/>
|
||||||
@ -325,7 +325,6 @@ This project is licensed under the terms of the MIT license.
|
|||||||
<td align="center"><a href="https://github.com/interactwithankush"><img src="https://avatars.githubusercontent.com/u/18613127?v=4?s=100" width="100px;" alt=""/><br /><sub><b>interactwithankush</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=interactwithankush" title="Code">💻</a></td>
|
<td align="center"><a href="https://github.com/interactwithankush"><img src="https://avatars.githubusercontent.com/u/18613127?v=4?s=100" width="100px;" alt=""/><br /><sub><b>interactwithankush</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=interactwithankush" title="Code">💻</a></td>
|
||||||
<td align="center"><a href="https://github.com/yuhangbin"><img src="https://avatars.githubusercontent.com/u/17566866?v=4?s=100" width="100px;" alt=""/><br /><sub><b>CharlieYu</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=yuhangbin" title="Code">💻</a></td>
|
<td align="center"><a href="https://github.com/yuhangbin"><img src="https://avatars.githubusercontent.com/u/17566866?v=4?s=100" width="100px;" alt=""/><br /><sub><b>CharlieYu</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=yuhangbin" title="Code">💻</a></td>
|
||||||
<td align="center"><a href="https://github.com/Leisterbecker"><img src="https://avatars.githubusercontent.com/u/20650323?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Leisterbecker</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=Leisterbecker" title="Code">💻</a></td>
|
<td align="center"><a href="https://github.com/Leisterbecker"><img src="https://avatars.githubusercontent.com/u/20650323?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Leisterbecker</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=Leisterbecker" title="Code">💻</a></td>
|
||||||
<td align="center"><a href="http://rosaecrucis.cn"><img src="https://avatars.githubusercontent.com/u/35420129?v=4?s=100" width="100px;" alt=""/><br /><sub><b>DragonDreamer</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=castleKing1997" title="Code">💻</a></td>
|
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
@ -9,6 +9,10 @@ tags:
|
|||||||
- Reactive
|
- Reactive
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## Name
|
||||||
|
|
||||||
|
Event Aggregator
|
||||||
|
|
||||||
## Intent
|
## Intent
|
||||||
A system with lots of objects can lead to complexities when a
|
A system with lots of objects can lead to complexities when a
|
||||||
client wants to subscribe to events. The client has to find and register for
|
client wants to subscribe to events. The client has to find and register for
|
||||||
@ -17,6 +21,136 @@ requires a separate subscription. An Event Aggregator acts as a single source
|
|||||||
of events for many objects. It registers for all the events of the many objects
|
of events for many objects. It registers for all the events of the many objects
|
||||||
allowing clients to register with just the aggregator.
|
allowing clients to register with just the aggregator.
|
||||||
|
|
||||||
|
## Explanation
|
||||||
|
|
||||||
|
Real-world example
|
||||||
|
|
||||||
|
> King Joffrey sits on the iron throne and rules the seven kingdoms of Westeros. He receives most
|
||||||
|
> of his critical information from King's Hand, the second in command. King's hand has many
|
||||||
|
> close advisors himself, feeding him with relevant information about events occurring in the
|
||||||
|
> kingdom.
|
||||||
|
|
||||||
|
In Plain Words
|
||||||
|
|
||||||
|
> Event Aggregator is an event mediator that collects events from multiple sources and delivers
|
||||||
|
> them to registered observers.
|
||||||
|
|
||||||
|
**Programmatic Example**
|
||||||
|
|
||||||
|
In our programmatic example, we demonstrate the implementation of an event aggregator pattern. Some of
|
||||||
|
the objects are event listeners, some are event emitters, and the event aggregator does both.
|
||||||
|
|
||||||
|
```java
|
||||||
|
public interface EventObserver {
|
||||||
|
void onEvent(Event e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class EventEmitter {
|
||||||
|
|
||||||
|
private final Map<Event, List<EventObserver>> observerLists;
|
||||||
|
|
||||||
|
public EventEmitter() {
|
||||||
|
observerLists = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void registerObserver(EventObserver obs, Event e) {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void notifyObservers(Event e) {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`KingJoffrey` is listening to events from `KingsHand`.
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Slf4j
|
||||||
|
public class KingJoffrey implements EventObserver {
|
||||||
|
@Override
|
||||||
|
public void onEvent(Event e) {
|
||||||
|
LOGGER.info("Received event from the King's Hand: {}", e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`KingsHand` is listening to events from his subordinates `LordBaelish`, `LordVarys`, and `Scout`.
|
||||||
|
Whatever he hears from them, he delivers to `KingJoffrey`.
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class KingsHand extends EventEmitter implements EventObserver {
|
||||||
|
|
||||||
|
public KingsHand() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public KingsHand(EventObserver obs, Event e) {
|
||||||
|
super(obs, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onEvent(Event e) {
|
||||||
|
notifyObservers(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
For example, `LordVarys` finds a traitor every Sunday and notifies the `KingsHand`.
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Slf4j
|
||||||
|
public class LordVarys extends EventEmitter implements EventObserver {
|
||||||
|
@Override
|
||||||
|
public void timePasses(Weekday day) {
|
||||||
|
if (day == Weekday.SATURDAY) {
|
||||||
|
notifyObservers(Event.TRAITOR_DETECTED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The following snippet demonstrates how the objects are constructed and wired together.
|
||||||
|
|
||||||
|
```java
|
||||||
|
var kingJoffrey = new KingJoffrey();
|
||||||
|
|
||||||
|
var kingsHand = new KingsHand();
|
||||||
|
kingsHand.registerObserver(kingJoffrey, Event.TRAITOR_DETECTED);
|
||||||
|
kingsHand.registerObserver(kingJoffrey, Event.STARK_SIGHTED);
|
||||||
|
kingsHand.registerObserver(kingJoffrey, Event.WARSHIPS_APPROACHING);
|
||||||
|
kingsHand.registerObserver(kingJoffrey, Event.WHITE_WALKERS_SIGHTED);
|
||||||
|
|
||||||
|
var varys = new LordVarys();
|
||||||
|
varys.registerObserver(kingsHand, Event.TRAITOR_DETECTED);
|
||||||
|
varys.registerObserver(kingsHand, Event.WHITE_WALKERS_SIGHTED);
|
||||||
|
|
||||||
|
var scout = new Scout();
|
||||||
|
scout.registerObserver(kingsHand, Event.WARSHIPS_APPROACHING);
|
||||||
|
scout.registerObserver(varys, Event.WHITE_WALKERS_SIGHTED);
|
||||||
|
|
||||||
|
var baelish = new LordBaelish(kingsHand, Event.STARK_SIGHTED);
|
||||||
|
|
||||||
|
var emitters = List.of(
|
||||||
|
kingsHand,
|
||||||
|
baelish,
|
||||||
|
varys,
|
||||||
|
scout
|
||||||
|
);
|
||||||
|
|
||||||
|
Arrays.stream(Weekday.values())
|
||||||
|
.<Consumer<? super EventEmitter>>map(day -> emitter -> emitter.timePasses(day))
|
||||||
|
.forEachOrdered(emitters::forEach);
|
||||||
|
```
|
||||||
|
|
||||||
|
The console output after running the example.
|
||||||
|
|
||||||
|
```
|
||||||
|
18:21:52.955 [main] INFO com.iluwatar.event.aggregator.KingJoffrey - Received event from the King's Hand: Warships approaching
|
||||||
|
18:21:52.960 [main] INFO com.iluwatar.event.aggregator.KingJoffrey - Received event from the King's Hand: White walkers sighted
|
||||||
|
18:21:52.960 [main] INFO com.iluwatar.event.aggregator.KingJoffrey - Received event from the King's Hand: Stark sighted
|
||||||
|
18:21:52.960 [main] INFO com.iluwatar.event.aggregator.KingJoffrey - Received event from the King's Hand: Traitor detected
|
||||||
|
```
|
||||||
|
|
||||||
## Class diagram
|
## Class diagram
|
||||||

|

|
||||||
|
|
||||||
@ -26,9 +160,13 @@ Use the Event Aggregator pattern when
|
|||||||
* Event Aggregator is a good choice when you have lots of objects that are
|
* Event Aggregator is a good choice when you have lots of objects that are
|
||||||
potential event sources. Rather than have the observer deal with registering
|
potential event sources. Rather than have the observer deal with registering
|
||||||
with them all, you can centralize the registration logic to the Event
|
with them all, you can centralize the registration logic to the Event
|
||||||
Aggregator. As well as simplifying registration, a Event Aggregator also
|
Aggregator. As well as simplifying registration, an Event Aggregator also
|
||||||
simplifies the memory management issues in using observers.
|
simplifies the memory management issues in using observers.
|
||||||
|
|
||||||
|
## Related patterns
|
||||||
|
|
||||||
|
* [Observer](https://java-design-patterns.com/patterns/observer/)
|
||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
* [Martin Fowler - Event Aggregator](http://martinfowler.com/eaaDev/EventAggregator.html)
|
* [Martin Fowler - Event Aggregator](http://martinfowler.com/eaaDev/EventAggregator.html)
|
||||||
|
@ -1,182 +0,0 @@
|
|||||||
---
|
|
||||||
layout: pattern
|
|
||||||
title: Metadata Mapping
|
|
||||||
folder: metadata-mapping
|
|
||||||
permalink: /patterns/metadata-mapping/
|
|
||||||
categories: Architectural
|
|
||||||
language: en
|
|
||||||
tags:
|
|
||||||
- Data access
|
|
||||||
---
|
|
||||||
|
|
||||||
## Intent
|
|
||||||
|
|
||||||
Holds details of object-relational mapping in the metadata.
|
|
||||||
|
|
||||||
## Explanation
|
|
||||||
|
|
||||||
Real world example
|
|
||||||
|
|
||||||
> Hibernate ORM Tool uses Metadata Mapping Pattern to specify the mapping between classes and tables either using XML or annotations in code.
|
|
||||||
|
|
||||||
In plain words
|
|
||||||
|
|
||||||
> Metadata Mapping specifies the mapping between classes and tables so that we could treat a table of any database like a Java class.
|
|
||||||
|
|
||||||
Wikipedia says
|
|
||||||
|
|
||||||
> Create a "virtual [object database](https://en.wikipedia.org/wiki/Object_database)" that can be used from within the programming language.
|
|
||||||
|
|
||||||
**Programmatic Example**
|
|
||||||
|
|
||||||
We give an example about visiting the information of `USER` table in `h2` database. Firstly, we create `USER` table with `h2`:
|
|
||||||
|
|
||||||
```java
|
|
||||||
@Slf4j
|
|
||||||
public class DatabaseUtil {
|
|
||||||
private static final String DB_URL = "jdbc:h2:mem:metamapping";
|
|
||||||
private static final String CREATE_SCHEMA_SQL = "DROP TABLE IF EXISTS `user`;"
|
|
||||||
+ "CREATE TABLE `user` (\n"
|
|
||||||
+ " `id` int(11) NOT NULL AUTO_INCREMENT,\n"
|
|
||||||
+ " `username` varchar(255) NOT NULL,\n"
|
|
||||||
+ " `password` varchar(255) NOT NULL,\n"
|
|
||||||
+ " PRIMARY KEY (`id`)\n"
|
|
||||||
+ ");";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create database.
|
|
||||||
*/
|
|
||||||
static {
|
|
||||||
LOGGER.info("create h2 database");
|
|
||||||
var source = new JdbcDataSource();
|
|
||||||
source.setURL(DB_URL);
|
|
||||||
try (var statement = source.getConnection().createStatement()) {
|
|
||||||
statement.execute(CREATE_SCHEMA_SQL);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
LOGGER.error("unable to create h2 data source", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Correspondingly, here's the basic `User` entity.
|
|
||||||
|
|
||||||
```java
|
|
||||||
@Setter
|
|
||||||
@Getter
|
|
||||||
@ToString
|
|
||||||
public class User {
|
|
||||||
private Integer id;
|
|
||||||
private String username;
|
|
||||||
private String password;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a user.
|
|
||||||
* @param username user name
|
|
||||||
* @param password user password
|
|
||||||
*/
|
|
||||||
public User(String username, String password) {
|
|
||||||
this.username = username;
|
|
||||||
this.password = password;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Then we write a `xml` file to show the mapping between the table and the object:
|
|
||||||
|
|
||||||
```xml
|
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<!DOCTYPE hibernate-mapping PUBLIC
|
|
||||||
"-//Hibernate/Hibernate Mapping DTD//EN"
|
|
||||||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
|
|
||||||
|
|
||||||
<hibernate-mapping>
|
|
||||||
<class name="com.iluwatar.metamapping.model.User" table="user">
|
|
||||||
<id name="id" type="java.lang.Integer" column="id">
|
|
||||||
<generator class="native"/>
|
|
||||||
</id>
|
|
||||||
<property name="username" column="username" type="java.lang.String"/>
|
|
||||||
<property name="password" column="password" type="java.lang.String"/>
|
|
||||||
</class>
|
|
||||||
</hibernate-mapping>
|
|
||||||
```
|
|
||||||
|
|
||||||
We use `Hibernate` to resolve the mapping and connect to our database, here's its configuration:
|
|
||||||
|
|
||||||
```xml
|
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<!DOCTYPE hibernate-configuration PUBLIC
|
|
||||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
|
||||||
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
|
|
||||||
<hibernate-configuration>
|
|
||||||
<session-factory>
|
|
||||||
<!-- JDBC Database connection settings -->
|
|
||||||
<property name="connection.url">jdbc:h2:mem:metamapping</property>
|
|
||||||
<property name="connection.driver_class">org.h2.Driver</property>
|
|
||||||
<!-- JDBC connection pool settings ... using built-in test pool -->
|
|
||||||
<property name="connection.pool_size">1</property>
|
|
||||||
<!-- Select our SQL dialect -->
|
|
||||||
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
|
|
||||||
<!-- Echo the SQL to stdout -->
|
|
||||||
<property name="show_sql">false</property>
|
|
||||||
<!-- Drop and re-create the database schema on startup -->
|
|
||||||
<property name="hbm2ddl.auto">create-drop</property>
|
|
||||||
<mapping resource="com/iluwatar/metamapping/model/User.hbm.xml" />
|
|
||||||
</session-factory>
|
|
||||||
</hibernate-configuration>
|
|
||||||
```
|
|
||||||
|
|
||||||
Then we can get access to the table just like an object with `Hibernate`, here's some CRUDs:
|
|
||||||
|
|
||||||
```java
|
|
||||||
@Slf4j
|
|
||||||
public class UserService {
|
|
||||||
private static final SessionFactory factory = HibernateUtil.getSessionFactory();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* List all users.
|
|
||||||
* @return list of users
|
|
||||||
*/
|
|
||||||
public List<User> listUser() {
|
|
||||||
LOGGER.info("list all users.");
|
|
||||||
List<User> users = new ArrayList<>();
|
|
||||||
try (var session = factory.openSession()) {
|
|
||||||
var tx = session.beginTransaction();
|
|
||||||
List<User> userIter = session.createQuery("FROM User").list();
|
|
||||||
for (var iterator = userIter.iterator(); iterator.hasNext();) {
|
|
||||||
users.add(iterator.next());
|
|
||||||
}
|
|
||||||
tx.commit();
|
|
||||||
} catch (HibernateException e) {
|
|
||||||
LOGGER.debug("fail to get users", e);
|
|
||||||
}
|
|
||||||
return users;
|
|
||||||
}
|
|
||||||
|
|
||||||
// other CRUDs ->
|
|
||||||
...
|
|
||||||
|
|
||||||
public void close() {
|
|
||||||
HibernateUtil.shutdown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Class diagram
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## Applicability
|
|
||||||
|
|
||||||
Use the Metadata Mapping when:
|
|
||||||
|
|
||||||
- you want reduce the amount of work needed to handle database mapping.
|
|
||||||
|
|
||||||
## Known uses
|
|
||||||
|
|
||||||
[Hibernate](https://hibernate.org/), [EclipseLink](https://www.eclipse.org/eclipselink/), [MyBatis](https://blog.mybatis.org/)......
|
|
||||||
|
|
||||||
## Credits
|
|
||||||
|
|
||||||
- [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=48d37c67fb3d845b802fa9b619ad8f31)
|
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 49 KiB |
@ -1,32 +0,0 @@
|
|||||||
@startuml
|
|
||||||
interface com.iluwatar.metamapping.service.UserService {
|
|
||||||
+ List<User> listUser()
|
|
||||||
+ int createUser(User)
|
|
||||||
+ void updateUser(Integer,User)
|
|
||||||
+ void deleteUser(Integer)
|
|
||||||
+ User getUser(Integer)
|
|
||||||
+ void close()
|
|
||||||
}
|
|
||||||
class com.iluwatar.metamapping.utils.DatabaseUtil {
|
|
||||||
+ {static} void createDataSource()
|
|
||||||
}
|
|
||||||
class com.iluwatar.metamapping.model.User {
|
|
||||||
- Integer id
|
|
||||||
- String username
|
|
||||||
- String password
|
|
||||||
+ User(String username, String password)
|
|
||||||
}
|
|
||||||
class com.iluwatar.metamapping.utils.HibernateUtil {
|
|
||||||
+ {static} SessionFactory getSessionFactory()
|
|
||||||
+ {static} void shutdown()
|
|
||||||
}
|
|
||||||
class com.iluwatar.metamapping.App {
|
|
||||||
+ {static} void main(String[])
|
|
||||||
+ {static} List<User> generateSampleUsers()
|
|
||||||
}
|
|
||||||
|
|
||||||
com.iluwatar.metamapping.service.UserService <.. com.iluwatar.metamapping.App
|
|
||||||
com.iluwatar.metamapping.model.User <.. com.iluwatar.metamapping.service.UserService
|
|
||||||
com.iluwatar.metamapping.utils.HibernateUtil <.. com.iluwatar.metamapping.service.UserService
|
|
||||||
com.iluwatar.metamapping.utils.DatabaseUtil <-- com.iluwatar.metamapping.utils.HibernateUtil
|
|
||||||
@enduml
|
|
@ -1,87 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<!--
|
|
||||||
|
|
||||||
The MIT License
|
|
||||||
Copyright © 2014-2021 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.
|
|
||||||
|
|
||||||
-->
|
|
||||||
<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.26.0-SNAPSHOT</version>
|
|
||||||
</parent>
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
|
|
||||||
<artifactId>metadata-mapping</artifactId>
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.junit.jupiter</groupId>
|
|
||||||
<artifactId>junit-jupiter-engine</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.h2database</groupId>
|
|
||||||
<artifactId>h2</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.hibernate</groupId>
|
|
||||||
<artifactId>hibernate-core</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.h2database</groupId>
|
|
||||||
<artifactId>h2</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>javax.xml.bind</groupId>
|
|
||||||
<artifactId>jaxb-api</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.sun.xml.bind</groupId>
|
|
||||||
<artifactId>jaxb-impl</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.sun.istack</groupId>
|
|
||||||
<artifactId>istack-commons-runtime</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-assembly-plugin</artifactId>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<configuration>
|
|
||||||
<archive>
|
|
||||||
<manifest>
|
|
||||||
<mainClass>com.iluwatar.metamapping.App</mainClass>
|
|
||||||
</manifest>
|
|
||||||
</archive>
|
|
||||||
</configuration>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
</project>
|
|
@ -1,72 +0,0 @@
|
|||||||
package com.iluwatar.metamapping;
|
|
||||||
|
|
||||||
import com.iluwatar.metamapping.model.User;
|
|
||||||
import com.iluwatar.metamapping.service.UserService;
|
|
||||||
import com.iluwatar.metamapping.utils.DatabaseUtil;
|
|
||||||
import java.util.List;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.hibernate.service.ServiceRegistry;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Metadata Mapping specifies the mapping
|
|
||||||
* between classes and tables so that
|
|
||||||
* we could treat a table of any database like a Java class.
|
|
||||||
*
|
|
||||||
* <p>With hibernate, we achieve list/create/update/delete/get operations:
|
|
||||||
* 1)Create the H2 Database in {@link DatabaseUtil}.
|
|
||||||
* 2)Hibernate resolve hibernate.cfg.xml and generate service like save/list/get/delete.
|
|
||||||
* For learning metadata mapping pattern, we go deeper into Hibernate here:
|
|
||||||
* a)read properties from hibernate.cfg.xml and mapping from *.hbm.xml
|
|
||||||
* b)create session factory to generate session interacting with database
|
|
||||||
* c)generate session with factory pattern
|
|
||||||
* d)create query object or use basic api with session,
|
|
||||||
* hibernate will convert all query to database query according to metadata
|
|
||||||
* 3)We encapsulate hibernate service in {@link UserService} for our use.
|
|
||||||
* @see org.hibernate.cfg.Configuration#configure(String)
|
|
||||||
* @see org.hibernate.cfg.Configuration#buildSessionFactory(ServiceRegistry)
|
|
||||||
* @see org.hibernate.internal.SessionFactoryImpl#openSession()
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
public class App {
|
|
||||||
/**
|
|
||||||
* Program entry point.
|
|
||||||
*
|
|
||||||
* @param args command line args.
|
|
||||||
* @throws Exception if any error occurs.
|
|
||||||
*/
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
// get service
|
|
||||||
var userService = new UserService();
|
|
||||||
// use create service to add users
|
|
||||||
for (var user: generateSampleUsers()) {
|
|
||||||
var id = userService.createUser(user);
|
|
||||||
LOGGER.info("Add user" + user + "at" + id + ".");
|
|
||||||
}
|
|
||||||
// use list service to get users
|
|
||||||
var users = userService.listUser();
|
|
||||||
LOGGER.info(String.valueOf(users));
|
|
||||||
// use get service to get a user
|
|
||||||
var user = userService.getUser(1);
|
|
||||||
LOGGER.info(String.valueOf(user));
|
|
||||||
// change password of user 1
|
|
||||||
user.setPassword("new123");
|
|
||||||
// use update service to update user 1
|
|
||||||
userService.updateUser(1, user);
|
|
||||||
// use delete service to delete user 2
|
|
||||||
userService.deleteUser(2);
|
|
||||||
// close service
|
|
||||||
userService.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate users.
|
|
||||||
*
|
|
||||||
* @return list of users.
|
|
||||||
*/
|
|
||||||
public static List<User> generateSampleUsers() {
|
|
||||||
final var user1 = new User("ZhangSan", "zhs123");
|
|
||||||
final var user2 = new User("LiSi", "ls123");
|
|
||||||
final var user3 = new User("WangWu", "ww123");
|
|
||||||
return List.of(user1, user2, user3);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,29 +0,0 @@
|
|||||||
package com.iluwatar.metamapping.model;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
import lombok.ToString;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* User Entity.
|
|
||||||
*/
|
|
||||||
@Setter
|
|
||||||
@Getter
|
|
||||||
@ToString
|
|
||||||
public class User {
|
|
||||||
private Integer id;
|
|
||||||
private String username;
|
|
||||||
private String password;
|
|
||||||
|
|
||||||
public User() {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a user.
|
|
||||||
* @param username user name
|
|
||||||
* @param password user password
|
|
||||||
*/
|
|
||||||
public User(String username, String password) {
|
|
||||||
this.username = username;
|
|
||||||
this.password = password;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,114 +0,0 @@
|
|||||||
package com.iluwatar.metamapping.service;
|
|
||||||
|
|
||||||
import com.iluwatar.metamapping.model.User;
|
|
||||||
import com.iluwatar.metamapping.utils.HibernateUtil;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.hibernate.HibernateException;
|
|
||||||
import org.hibernate.SessionFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Service layer for user.
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
public class UserService {
|
|
||||||
private static final SessionFactory factory = HibernateUtil.getSessionFactory();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* List all users.
|
|
||||||
* @return list of users
|
|
||||||
*/
|
|
||||||
public List<User> listUser() {
|
|
||||||
LOGGER.info("list all users.");
|
|
||||||
List<User> users = new ArrayList<>();
|
|
||||||
try (var session = factory.openSession()) {
|
|
||||||
var tx = session.beginTransaction();
|
|
||||||
List<User> userIter = session.createQuery("FROM User").list();
|
|
||||||
for (var iterator = userIter.iterator(); iterator.hasNext();) {
|
|
||||||
users.add(iterator.next());
|
|
||||||
}
|
|
||||||
tx.commit();
|
|
||||||
} catch (HibernateException e) {
|
|
||||||
LOGGER.debug("fail to get users", e);
|
|
||||||
}
|
|
||||||
return users;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add a user.
|
|
||||||
* @param user user entity
|
|
||||||
* @return user id
|
|
||||||
*/
|
|
||||||
public int createUser(User user) {
|
|
||||||
LOGGER.info("create user: " + user.getUsername());
|
|
||||||
var id = -1;
|
|
||||||
try (var session = factory.openSession()) {
|
|
||||||
var tx = session.beginTransaction();
|
|
||||||
id = (Integer) session.save(user);
|
|
||||||
tx.commit();
|
|
||||||
} catch (HibernateException e) {
|
|
||||||
LOGGER.debug("fail to create user", e);
|
|
||||||
}
|
|
||||||
LOGGER.info("create user " + user.getUsername() + " at " + id);
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Update user.
|
|
||||||
* @param id user id
|
|
||||||
* @param user new user entity
|
|
||||||
*/
|
|
||||||
public void updateUser(Integer id, User user) {
|
|
||||||
LOGGER.info("update user at " + id);
|
|
||||||
try (var session = factory.openSession()) {
|
|
||||||
var tx = session.beginTransaction();
|
|
||||||
user.setId(id);
|
|
||||||
session.update(user);
|
|
||||||
tx.commit();
|
|
||||||
} catch (HibernateException e) {
|
|
||||||
LOGGER.debug("fail to update user", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete user.
|
|
||||||
* @param id user id
|
|
||||||
*/
|
|
||||||
public void deleteUser(Integer id) {
|
|
||||||
LOGGER.info("delete user at: " + id);
|
|
||||||
try (var session = factory.openSession()) {
|
|
||||||
var tx = session.beginTransaction();
|
|
||||||
var user = session.get(User.class, id);
|
|
||||||
session.delete(user);
|
|
||||||
tx.commit();
|
|
||||||
} catch (HibernateException e) {
|
|
||||||
LOGGER.debug("fail to delete user", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get user.
|
|
||||||
* @param id user id
|
|
||||||
* @return deleted user
|
|
||||||
*/
|
|
||||||
public User getUser(Integer id) {
|
|
||||||
LOGGER.info("get user at: " + id);
|
|
||||||
User user = null;
|
|
||||||
try (var session = factory.openSession()) {
|
|
||||||
var tx = session.beginTransaction();
|
|
||||||
user = session.get(User.class, id);
|
|
||||||
tx.commit();
|
|
||||||
} catch (HibernateException e) {
|
|
||||||
LOGGER.debug("fail to get user", e);
|
|
||||||
}
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Close hibernate.
|
|
||||||
*/
|
|
||||||
public void close() {
|
|
||||||
HibernateUtil.shutdown();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
package com.iluwatar.metamapping.utils;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.h2.jdbcx.JdbcDataSource;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create h2 database.
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
public class DatabaseUtil {
|
|
||||||
private static final String DB_URL = "jdbc:h2:mem:metamapping";
|
|
||||||
private static final String CREATE_SCHEMA_SQL = "DROP TABLE IF EXISTS `user`;"
|
|
||||||
+ "CREATE TABLE `user` (\n"
|
|
||||||
+ " `id` int(11) NOT NULL AUTO_INCREMENT,\n"
|
|
||||||
+ " `username` varchar(255) NOT NULL,\n"
|
|
||||||
+ " `password` varchar(255) NOT NULL,\n"
|
|
||||||
+ " PRIMARY KEY (`id`)\n"
|
|
||||||
+ ");";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Hide constructor.
|
|
||||||
*/
|
|
||||||
private DatabaseUtil() {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create database.
|
|
||||||
*/
|
|
||||||
static {
|
|
||||||
LOGGER.info("create h2 database");
|
|
||||||
var source = new JdbcDataSource();
|
|
||||||
source.setURL(DB_URL);
|
|
||||||
try (var statement = source.getConnection().createStatement()) {
|
|
||||||
statement.execute(CREATE_SCHEMA_SQL);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
LOGGER.error("unable to create h2 data source", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
package com.iluwatar.metamapping.utils;
|
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.hibernate.SessionFactory;
|
|
||||||
import org.hibernate.cfg.Configuration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Manage hibernate.
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
public class HibernateUtil {
|
|
||||||
|
|
||||||
private static final SessionFactory sessionFactory = buildSessionFactory();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Hide constructor.
|
|
||||||
*/
|
|
||||||
private HibernateUtil() {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Build session factory.
|
|
||||||
* @return session factory
|
|
||||||
*/
|
|
||||||
private static SessionFactory buildSessionFactory() {
|
|
||||||
// Create the SessionFactory from hibernate.cfg.xml
|
|
||||||
return new Configuration().configure().buildSessionFactory();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get session factory.
|
|
||||||
* @return session factory
|
|
||||||
*/
|
|
||||||
public static SessionFactory getSessionFactory() {
|
|
||||||
return sessionFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Close session factory.
|
|
||||||
*/
|
|
||||||
public static void shutdown() {
|
|
||||||
// Close caches and connection pools
|
|
||||||
getSessionFactory().close();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<!DOCTYPE hibernate-mapping PUBLIC
|
|
||||||
"-//Hibernate/Hibernate Mapping DTD//EN"
|
|
||||||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
|
|
||||||
|
|
||||||
<hibernate-mapping>
|
|
||||||
<class name="com.iluwatar.metamapping.model.User" table="user">
|
|
||||||
<id name="id" type="java.lang.Integer" column="id">
|
|
||||||
<generator class="native"/>
|
|
||||||
</id>
|
|
||||||
<property name="username" column="username" type="java.lang.String"/>
|
|
||||||
<property name="password" column="password" type="java.lang.String"/>
|
|
||||||
</class>
|
|
||||||
</hibernate-mapping>
|
|
@ -1,20 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<!DOCTYPE hibernate-configuration PUBLIC
|
|
||||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
|
||||||
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
|
|
||||||
<hibernate-configuration>
|
|
||||||
<session-factory>
|
|
||||||
<!-- JDBC Database connection settings -->
|
|
||||||
<property name="connection.url">jdbc:h2:mem:metamapping</property>
|
|
||||||
<property name="connection.driver_class">org.h2.Driver</property>
|
|
||||||
<!-- JDBC connection pool settings ... using built-in test pool -->
|
|
||||||
<property name="connection.pool_size">1</property>
|
|
||||||
<!-- Select our SQL dialect -->
|
|
||||||
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
|
|
||||||
<!-- Echo the SQL to stdout -->
|
|
||||||
<property name="show_sql">false</property>
|
|
||||||
<!-- Drop and re-create the database schema on startup -->
|
|
||||||
<property name="hbm2ddl.auto">create-drop</property>
|
|
||||||
<mapping resource="com/iluwatar/metamapping/model/User.hbm.xml" />
|
|
||||||
</session-factory>
|
|
||||||
</hibernate-configuration>
|
|
@ -1,20 +0,0 @@
|
|||||||
package com.iluwatar.metamapping;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests that metadata mapping example runs without errors.
|
|
||||||
*/
|
|
||||||
class AppTest {
|
|
||||||
/**
|
|
||||||
* Issue: Add at least one assertion to this test case.
|
|
||||||
*
|
|
||||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
|
||||||
* throws an exception.
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
void shouldExecuteMetaMappingWithoutException() {
|
|
||||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
|
||||||
}
|
|
||||||
}
|
|
7
pom.xml
7
pom.xml
@ -75,7 +75,6 @@
|
|||||||
<license-maven-plugin.version>3.0</license-maven-plugin.version>
|
<license-maven-plugin.version>3.0</license-maven-plugin.version>
|
||||||
<urm-maven-plugin.version>1.4.8</urm-maven-plugin.version>
|
<urm-maven-plugin.version>1.4.8</urm-maven-plugin.version>
|
||||||
<commons-io.version>2.7</commons-io.version>
|
<commons-io.version>2.7</commons-io.version>
|
||||||
<istack-commons-runtime.version>4.0.1</istack-commons-runtime.version>
|
|
||||||
<!-- SonarCloud -->
|
<!-- SonarCloud -->
|
||||||
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
|
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
|
||||||
<sonar.organization>iluwatar</sonar.organization>
|
<sonar.organization>iluwatar</sonar.organization>
|
||||||
@ -228,7 +227,6 @@
|
|||||||
<module>lockable-object</module>
|
<module>lockable-object</module>
|
||||||
<module>fanout-fanin</module>
|
<module>fanout-fanin</module>
|
||||||
<module>domain-model</module>
|
<module>domain-model</module>
|
||||||
<module>metadata-mapping</module>
|
|
||||||
</modules>
|
</modules>
|
||||||
<repositories>
|
<repositories>
|
||||||
<repository>
|
<repository>
|
||||||
@ -379,11 +377,6 @@
|
|||||||
<artifactId>commons-io</artifactId>
|
<artifactId>commons-io</artifactId>
|
||||||
<version>${commons-io.version}</version>
|
<version>${commons-io.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>com.sun.istack</groupId>
|
|
||||||
<artifactId>istack-commons-runtime</artifactId>
|
|
||||||
<version>${istack-commons-runtime.version}</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
Reference in New Issue
Block a user