Hibernate with JSON (no DB table) - json

I am stuck with a peculiar scenario on JSON / Hibernate. Here goes -
I have an existing application with Oracle DB and Hibernate / JPA. Now, I need to use JSON data rather than DB table data for some of the domain / entity classes while keeping the hibernate framework intact. This is required because I still need to use the existing hibernate / Oracle interaction for the rest of the domain classes. Once I replace the DB table data with JSON, those DB tables will no longer be available to the application. How can I achieve this?
Thank you in advance.
EDIT:
Thanks Vlad for you input. I went through the article. But I think my scenario is a little different. Let's say I have two entities - Country and City which are currently mapped to the DB tables COUNTRY and CITY respectively. City has a Country. Now, I want to remove the DB table COUNTRY and instead provide JSON data to the application. How can I handle that while still enjoying Hibernate services like caching etc even for Country.

Use the Hibernate Types project which allows you to persist JSON properties as:
List
Map
JsonNode
POJOs
For a detailed example, check out this article.

Related

How to structure database of an application that will utilize multiple datasources for its models?

I'm creating an application that will use at minimum two different API for data of its models. For example, I'll have an entity / model of a person. The data for this person comes at minimum two different API and more may be relevant in the future. For example, email and phonenumber may come from one data source and first name and second name from another.
How would one structure an database for this, that is flexible enough to handle adding / removing of the "data sources".
I initially though of an database structure looking like this:
[Person]
- ID
- FirstName
- LastName
- Email
- PhoneNumber
[Person_FIRST_SOURCE]
- Person_ID
- Person_FIRST_SOURCE_ID
[Person_SECOND_SOURCE]
- Person_ID
- Person_SECOND_SOURCE_ID
Which I then would be able to in the data synchronization script to look up what "Person" entity in my database, the current iterating person from the source represents. This would allow me to easily connect/add more sources as I go.
EDIT: I would like to add that it's not always that Person A exist within both sources of data.
EDIT 2: The main question is not really dependent on the "Person" model. This could have been any type of model; Book, School, Company etc. Just some form of model, where its data comes from multiple sources.

STS JPA Tools not generating all Entities from Tables

I have created a few tables in my DB and a demo project in STS which I have converted to a JPA project. I then right click on it and select JPA Tools -> Generate Entities from Tables.
It works perfectly, except that it does not generate all Entities from the Tables. I have added two new tables and can see the in the Data Source Explorer, but when doing the generation, entities for those two does not get generated.
I have had this problem before, it's almost as if STS is keeping cache or something from a previous generation and now doing only that, but I am not sure where to look or how to clean it.
Any help please.
Got it working,
For those of you interested, I had to select each table one by one and generate the Entity. STS does not like it when you select all tables. Just select one and generate the entity, then repeat the process for the next one, etc, etc.

JSF retrieving data from multiple tables

I'm trying to show data from multiple tables on a jsf page.
The page is bound with the backing bean.
Now, there are many ways to do it.
Method 1: I can get the entire table_row on the entity object generated by hibernate using the entity objects.
Method 2: I can create POJO's (Value beans) which hold the data and write queries myself in HQL.
NOTE : The data is coming from multiple tables.
Some people suggested the first method and some suggested the second.
Now the question is
When i need specific columns of specific tables, why do i retrieve a whole row from the db but on the other hand a thought comes that what are the entities for, then ?
I hope i explained my problem well.
Please guide.
Personally, I would go with the second method.Just have the backing beans call a DAO method to populate a POJO instance created in the bean. If a lot of the data is not needed. This way you can query only the data you needed, but if you ever thought in the future some of the other data will be used you would already have the backend coded to retrieve it in the entity class where with the pojo you would have to change the DAO, POJO, and JSF.

Correct foreigen key approach in jpa Or data saving basic approach

This question of mine is subjective
i am getting a list of objects from a third site.
now i want to save that data in database.
suppose the data is List. This response is to a query that i fired to that site .
now i want to save two things
1) query name
2) the response(List) (answer)
the myobject can have lot of answers corresponding to my query. now i want to save all these answers separately so that each answer can be fetched independently.
now i have this DB approach
one table for query and query id
second table which will consist of query id and query answer. (which will be foreigen key in first table
My question is am i following right approach?
initially i thought of saving the whole list in database but as per my knowledge we can not save list in database directly although in jpa implementation 2.0 we can save list in db (correct me if i am wrong)
please guide me with my current approach or of there is any better approach
i am using JPA 2.0 eclipselink.
Regards
Anil Sharma
What is your object model?
You can use OneToMany or ManyToMany to store a collection of Entity objects.
If you have a List or List you can store this using an ElementCollection.
But you may be better off creating an Answer or AnswerReference Entity.
See,
http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection

SQL - adding fields to query to sorty by

I'm working with a third party software package that is on it's own database. We are using it for the user management back bone on our application. We have an API to retrieve data and access info.
Due to the nature of information changing daily, we can only use the user_id as a pseudo FK in our application, not storing info like their username or name. The user information can change (like person name...don't ask).
What I need to do is sort and filter (paging results) one of my queries by the person's name, not the user_id we have. I'm able to get an array of the user info before hand. Would my best bet be creating a temporary table that adds an additional field, and then sorts by that?
Using MySQL for the database.
You could adapt the stored procedure on this page here to suit your needs the stored procedure is a multi purpose one and is very dynamic, but you could alter it to suit your needs for filtering the person table.
http://weblogs.asp.net/pwilson/archive/2003/10/10/31456.aspx
You could combine the data into an array of objects, then sort the array.
Yes, but you should consider specifically where you will make the temporary table. If you do it in your web application then your web server is stuck allocating memory for your entire table, which may be horrible for performance. On the other hand, it may be easier to just load all your objects and sort them as suggested by eschneider.
If you have the user_id as a parameter, you can create a user defined function which retrieves the username for you within the stored procedure.
Database is on different servers. For all purposes, we access it via an API and the data is then turned into an array.
For now, I've implemented the solution using LINQ to filter and out the array of objects.
Thanks for the tips and helping me go in the right direction.