Giter Site home page Giter Site logo

mom0aut / dynamicrelations Goto Github PK

View Code? Open in Web Editor NEW
14.0 3.0 12.0 186 KB

Dynamic Relations for Java Spring Projects

License: Apache License 2.0

Java 100.00%
java spring-boot springboot sql database dynamic-relations relationship open-source opensource spring good-first-issue dynamic relations hibernate hibernate-jpa hibernate-orm

dynamicrelations's Introduction

Dynamic Relations

Java CI with Maven Coverage

In every relational database you must always know which relations are possible to your entities. But sometimes these relations are unknown or could change anytime. With Dynamic Relations you can add or delete custom relations between entities during runtime.

What is a Dynamic Relation?

A dynamic relation can be viewed as a directed graph with a fixed input(SourceObject) and a dynamic output(target).

flowchart LR
    subgraph DynamicRelation
    direction LR
    SourceObject-->Target
    end

For example with following entities:

  • Person
  • Dog
  • Document

A person can have a dog and both entites could have documents(person info documents and dog info documents). Now you could add dynamic relations to all entities which could look like this:

graph TD;
    Person-->Dog;
    Person-->Person_Document
    Dog-->Dog_Document;

Each connection is a dynamic relation and following relations will be generated:

  • Person Relation with SourceObject Person
  • Person_Document Relation with SourceObject Person_Document
  • Dog Relation with SourceObject Dog
  • Dog_Document Relation with SourceObject Dog_Document

Each relation got a dynamic target, that means you could create a relation to any other entity.

In this scenario a person have a dog and both got documents, now you could change the relation during runtime (no altering of your Entities or Models). For example, you could delete a Person_Document(got lost):

graph TD;
    Person-->Dog;
    Dog-->Dog_Document;

Maven dependency

<dependency>
  <groupId>io.github.Mom0aut</groupId>
  <artifactId>dynamic-relations</artifactId>
  <version>1.0.4</version>
</dependency>

How to use

Add the @Relation

Simply add the @Relation to your existing entity and the necessary dynamic relations entity will be generated. Dynamic relations are only working with classed which are annotated with @Entity!

@Relation(sourceClass = Person.class)
@Entity
@Getter
@Setter
public class Person implements RelationIdentity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Override
    public String getType() {
        return "PersonType";
    }
}

Implement RelationIdentity

Implement the relationIdentity, each dynamic relation need a Long id and a String Type which you can define.

@Relation(sourceClass = Person.class)
@Entity
@Getter
@Setter
public class Person implements RelationIdentity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Override
    public String getType() {
        return "PersonType";
    }
}

Import Config Module for Component Scan

Import the DrmConfig in your Spring Boot Application, so that you can use the RelationService

@SpringBootApplication
@Import(DrmConfig.class)
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

Use the RelationService

Create Relation

@Autowired 
private RelationService relationService;

        void createRelation() {
            Person person = new person();
            personDao.save(person);
    
            Dog dog = new Dog();
            dogDao.save(dog);
    
            //Dynamic Relation can only be created with persisted Entities!
            RelationLink relationLinkPersonToDog=relationService.createRelation(person, dog);
        }

Dynamic relation can only be created with persisted Entities!

Delete Relation

@Autowired 
private RelationService relationService;

        void deleteRelation() {
            relationService.deleteRelation(relationToBeDeleted);
        }

Find Relations

@Autowired 
private RelationService relationService;

        void findRelations() {
            Person person = new person();
            personDao.save(person);
    
            Dog dog = new Dog();
            dogDao.save(dog);
    
            Document document = new Document();
            documentDaio.save(document);
    
            //Dynamic Relation can only be created with persisted Entities!
            RelationLink relationLinkPersonToDog = relationService.createRelation(person, dog);
            RelationLink relationLinkPersonToDocument = relationService.createRelation(person, document);
            RelationLink relationLinkDogToDocument = relationService.createRelation(dog, document);
    
            //Return 1 Relation person -> dog
            RelationLink foundRelation = relationService.findRelationBySourceObjectAndRelationIdentity(person, dog);
            //Returns 2 Relations person -> dog and person -> document
            List<RelationLink> relationBySourcePerson = relationService.findRelationBySourceObject(person);
            //Returns 2 Relations from person -> document and dog -> document
            Set<RelationLink> relationByTargetDocument = relationService.findRelationByTargetRelationIdentity(document);
        }

Get the SourceObject by Relation

@Autowired 
private RelationService relationService;

        void getSourceObject() {
            RelationLink foundRelation = relationService.findRelationBySourceObjectAndRelationIdentity(person, dog);
            //Can be cast to Person because we know it is from Person.class
            Person sourceObject = (Person) foundRelation.getSourceObject();
        }

Limitations

  • Java EE with Spring
  • Sql Database (tested with Postgres)

Contributors

Contribution

Every contribution is welcome, please follow the Contribution Guidelines

Code of Conduct

See our Code of Conduct

dynamicrelations's People

Contributors

cerrussell avatar dependabot[bot] avatar ganga-jpg avatar github-actions[bot] avatar mom0aut avatar neha121 avatar thibstars avatar vichukano avatar yan-3005 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

dynamicrelations's Issues

Add configuration for the flyway-maven-plugin

It would be nice to execute the flyway goals (info, clean, migrate) against a local database. Therefore the plugin needs an configuration.

Example:

<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>${flyway-maven-plugin.version}</version>
<configuration>
  <user>localUser</user>
  <password>localPw</password>
  <url>localUrl/</url>
  <schemas>localSchema</schemas>
</configuration>

How to test:

Submodule testing mvn flyway:info

Add Dependency Management into Root pom.xml

Currently the the versions of the dependencies are defined in each sub module. It would be convenient to define all dependencies in the root pom via the dependency management tag and use them in the matching sub module. With this change u can see all the version of the used dependencies from the whole project at once.

Example:

Root pom.xml:

 <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgresql.version}</version>
 </dependency>

 <dependencyManagement>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
 </dependencyManagement>

Sub Module using it:

 <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
 </dependency>

Extract maven dependency versions into properties

Currently the dependencies in the pom.xml are direct under the dependency it would be convenient to have them all under some pom properties.

Example: <spring.boot.version->3.0.0<\spring.boot.version>

Extract jacoco version into pom property

The jacoco-maven-plugin version from the module jacoco-report should be extracted into a pom property.

Currently:

<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>

Should:

<properties>
        <jacoco.version>0.8.8</code.coverage.project.folder>        
 </properties>

<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>

Simplify the usage of the framwork

Currently there is a lot of configuration needed to get the DynamicRelations working (DrmConfig,RealtionType)
It should be possible to streamline the usage of the framework to a simpler process.
Any suggestions are welcome

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.