create fixture using hibernate(not import.sql method) - java

Is there a way to use java code and those hibernate entity classes to create fixture data.
I find using import.sql is not good when it comes to database migration

Since you are using hibernate, the way to create sample data is to use hibernate itself to create it. You have two options:
generate random data - look from 1 to X, instantiate entities and set random values, then save each entity
predefined data - use some meta format, like .properties, .json or .xml to define field=value pairs, parse that file, instantiate entities for each record, set the fields, and save it.
That way your data generation is tied to your entity model, and when the model changes, your data won't become out-of-sync.

Related

How to create Sequence of Tables dynamically in hibernate

I have create log tables generated daily, which has massive amount of data. If I have add data in single table, then the size of table will be very big.
Is there a way to create sequence of tables automatically depending on the day to store logs in hibernate and dynamically bind them to class.
Example - Log_20180101, Log_20180102, ....
Edit: l don't generate these logs. These are web access logs generated from another server. I want to store valid log from it and want analyse it.
It is probably not a good idea to use hibernate for this usecase nor is it a good idea to use RDBMS for this usecase.
You may want to look at log4j jdbc appender if you want to go with RDBMS.
log4j JDBCAppender rotate table name
You will have to create the tables yourself. Hibernate cannot do such mapping.

Java Persist without Entity

I am working on an JavaEE application, and there are almost 1000+ tables in the database, now I have to query the records by the parametes from the client.
Generally I will create one Entity for each table, and create the Dao,Service to do the query.
However I meet two problems:
1 Number of the tables
As I said, 1000+ table with almost 40+ columns for each, it would a nightmare to create the entity one by one.
2 Scheme update
Even I can create the Entity by program, the schema of the data may change sometime which is out of my control.
And in my application, only read operations are related to these kinds of data,no update,delete,create required.
So I wonder if the following solution is possible:
1 Use Map instead of POJOs
Do not create POJOs at all, use the native Map to wrap the columns and values.
2 Row mapping
When querying using Hibernate or Spring JdbcTemplate or something else, use a mapper to map each row to an entry in the map.
If yes, I would use the ResultMetaData to detect the column name,type,value:
ResultMetaData rmd=rs.getMetaData();
for(int i=0;i<rmd.getColumnCount();i++){
Type t=rmd.getType(i)
if(t==....){
...
}else if(t=...){
...
}
}
Looks like part of JPA's job, any library can used here?
If not, any other alternatives?

JPA Multilingual sorting

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!

Conversion of Java Objects to XML for OptaPlanner Input

I need to convert java objects being imported from the dB to the XML so that I could user it with Xstream in OptaPlanner. Is there any alternate way other than Hibernate to access the data from the dB. How to add more attributes for job scheduling.
optaplanner-core works based on POJO's (javabeans). It's oblivious to the fact that in optaplanner-examples, those POJO's are being read/written to XML files by XStream (and it doesn't care). Similarly, you can use any other technology to store those POJO's:
JPA (for example Hibernate-ORM, OpenJPA, ...) to store them into a database
JDBC to store them into a database. Note: JDBC works with SQL statements, so you 'll need to manually map SQL records to POJO's.
JAXB to store them in XML
XStream to store them in XML (as the examples do it)
Infinispan, mongodb, ... to store them in to a big data cloud. Note: might require manually mapping too, unless you use hibernate-ogm
...
OptaPlanner doesn't care, so it doesn't restrict you :)

How to accomplish CRUD operations on dynamic forms?

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.

Categories

Resources