Paul Jackson
Paul Jackson

Reputation: 2147

Spring Data API for query without using ORM

Is there an interface that is common across Spring Data backends that we can pass a JPQL query and get back a List<Map<String, Object> or similar generic data structure that can support nested hierarchical data?

My objective is to create an app that works with multiple NoSQL backends without prior knowledge of the schema of the tables/collections. Poking around I found MongoTemplate, MongoOperations, CassandraTemplate and CassandraOperations, but none of these implement a common interface.

The second half of my problem is I don't want to have to create DAOs for every table/collection because I don't have that knowledge until run time.

Do these requirements preclude the use of Spring Data?

Upvotes: 3

Views: 510

Answers (2)

edixon
edixon

Reputation: 1000

It doesn't makes sense to me to pass a JPQL query to get a generic object.

I'd say the point of JPA and ORM is to help with "know objects" (objects that the application actually uses). If your app works with "generic" objects it means that they are "used" only for display and nothing else.

You could build dynamic objects like Map<String, String> but to what point? If only for display then you'd better leave out the persistence and write a "database explorer" - an app that reads the schema at runtime and creates that hierarchical structure you mentioned.

If you have a JPQL query you would like to run, then you already have some persistent objects in your app and maybe you need to connect to a backend database that does match exactly your design. Or maybe you try to use JPQL as a "portable" SQL. In either case you need a dynamic mapping. You could use the mentioned "database explorer" to figure out what tables and columns need to be mapped, at runtime, to what "known objects".

You could write a generic DAO class with customized instances for each "known object", or you could rewrite the configuration files for EntityManager to get the correct mapping.

I hope this helps.

Upvotes: 2

crizzis
crizzis

Reputation: 10716

You should have a look at Hibernate OGM, it supports a number of NoSQL DBs, perhaps there's a way to combine it with Hibernate's dynamic models.

DataNucleus JDO seems to have much wider support for NoSQL than most JPA providers. However, I'm not sure if there is any support for dynamic schemas.

Eclipselink also seems to support some NoSQL DBs, and provides an API for dynamic entities; I wasn't able to find a decent piece of up-to-date documentation, though.

Upvotes: 2

Related Questions