Coder Social home page Coder Social logo

mariodavid / cuba-component-entity-soft-reference Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 5.0 265 KB

CUBA component that allows reference any entities with soft references

License: Apache License 2.0

Groovy 40.80% Java 58.58% Shell 0.61%
cuba-platform cuba-component foreign-keys soft-references entity database

cuba-component-entity-soft-reference's Introduction

Download license Build Status

CUBA Platform Component - Soft References for Entities

This application component can be used for referencing entities without a foreign key in the database.

What is a soft reference?

A soft reference is a reference in the data model, which does not create a foreign key in the database as well as not creating referential integritry for it. Instead it is just a "weak" or "soft" reference to another entity.

Example: Customer - Order

Let's take the Customer <>--> Order example. A customer can have multiple orders. We model that as an composition in CUBA. This means, that there will be a column "customer_id" in the Order table, which stores the primary key of the customer in the Order Table. For every Order, there is a Customer associated to it.

In the soft reference example, it will be different. There will be a "customer_id" column as well in the Order table. But instead there will be no foreign key association on the database. Instead the following String will be stored in this soft reference column: example$Customer-2fdc4906-fa89-11e7-8c3f-9a214cf093ae.

Foreign keys are valuable, why would someone not want to have them?

Correct. For pretty much every case, there is no need to use soft references. Since it misses all the cool features of referential integrity, there are a lot of downsides. Why do we still need them? Here is an example:

Example: Customer - Order - Comment

Let's assume we want to have a column called "Comment". Comments can be created for a lot of different entities. There can be comments for customers as well as comments for orders, products etc. The common way to handle that in a programming language like Java is that you would create an interface called Commentable. This interface marks all classes that want to be commentable as such. Then the Comment class could have a reference to a Commentable object. This is what polymorphism is all about.

However - there is no native equivalent in a relational database. The reason is that when you want to create a foreign key, you need to point it to a destination table. But there is no generic table that you could point to. Instead, you want to point to totally different tables.

For cross cutting functionality, these soft references can be the solution here.

Installation

  1. entity-soft-reference is available in the CUBA marketplace
  2. Select a version of the add-on which is compatible with the platform version used in your project:
Platform Version Add-on Version
7.2.x 0.7.x
7.1.x 0.6.x
7.0.x 0.5.x
6.10.x 0.4.x
6.9.x 0.3.x
6.8.x 0.1.x - 0.2.x

The latest version is: Download

Add custom application component to your project:

  • Artifact group: de.diedavids.cuba.entitysoftreference
  • Artifact name: entity-soft-reference-global
  • Version: add-on version

Example usage

To see this application component in action, check out this example: cuba-example-using-entity-soft-reference.

Usage

Soft references as Table columns

In order to use a soft reference as a Table column, there is a class de.diedavids.cuba.entitysoftreference.web.SoftReferenceInstanceNameTableColumnGenerator which can be used to create a column which renders the soft reference as a link with the instance name into the table.

Usage:

@Subscribe
protected void onInit(InitEvent event) {
    myTable.addGeneratedColumn("softReferenceColumnId",
        new SoftReferenceInstanceNameTableColumnGenerator(
            "softReferenceColumnName",
            uiComponents,
            metadataTools,
            screenBuilders,
            this
        )
    );
}

Soft references as Form fields

With CUBA 7 the form components is the new standard way of rendering forms. In order to render a soft reference into a form, there is a Spring bean de.diedavids.cuba.entitysoftreference.web.SoftReferenceFormFieldGenerator which can be utilized.

Usage (in an editor screen):

@Inject
private SoftReferenceFormFieldGenerator softReferenceFormFieldGenerator;

@Subscribe
protected void onInit(InitEvent event) {
    softReferenceFormFieldGenerator.initSoftReferenceFormField(form, myFormInstanceContainer, "mySoftReferenceProperty");
}

cuba-component-entity-soft-reference's People

Contributors

mariodavid avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cuba-component-entity-soft-reference's Issues

Transaction in try and removal of some deprecation.

The start of the transaction was not in a try with resource block, so in case of exception the connection was not cleanly rolled back and closed.
Deprecated method calls have been replaced by non deprecated methods.
The call to setParameter with 3 parameters has been replaced by the one with 2 parameters (as the implicit Conversion false is the default value of ServerConfig).

Execution failed for task ':app-web:deploy'.

Platform: 7.1.1 (new clean project)
Add-om: 0.6.1

I got an error while starting app:

Execution failed for task ':app-web:deploy'.
Could not resolve all files for configuration ':app-web:webcontent'.
Could not find entity-soft-reference-web-web.zip (de.diedavids.cuba.entitysoftreference:entity-soft-reference-web:0.6.1).

Editor field enhancement

Hello. Very nice component!

Is it possible to enhance filed in editor screen?
My vision:
For parent can be used LookupField with actions (open, lookup, clear). When clicked on lookup action, then openes entity class selection screen. After choosing entity class, openes entity instance selection screen. Also ability to specify list of entity classes needed (maybe in attribute annotation).

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.