How would you solve this in Java? Any design pattern for referencing unknown data?

My system should interact with several other systems, and possibly in the future. The web application has an internal data model suitable for its purpose; however, when displaying this data, the web application will need to import or display data from other systems. Specified by user. I am using EJB3, and the problem is how to load unknown tables and display their contents in a good formatted format?

Suppose you have an Entity class, for example

@Entity public class myDatas() { private String field; private Other data; // getters and setters } public interface Other<T> { public T getOtherData(); 

T needs to be dynamically created as a data class, since I don’t know what the data looks like. How to do it?

Is there a completely different way to do this? When should your object reference an unknown table?

Does the adapter template match for this?

EDIT: I could probably use openjpa to reverse-map the schema of external system databases to create a data class or object object. However, I am not sure if I can do this at runtime, is there no need to recognize / deploy the bean entity for AS? If this works, the hacker way would be, for example, to provide a button to retrieve the table and unbind it to the entity - bean, and then reload it in AS. But, it is so ugly ...

EDIT2 : Could groovy be suitable for something like that? I heard that it is a dynamic language.

+4
source share
3 answers

JPA is not suitable for this, because what you need seems pretty dynamic. I would use JDBC directly, which gives you access to schema metadata and a general view of your results as RowSets.

+3
source

One of the possible options;

Define a class containing information about one column of a table

 class ColumnMetadata{ String columnName; String columnValue; String (maybe enum) dataType; int length; . . } 

and for all row data; you can save a List<ColumnMetadata> representing a single row of data from an unkown table.

You can add additional properties to the ColumnMetada class if you need other specific thin parts.

When you receive unknown table data, you will need to implement a logic (using reflection) that converts it to this data type. And you can use this list representing a single row of data in your application.

+1
source

What you are trying to do is extremely difficult. If you had many external systems, you could design your system with this in mind and create entities for these external systems.

In your case, when you have to dynamically add systems, storing their data is not recommended. One of the main assumptions of systems using DBMSs is that the data model is well defined and understood. If you do not understand the relationship ahead, it is difficult to construct a data model around the data.

Another thing to consider when using an external system is what happens if they change their API?

+1
source

Source: https://habr.com/ru/post/1336165/


All Articles