Skip to content

coodoo-io/coodoo-jpa-essentials

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

coodoo-jpa-essentials

Essential JPA entities and functionality

Table of Contents

Background

Every JPA entity needs an identifier and most of the time it is an auto increment number. Also in many cases you want to have the basic revision information stored for every entry. Instead of writing the same stuff over and over again you could just take this fields and functionality from abstract super classes. Enough said.

Install

Add the following dependency to your project (published on Maven Central):

<dependency>
    <groupId>io.coodoo</groupId>
    <artifactId>coodoo-jpa-essentials</artifactId>
    <version>1.2.0</version>
</dependency>

Usage

Following example will not only provide you an @Id annotated field, but also the revision field that will store the timestamps of creations and updates:

import io.coodoo.framework.jpa.entity.AbstractIdCreatedUpdatedAtEntity;

@Entity
public class SomeEntity extends AbstractIdCreatedUpdatedAtEntity {

    @Column
    private String something;

    @Column
    private int amount;

    // ...
}

API

All entities are annotated by the @MappedSuperclass, so all you need is to extend your entity by one of them.

Identification entity

Since it is the most used identifier all entities with Id in the name come with an auto incremental Long named id. This id annotated field uses the GeneratedValue strategy IDENTITY. It comes with hashCode() and equals(Object) methods based on the id field.

Optimistic concurrency control

Every entity with Occ in the name comes with an @Version annotated Integer named version as optimistic concurrency control value.

Revision entities

Entities with At in the name provide timestamp fields createdAt, updatedAt or deletedAt that will get updated automatically when the entity is created, updatedAt or deleted. Same game for entities with By in the name, they will add an user ID.

To provide the user ID (currently only type Long by the same reasons like the identifier) you have to implement the interface RevisionUser where ever you get your current users ID from.

For example:

import io.coodoo.framework.jpa.boundary.RevisionUser;

@Stateless
public class UserService implements RevisionUser {

    // ...

    @Override
    public Long getUserId() {
        return getCurrentUser().getId();
    }

    // ...
}

Fields

Field Name Type Column Description
ID id Long id Auto incremental identifier
Creation Date createdAt LocalDateTime created_at Timestamp of creation
Creation User createdBy Long created_by ID of user who triggered the creation
Update Date updatedAt LocalDateTime updated_at Timestamp of last update
Update User updatedBy Long updated_by ID of user who triggered the update
Deletion Date deletedAt LocalDateTime deleted_at Timestamp of deletion (just marked)
Deletion User deletedBy Long deleted_by ID of user who triggered the deletion
OCC version Integer version Optimistic concurrency control value

Entities

Entity ID OCC Creat. Date Creat. User Upd. Date Upd. User Del. Date Del. User
AbstractCreatedAtEntity
AbstractCreatedAtByEntity
AbstractCreatedUpdatedAtEntity
AbstractCreatedUpdatedAtByEntity
AbstractCreatedUpdatedDeletedAtEntity
AbstractCreatedUpdatedDeletedAtByEntity
AbstractIdCreatedAtEntity
AbstractIdCreatedAtByEntity
AbstractIdCreatedUpdatedAtEntity
AbstractIdCreatedUpdatedAtByEntity
AbstractIdCreatedUpdatedDeletedAtEntity
AbstractIdCreatedUpdatedDeletedAtByEntity
AbstractIdOccCreatedAtEntity
AbstractIdOccCreatedAtByEntity
AbstractIdOccCreatedUpdatedAtEntity
AbstractIdOccCreatedUpdatedAtByEntity
AbstractIdOccCreatedUpdatedDeletedAtEntity
AbstractIdOccCreatedUpdatedDeletedAtByEntity
AbstractOccCreatedAtEntity
AbstractOccCreatedAtByEntity
AbstractOccCreatedUpdatedAtEntity
AbstractOccCreatedUpdatedAtByEntity
AbstractOccCreatedUpdatedDeletedAtEntity
AbstractOccCreatedUpdatedDeletedAtByEntity
AbstractIdEntity
AbstractIdOccEntity
AbstractOccEntity
BaseEntity
RevisionDatesEntity
RevisionDatesDmEntity
RevisionEntity
RevisionDmEntity
BaseOccEntity
RevisionDatesOccEntity
RevisionDatesDmOccEntity
RevisionOccEntity
RevisionDmOccEntity

Entity = Deprecated

Configuration

To provide own configuration you need to add a property file named coodoo.jpa-essentials.properties to your project. This file gets read on JavaEE server startup if available or manually by calling JpaEssentialsConfig.loadProperties();

You can find a template here

Changelog

All release changes can be viewed on our changelog.

Maintainers

coodoo

Contribute

Pull requests and issues are welcome.

License

MIT © coodoo GmbH