Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/acdcjunior/domain-id
Domain Id Hibernate UserType implementation with Jackson serializers
https://github.com/acdcjunior/domain-id
Last synced: 26 days ago
JSON representation
Domain Id Hibernate UserType implementation with Jackson serializers
- Host: GitHub
- URL: https://github.com/acdcjunior/domain-id
- Owner: acdcjunior
- Created: 2019-05-02T05:04:27.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2022-04-18T21:50:53.000Z (over 2 years ago)
- Last Synced: 2024-04-15T02:05:21.818Z (7 months ago)
- Language: Java
- Homepage:
- Size: 280 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# domain-id
An implementation for Domain IDs, as suggested by Implementing Domain Driven Design.
This library includes:
- a `DomainId` base class;
- a hibernate `UserType`, which allows you to map the IDs directly into entities;
- a Jackson serializer and deserializer.Importing:
```groovy
dependencies {
implementation('io.github.acdcjunior:domain-id-all:0.2.0')
```
```xml
io.github.acdcjunior
domain-id-all
0.2.0
```**NOTE:** Although `domain-id-hibernate-usertype`'s code depends on hibernate and JPA, its `.jar` does not bring any dependencies.
That is **intentional**. Our purpose is not to force any specific Hibernate or Jackson (in the case of `domain-id-serializer-converter`)
minor versions.
You should, therefore, declare the dependencies as usual (in your `build.gradle` or `pom.xml`) and guarantee
everything works via at least one runtime test.
# Declaring
In order to use an ID, you must declare some classes first. Basically, you'll declare the ID and that's all.
The hibernate `UserType` is automatically registered.
The Jackson serializer/deserializer may be registered, if you'll need it.### The ID class:
```java
// src/main/java/com/myservice/domain/myentity/MyEntityId.java
package com.myservice.domain.myentity;import io.github.acdcjunior.domainid.DomainId;
public class MyEntityId extends DomainId {
public MyEntityId(long id) {
super(id);
}
}
```
An hibernate `UserType` will be automatically registered for that ID class.
#### Add, if desired, the Jackson serializer/deserializer```java
// src/main/java/com/myservice/MyServiceApplication.java
package com.myservice;// ...
import io.github.acdcjunior.domainid.DomainIdSerializer;@SpringBootApplication(scanBasePackages = "com.myservice", scanBasePackageClasses = DomainIdSerializer.class)
public class MyServiceApplication {
```
# UsageAfter the declarations above, use as follows:
# Non-ID field:
```java
@Entity
@Table(name = "MY_ENTITY", schema = "MYSERVICE")
public class MyEntity {// ...
@Column
private OtherEntityId otherEntity;
```#### Using as `@Id` and auto-generated using a Sequence:
```java
import io.github.acdcjunior.domainid.hibernate.sequence.DomainIdSequenceStyleGenerator;
import org.hibernate.annotations.GenericGenerator;import javax.persistence.*;
@Entity
@Table(name = "MY_ENTITY", schema = "MYSERVICE")
public class MyEntity {@Id
@GenericGenerator(
name = "SEQ_MY_ENTITY",
strategy = DomainIdSequenceStyleGenerator.SEQUENCE,
parameters = @org.hibernate.annotations.Parameter(name = "sequence_name", value = "MYSERVICE.SEQ_MY_ENTITY")
)
@GeneratedValue(generator = "SEQ_MY_ENTITY", strategy = GenerationType.SEQUENCE)
@Column
private MyEntityId id;
```
# Details
`@GenericGenerator` - Parameters:
- `sequence_name`: Sequence's full name, including the schema/owner name. Example: `MY_SCHEMA.MY_ENTITY_SEQ`.