Java Database API requiring no XML configuration - java

I am recently back in Javaland from Ruby and Activerecord and was wondering if there were any database solutions that do not require me to set up XML files to use them, and if possible supply any configuration in pure Java?

If you like ActiveRecords in Ruby, you might be interested in jOOQ (Java Object Oriented Querying, a database abstraction library that I wrote). jOOQ requires only little configuration for its source code generator. Your database schema is mapped 1:1 to Java classes, which can then be used in a fluent API very similar to SQL itself.
Also, jOOQ doesn't manage transactions, sessions, caches, etc like JPA or Hibernate might do. So there is no additional runtime configuration required.
http://www.jooq.org

You can configure Hibernate without using any XML via the Configuration class (it doesn't have to read XML documents). It's easier to avoid the mapping of .hbm.xml files if you're using annotated classes and AnnotationConfiguration instead.
See this for more details: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-programmatic

There are many. Though hibernate is the most famous one. It has annotation for most of the configuration and that too is not required, if you use naming strategies.
http://www.hibernate.org/

Related

How jpa achieves schema independence

I am a beginner to jpa and hibernate, I understood that how jpa achieves database independence and persistence provider independence.
I came across the sentence which states
jpa can achieve schema independence
how does jpa achieves this?
JPA is an API, this API is completely abstracted from your data representation and DDL. Also, it is abstracted from the API implementation (unlike going directly for Hibernate).
This means that you can:
Use differing JPA implementations such as Hibernate or Toplink
Use different DB dialects such as Oracle, T-SQL, PostgreSQL
Implement your own JPA implementation
These can be done through annotations AND/OR configuration. So in theory you can switch from Oracle to PostgreSQL without having to recompile your application.
This also means you can do other funky things like:
Use a free, Open Source DB for development, and then Oracle/SQL Server for Production
Use Hypersonic for automated tests, so everything is in memory and torn down automatically.
Use other data-sources such as XML transparently.
You get other niceties like SQL injection protection out of the box, very quick startup time (rather than having to code everything twice), automatic DDL generation when you've defined your entities etc. which get small projects started quicker than the traditional route.
IN REALITY:
Nobody chooses to go from Hibernate to Toplink
Very few people ever implement JPA for a project
Very few people rapidly switch DB backends as each Database performs very differently
Also, the differing dialects can drive out weird bugs.
The configuration aspects and the ability to use things like Hypersonic are useful though.
The other "flip side" to the lovely API abstraction is that the implementation can be too constraining, resulting in very inefficient SQL being produced. This ends up with the developer having to add in native SQL anyway.
Schema independence can be achieved in the same way as DB and provider independence - it is possible to change the schema of all tables simply in configuration. You can change default schema in an orm.xml file included in persistence.xml via persistence-unit-defaults.
You may also specify schema via #Table annotations.

Autogenerating Hibernate annotations from XSD

I have been using jaxb to autogenerate Java classes from XSD files. I am seeking a list the various ways that I can also autogenerate the Hibernate annotations for persisting the data from the classes into a relational database. If the Hibernate annotations are autogenerated, then creating the database structures should be automatic using hbm2ddl.
I need to import XML data into a relational structure which can then be used by other apps. I have been experimenting with hyperjaxb, and I may stick with it, but there seem to be a lot of picky settings, for example its default settings can result in cripplingly verbose SQL statements. See this posting and this other posting.
I have read about Dali and EclipseLink, but it seems that neither of these other tools use Hibernate. I would like to stick with Hibernate. What are the options?

Why ormlite has its own annotations?

From what I see in ormlite it has implemented its annotations as well as JPA standard annotations. First of all, what was the reason of designing new set of annotations?
Secondly, how one can use standard annotation like #Entity, etc instead of ormlite specific annotations. Right now, I am getting not defined error for those entities. Do I need a jar file?
#DataNucleus is correct. ORMLite is not a fully compliant JPA implementation. There are many features of ORMLite that do not map well with the JPA annotations and it was easier to create my own set. JPA is also a very large specification and I didn't want a large percentage of the annotations to generate UnsupportedOperationException or jut fail quietly. Lastly, I was trying to write a ORM library with 0 dependencies.
All that said, I am interested in improving ORMLite's JPA compatibility so if you have any suggestions on how to make it better, please send them to the developers mailing list. I'd love to improve it.
Because it isn't a real JPA implementation, and just makes use of JPA annotations for convenience. Obviously, by using it, you lose the portability offered by JPA itself, but then it may have some advantages for very specific situations

Easy Java ORM for small projects

I'm currently searching for a really easy way to get simple Java Objects persistent in Databases and/or XML and/or other types of data stores.
For big projects in the company i would use hibernate, ibatis, datanucleus or something like that. But with small private projects this will take over 80% of the worktime.
I also found "simpleORM" but this one requires to code data-related stuff pretty hardly into the data-model classes. I don't really like that style so this is no option for me.
Do you have a suggestion for some library which simply takes my objects and saves / loads them as they are or with very little configuration?
You could try my ORMLite library, which was designed as a simple replacement for hibernate and iBatis. I'm the main author. It supports a number of JDBC databases and has an Android backend. Here is the getting started section of the manual which has some code examples. Here also are working examples of simple usage patterns.
Try Norm. It's a lightweight layer over JDBC. It adds almost zero overhead to JDBC calls and is very easy to learn.
You could just serialize your objects into a file/database whatsoever.
If you want to define the mapping then you'd have to go for more configuration and the standard OR mappers out there (like Hibernate) don't really add that much on top.
You could try xstream. It's really simple OXM library working without upfront configuration.
Sample code:
XStream xstream = new XStream();
// marshalling
String xml = xstream.toXML(domainObject);
// unmarshalling
domainObject = xstream.fromXML(xml);
For relational database persistence try one of the JPA implementations, such as OpenJPA.
The setup overhead is minimal. You can let JPA to create your schema & tables for your from your object definitions, so you don't need to hand crank any sql. All you need to supply is some annotations on your entities and a single config file, persistence.xml.
You can also use jEasyORM (http://jeasyorm.sourceforge.net/).
In most cases it automatically maps objects to database tables with no need for configuration.
You may want to consider www.sormula.org. Minimal programming/annotations and simple learning curve. It uses standard SQL and JDBC so will work with any relational db.
U could try SnakeORM http://sourceforge.net/p/selibs/wiki/Home/
It doesnt have many runtime dependencies, uses JPA annotations and follows DAO pattern.
Disclosure: I am the author of this project
Well if you want an ORM, then that implies that you want to map objects to tables, columns to fields etc. In this case, if you want to avoid the hassle of bigger ORM implementations, you could just use plain old JDBC, with simple DataAccessor patterns. But then this does not translated to XML directly.
If you want to just persist the object somewhere, and only care about "understanding" the object in Java, then serialization is a simple effective method, as Thomas mentioned earlier.
You could also try my little ORM library, Java2DB. I created it specifically for small projects that just want quick and easy access to their database. Check it out on GitHub.
Onyx Database is a very feature rich Java NoSQL database alternative. It's pure java with several persisting modes (caching, embedded-database, save-to-remote, and save-to-remote-cluster. It has an embedded ORM, and is probably the easiest persistence API I've used.

Auto-Generating EJB3 Entity Beans

I would like to know if there are any tools to automatically generate EJB3 Entity Beans(for JPA) from a database schema.
Thanks.
Dali supports top-down, bottom-up (this is what you're looking for), and meet-in-the-middle development approaches.
Some of IDs have such feature, for example NetBeans
OpenJPA has a tool which will generate your Entity definitions.
From the OpenJPA user manual:
OpenJPA includes a reverse mapping tool for generating persistent class definitions, complete with metadata, from an existing database schema. You do not have to use the reverse mapping tool to access an existing schema; you are free to write your classes and mappings yourself, as described in Section 3, “ Meet-in-the-Middle Mapping ”. The reverse mapping tool, however, can give you an excellent starting point from which to grow your persistent classes.
No IDE required!
-Rick
I have a solution for you i.e to create auto generate domain objects with all table relationship properly mapped in class ...Try Dal4j yes you can find it in sourceforge.net/p/dal4j/wiki/ DAL4j is a Command Line and Framework tool that can be used to reverse engineer a MySQL or SQLServer database schema into a set of JPA Entity Beans.
DAL4j can be useful for scenarios where there is an existing database schema but a technology other that JPA is used by applications to interact with the database. DAL4j can provide an easy way to migrate your code base from other technologies such as JDBC or Hibernate to JPA.
The beans generated can be 1 or two types: Simple or Framework. Simple beans are standard pojo classes managed by your application using JPA semantics. Framework generated pojos use the DAL4j framework DAO generic to simplify CRUD operations.
DAL4j provides optional hooks to allow you integrate encryption/decryption of data fields that must be encrypted in the database.
Last, DAL4j provides a set of Generic classes that can be used to simplify creation of Session Beans which perform CRUD operations using generated Entities.
I think you will find this article feasible....

Categories

Resources