6

The problem: I need to abstract my database interaction on my application. The thing is JPA doesn't work for me because the tables/schemas are dynamic and I only know them at runtime. I thought of creating the classes dynamically, but I'm looking at performance problems with that approach since every insertion would result in an instantiation of the class using reflection. I thought about using XML to model, but seems also slow and a bit difficult to manage.

What am I looking for? I'm looking for some middle layer language (something like JPQL [Java Persistence Query Language]) that I don't need to map to objects. Some language that abstracts schema/catalog and table creation as well queries using select clause.

Thanks in advance.

10
  • 1
    I think I'm not clear on the concept. You want to dynamically access tables and create queries with no prior knowledge of the database schema? That a real bad design smell, unless you're making a reverse enginering tool. Commented Mar 21, 2011 at 15:33
  • No, the thing is I need to create schemas and generate tables dynamically. So I can't know before hand the schemas and tables since they don't exist at the time. For that I would like to have an abstraction so if I change my underlying database I guarantee everything keeps working. Commented Mar 21, 2011 at 15:41
  • 1
    Is using a database a requirement, or are you just assuming that one will be used? Commented Mar 21, 2011 at 15:46
  • I'm looking specifically for databases. Is as simple as imagine JPQL but with no objects. Commented Mar 21, 2011 at 15:55
  • 2
    This takes me back to my earlier question: do you need to use a database? Why is the data being stored? A database is generally used for sharing data. To access the data, users need to know the schema. If you don't know the schema when you create the database, then no one else does either, so... what good is the database? Every time your application runs, you'll be creating a write-only database. Commented Mar 21, 2011 at 17:36

1 Answer 1

3

I'm pretty sure hibernate has an object-less mode, where entities are described using Maps. and, you can setup a hibernate factory at runtime if you so desire. i believe the combination of these features could do what you need.

that said, i worked on a similar system at my last job. we wanted a functionality layer which was abstracted from the data layer. the data layer wasn't "dynamic" per-se, it was just not known at compile time. we ended up building a system which loaded a configuration file which defined the db schema and could generate sql against that schema. we wanted a pretty high level of control over the resulting sql, so we ended up building our own sql building library, which we open sourced as SqlBuilder. SqlBuilder works best for generating queries using an in-memory db schema (which we built from the config file).

Sign up to request clarification or add additional context in comments.

7 Comments

I've only dabbled with Hibernate. Does it support DDL as well as DML? OP needs to create tables as well.
Well I saw that as well but it seemed to me that I need to define that mapping in the XML file.
@Dave - don't know. haven't actually used the dynamic functionality myself.
@user669634 - pretty much any solution is going to require you to configure it somehow. hibernate uses xml. wouldn't be surprised if other solutions also use xml. (btw, you don't need to use a file, you can create the xml DOM at runtime and pass it directly to the Configuration class).
SQLBuilder looks kind of neat. Do you have any other users?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.