How would you solve this in Java? Any design pattern for referencing to unknown data?
My system needs to interface with a couple of other systems, and possibly more in the future. The web application has an internal data model suitable for its purpose, however when displaying this data the web app will need to import or display data from other systems. Specified by user. Im using EJB3 and the problem seems to be how to load unknown tables and display their contents in a nice formatted way?
Lets say you have an Entity class like
@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 dont know right now how the data looks like. How to do this?
Is there a totally different way of doing this? When your entity needs to reference an unknown table?
Does the adapter pattern suit for this?
EDIT: I probably could use openjpa to do a reverse-mapping of the external systems database schema to create a data class or entity object. However I am unsure if I can do this during runtime, wont the entity bean need to be recognized/deployed by the AS as well? If that would work, a hacky way would be, well, to provide a button to fetch the table and revers-map it to an entity-bean, then reload-it in the AS. But, thats so ug开发者_C百科ly...
EDIT2: Would groovy be suitable for something like this? I heard it is a dynamic language.
JPA is unsuitable for that, as what you need seems to be fairly dynamic. I would use JDBC directly, which gives you access to the metadata of the schema and a generic representation of your results as RowSets.
One possible option;
Define a class that holds information about one column of a table
class ColumnMetadata{
String columnName;
String columnValue;
String (maybe enum) dataType;
int length;
.
.
}
and for the whole row data; you can keep a List<ColumnMetadata>
representing one row data of the unkown table.
You can add more properties to ColumnMetada class if you need any other specific thins as well.
When you get unknown table data you will need to implement a logic (using reflection) that will convert it to this type of data. And you can use this List representing one row data in your app.
What you are trying to do is extremely complicated. If you had a set number of external systems, you could design your system with that in mind, and create entities for the external systems' data.
In your case, where you are going to have to add systems dynamically, it is not a good idea to store their data. One of the underlying assumptions of systems that use an RDBMS is that the data model is well defined and understood. If you don't understand the relationships up front, its hard to design a data model around the data.
Another thing to consider when using an external system -- what happens if they change their API?
精彩评论