I have followed Balusc's 1st method to create dynamic form from fields defined in database.
I can get field names and values of posted fields.
But I am confused about how to save values into database.
Should I precreate a table to hold values after creating form and
save values there manually (by forming SQL query manually)?
Should I convert name/value pairs to JSON objects
and save?
Should I create a simple table with id,name,value field and
save name/value pairs here (Like EAV Scheme)?
Or is there any way for persisting posted values into database?
Regards
It look like that you're trying to work bottom-up instead of top-down.
The dynamic form in the linked answer is intented to be reused among all existing tables without the need to manually create separate JSF CRUD forms on "hardcoded" Facelets files for every single table. You should already have a generic model available which contains information about all available columns in the particular DB table (which is Field in the linked answer). This information can be extracted dynamically and generically via JPA metadata information (how to do that in turn depends on the JPA provider used) or just via good 'ol JDBC ResultSetMetaData class once during application's startup.
If you really need to work bottom-up, then it gets trickier. Creating tables/columns during runtime is namely a very bad design (unless you intend to develop some kind of DB management tool like PhpMyAdmin or so, of course). Without the need to create tables/columns runtime, you should basically have 3 tables:
1 table which contains information about which "virtual" DB tables are all available.
1 table which contains information which columns one such "virtual" DB table has.
1 table which contains information which values one such column has.
Then you should link them together by FK relationships.
Related
Now and then I come into the situation that I have to display the table hierarchie of a database for further operations, currently in a data migration project where I have to treat "leaf tables" (tables which are leafes in the table dependency tree) in a different way.
I've always wanted to use Hibernate's meta information to retrieve and display the table dependency tree, but never knew how to approach the problem.
So can anyone give me feedback on whether Hibernate provides an API to do this? I am not asking for a complete solution, the information if there is an API and what it is called is absolutely sufficient.
I want to solve the following questions:
Which tables are in the database?
Is a given table a root table (not dependant from other tables)?
Is a given table a leaf table (dependant from other tables but no table is dependant from the given table)?
Which tables are dependant from the given table?
On which tables does the given table depend?
I know how to retrieve the mapping between entities and tables:
How to discover fully qualified table column from Hibernate MetadataSources , but I want the relationship between the tables.
In a custom MetadataContributor you can access metadataCollector.getDatabase() which exposes the full relational model to you. You just have so save that into a static volatile variable and then access it later on in your app to do whatever you want to do with it.
I am trying to figure out (and so far it seems I am out of luck), if it possible to refer (for association, for example) to a nested select from a different mapper/datasource ? Or may be some HashMap, which I could prepare manually beforehand..
What am I trying to achieve is basically the following:
say we have a database ONE, which has a table with a list of items. Every item has a field like "external_id"
we have also a database TWO, which is on different host and/or of a different driver type, which has a table with id; name pairs
And I would like to return a Mapped type which would contain fields from BOTH databases ONE and TWO.
Any ideas? So far I can only suggest constructing 2 POJOs independently, one with a returned ResultSet from db ONE and another with a list of id/name-pairs from db TWO and building the resulting object "by hands". But this is so boring...
We have a document management application and a part of it is having customized document forms for the customer to enter document metadata and save it to his database. To create these forms we have developed a 3rd party proprietary application which takes care of the design of the forms (where the fields go, what type they are etc) and then takes this design and creates dynamic tables to represent it in the database. Each form is represented by 3 tables:
Table_A contains information about the form's fields. This table has a standard format and each row in the table represents one of the form's fields.
Table_B contains information about the design. This table also has a standard format and each row contains information about each field's position when dynamically creating the form at runtime.
Table_C contains information about the metadata the user entered when he saved a document in the system. This table does NOT have a standard format. It is dynamically created by the 3rd party software and its columns are dynamic in both number and type. In one form column_A might be an integer and in another form column_A might be a date. One table might have 10 columns while another might have 70. It all depends on the form it represents.
The problem we have at the moment is that we want to add JPA to our project. However, in order to work JPA requires that all persistence models are defined in the xml or through annotations, which is impossible for us because our application doesn't know beforehand the design of Table_C for each form. When we want to retrieve data from that table we actually query Table_A, find out how many fields there are and then build a query dynamically according to the result and query Table_C for its metadata. This technique is impossible to execute in JPQL since it needs concrete objects to create queries. My question is, would it be possible to create persistence models dynamically using DatabaseMetaData and Java's Reflection API?
TL;DR: Is it possible to create JPA persistence models at runtime by reading DatabaseMetaData and using Java's Reflection API?
Recently I am working on a bilingual project and for some reference tables I need to sort the data. But because it is bilingual, the data is coming from different languages (in my case English and French) and I like to sort them all together, for example, Île comes before Inlet.
Ordinary Order By will put Île at the end of the list. I finally came up with using nativeQuery and sort the data using database engine's function (in oracle is about using NLS_SORT)
But I am tight with database engine and version, so for example if I change my database to postgres then the application will break. I was looking for native JPA solution (if exists) or any other solutions.
To archive this, without use native query JPA definition, I just can see two ways:
Create a DB view which includes escaped/translated columns based on DB functions. So, the DB differences will be on the create view sentence. You can define a OneToOne relation property to original entity.
Create extra column which stores the escaped values and sort by it. The application can perform the escape/translate before store data in DB using JPA Entity Listeners or in the persist/merge methods.
Good luck!
I have a hashmap with pair.
the key in this are column-names in a table Now I want to insert them into a table say users_table,
i should be able to match the key to the column names and if both are same then insert that value into table.
What I am doing is that i have to write preparedstatement with all the columns and then pass the hashmap values as parameter using setter methods of preparedsatatement.
For doing this i need to know all the columns of table and this would be tedious work as there would be no. columns and this step would be repeated to no.of tables.
tell me Any idea of doing this, Thanks in Advance
First off, use a LinkedHashMap to preserve the order of the columns. This will make a difference when iterating over the map to assign column names and then values.
I'm not entirely sure what you're asking, but you're hinting at what is called Object Relational Mapping (ORM). Simply put, it's a way to map database tables to plain old Java objects (POJO). Though there's a lot more to it than that.
If you're interested in representing your database tables as objects, you should look into Hibernate, which is a popular Java ORM API.
Otherwise, create and keep to a standard that is uniform across both your database and your Java project and you'll be fine.
Edit:
If I understand your question a little more, you're having issues with knowing the names of the columns? This is something you have to know, there's not going to be an easy, dynamic, or efficient way of getting that information.
One example of setting that information is storing the column names in a String array of a class that represents your table. You can then access the array and iterate over it when saving to a database.
And finally, if you feel like doing some reading, check out my answer (Store nested Pojo Objects as individuall Objects in Database). I go quite in-depth on how I manage Database to Java and vice versa.