Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
Replies: 24 - Pages: 2   [ 1 2 | Next ]
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

ActiveObjects: An Easier Java ORM

URL: ActiveObjects: An Easier Java ORM

At 5:32 PM on Sep 13, 2007, Daniel Spiewak wrote:

I've written a full-length Javalobby spotlight article running through the basics of the ActiveObjects ORM. In this article, I've gone into a bit more depth regarding the decisions which went into the API design, as well as the reasoning behind yet another Java ORM.

As a side note, I've recently pushed out the version 0.5 release of the framework. It now has a full test suite, as well as some marginally useful javadocing. It's still pre-release, so the public API could potentially change, but at this point the focus has shifted more towards stability (hence the test suite) and documenting.

Another significant goal is benchmarking and improving performance. Since ActiveObjects is an implementation of the Active Record pattern, it's inherently less performant than the relational mapping matter implementations (such as Hibernate) for most scenarios. As such, the framework has to be a bit smarter about how it determines caching and what queries are required. At the moment, AO stands at about 50-60% faster than ActiveRecord for the same operations on the first pass. It's likely this margin would increase on the second pass, since AO has better caching support, as well as the use of PreparedStatements for all queries.

Anyway, I hope y'all enjoy the article, even if you're a die-hard Hibernate fan. Happy databasing!
1 . At 8:25 PM on Sep 13, 2007, Brian S O'Neill wrote:
  Click to reply to this thread Reply

Re: ActiveObjects: An Easier Java ORM

I agree that the Active Record design pattern is much easier to use, and I like your design of ActiveObjects. In many ways it is identical to Carbonado, the framework I wrote. The key difference is that Carbonado works with more than just SQL databases.

Since it relies on code generation instead of reflection proxies, it doesn't have the interface restriction. This allows domain logic to be written in the same abstract class instead of depending on an annotation to link it up.

As for query optimizations that ActiveObjects can provide, Carbonado does exactly as you describe. Query objects are constructed up in pieces, and the resulting PreparedStatement (if using JDBC) is created once and associated with the cached Query object. Carbonado queries lack a few features, but enhancements are still being made. In version 1.2, Carbonado will finally support sub-queries with "where exists".

Carbonado doesn't support DDL generation, but such a tool is relatively easy to write, since Carbonado provides easy access to all the metadata described by entity definitions.
Brian S O'Neill
carbonado.sourceforge.net
2 . At 12:51 AM on Sep 14, 2007, Alexander wrote:
  Click to reply to this thread Reply

Re: ActiveObjects: An Easier Java ORM

I like the ActiveObjects. I avoided other ORMs due to their complexity. What's the point create million of XML files instead of million of JAVA files? So your designs seems to be quite attractive.
I have one question. I need to add many records to a DB. I don't need references to these records to be cached at all. How can I do that?
 new EntityManager( myProvider, true); 

is that enough? can I switch off caching completely?
3 . At 12:56 AM on Sep 14, 2007, Daniel Spiewak wrote:
  Click to reply to this thread Reply

Re: ActiveObjects: An Easier Java ORM

> I like the ActiveObjects. I avoided other ORMs due to
> their complexity. What's the point create million of
> XML files instead of million of JAVA files? So your
> designs seems to be quite attractive.
> I have one question. I need to add many records to a
> DB. I don't need references to these records to be
> cached at all. How can I do that?
>
 new EntityManager( myProvider, true); 

> is that enough? can I switch off caching completely?

Good eye! I didn't even think that constructor was documented. Actually, constructing an EntityManager in this way won't turn off caching, but it will force it to use WeakReference(s). Thus, as soon as the entities go out of scope in your code, they become eligible for garbage collection. By default, EM uses SoftReference(s), which force the GC to be a bit less aggressive in reclaiming cached entities.
Daniel Spiewak
ActiveObjects: an Easier Java ORM; Fuse: Resource Injection for Java
4 . At 4:53 AM on Sep 14, 2007, Ray Cromwell DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: ActiveObjects: An Easier Java ORM

Hibernate doesn't require XML mapping files, it supports aspects of configuration-by-convention and annotations (the most trivial mapping is just sticking @Entity in front of your class). However, it gets even simpler than that, with Google Guice's dependency injection, and the Warp Persist framework's Dynamic Finder Guice module, you can handle complex queries with a simple annotation, e.g.

@Finder(query="from Customer where firstName = :fn and lastName = :ln")
public Customer findByCustomerName(@Named("fn") firstName, @Named("ln") lastName);


That's it, stick this method on an interface, and have Guice inject an auto-generated implementation (DAO), or stick it on the POJO itself (stub the implementation to return null) and Guice will override it with another implementation (DDD)

Not to criticize your design, but I don't think it is any "easier" than Hibernate Annotations w/Guice injection, and it's not less lines of code (Dynamic Finders are more concise). Less lines of code, and a more powerful backend (Hibernate or JPA).

I can't stand XML config files, and I don't have any ORM-oriented ones in my project (I don't use Spring either), see http://www.wideplay.com/dynamicfinders for details.
5 . At 6:59 AM on Sep 14, 2007, Shai Almog wrote:
  Click to reply to this thread Reply

Re: ActiveObjects: An Easier Java ORM

Have you seen the work Glen Marchesani and myself have been doing at: https://bean-properties.dev.java.net/orm.html

(Well Glen has been doing the "heavy lifting" in relational mapping). I think it solves pretty much all the problems you raised and it does it in a more "Java" way by maintaining type safety and allowing compile time checks of all ORM related references in the virtual schema. Its at least as concise and doesn't require any bytecode instrumentation whatsoever. Its not yet at release stages but its functional for most uses (still missing auto-increment and many-many relations).

I'm also discussing a port of Hibernate on top of bean properties that would use the same syntax thus allowing a "brand name" ORM solution to use the simplified/type checked syntax properties can offer.
Shai Almog vPrise Software makers of vPrise Workgroup http://wg.vprise.com/ founder of bean-properties the leading OSS properties implementation in Java https://bean-properties.dev.java.net/
6 . At 1:33 PM on Sep 14, 2007, Will Hartung DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: ActiveObjects: An Easier Java ORM

Yea, JPA simply isn't that bad.

We have almost no XML in our system, and while we annotate all of the bean properties, we don't have do. We simply choose to.

And at the same time, we don't use EQL hardly at all, but instead leverage Toplink Essentials internal query code.

At its core it looks like this:

List result = createQuery(new ExpressionBuilder().get("name").equal("Bob"), Customer.class).getResultList();

That's the essence of it, there some ugly casting and what not, digging the Oracle stuff out of the generic JPA structures, etc. So we wrapped it all up into a structure we call a Filter.

Here's a simple Customer filter that will get us customers by name, or customers with active (or inactive for that matter) orders (which is related through the entity).
public CustomerFilter extends ExpressionFilter {
    String name;
    Boolean activeOrders;
 
    public CustomerFilter() {
        super(Customer.class);
    }
 
    // setters/getters ellided
    public void buildQuery() {
        if (name != null) {
            // addExpr adds an expression to the filter. These are all ANDed together later in the superclass.
            // eb is an Oracle ExpressionBuilder instance defined in the superclass;
            addExpr(eb.get("name").equal(name));
        }
        if (activeOrders != null) {
            // our booleans are 0/1 in the db -- we didn't bother mapping them as booleans.
            int flag = activeOrders ? 1 : 0;
            addExpr(eb.anyOf("orders").get("active").equal(flag));
        }
        // Result list ordered by name.
        addOrderBy(eb.get("name"));
    }
}


using it:

DAOSessionBean sb = lookupDAOBean();
CustomerFilter f = new CustomerFilter();
f.setName("Bob");
List<Customer> result = sb.findByFilter(f);


We can make the filters arbitrarily complex, and we have it so that we can get counts or just primary keys as well as list of entities, all with the same filter.

sb.findByFilter(Filter f);
sb.getCountByFilter(Filter f);
sb.getKeysByFilter(Filter f); // For ValueList pattern

It's really easy to use.

The problems with this are the same problems you have when working with any of the ORM solutions -- the ORM itself. There's just times when they simply get in the way, fighting lifecycle, fighting caches, etc. Nature of the beast.
7 . At 7:24 AM on Sep 15, 2007, Konstantin Scheglov DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: ActiveObjects: An Easier Java ORM

Very nice idea and very nice article. This is exactly how I thought when tried to use Hibernate with large RCP application. Hibernate was too complex, had too much AI (for some time past "AI" become swearword in our team) and was too bound to the Web model of behavior. So, after several weeks, we dropped Hibernate and in one week implemented our own simple ORM tool. If in future I will need again to work with database, I definitely will consider ActiveObjects. :-)
Konstantin Scheglov - Java GUI developer
SWT/Swing/ GWT Designer - SWT/Swing/GWT GUI designer for Eclipse
8 . At 1:24 PM on Sep 15, 2007, Tom wrote:
  Click to reply to this thread Reply

Re: ActiveObjects: An Easier Java ORM

The term "active objects" already has a meaning in computer science related to threading. Calling this an "ActiveObjects ORM" is rather confusing since it doesn't seem to have anything to do with active objects.
9 . At 1:30 PM on Sep 15, 2007, Ray Cromwell DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: ActiveObjects: An Easier Java ORM

umm, how is Hibernate bound to web model behavior? I use Hibernate for non-web applications, I've never encountered this "limitation".
10 . At 1:46 AM on Sep 16, 2007, Konstantin Scheglov DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: ActiveObjects: An Easier Java ORM

Hibernate was designed IMHO to work in short request/response sequences, i.e. as in web. I found at least two problems for thick GUI clients:

1. You request objects tree, open dialog, edit your objects and at some point decide that you don't like what you did and press cancel. But objects tree is still in Hibernate's cache, in modified state. So, I need some trick to erase them from cache. There is IIRC method to remove single object from cache, but in my task I have tree of objects with complex dependencies.

2. Failed sessions. Once you have exception during some operation, for example some check in your table failed, you HAVE to close session and open new one, so you loose all your objects, they become disconnected. This is absolutely web-like behavior - you catch all exceptions, close session and send error page to user. But for GUI application this is big pain. I want just rollback transaction, show error to user and allow him continue editing. In hibernate - no way, I've read 2 years ago all - articles, forums, etc. Many people have had this problem and nobody was able to find good solution.
Konstantin Scheglov - Java GUI developer
SWT/Swing/ GWT Designer - SWT/Swing/GWT GUI designer for Eclipse
11 . At 6:19 AM on Sep 16, 2007, Alexander Hristov DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: ActiveObjects: An Easier Java ORM

> 2. Failed sessions. Once you have exception during
> some operation, for example some check in your table
> failed, you HAVE to close session and open new one,
> so you loose all your objects, they become
> disconnected.

Out of curiosity, what is exactly the problem with rolling back the transaction?
Planetalia - Cursos de Java
12 . At 8:46 AM on Sep 16, 2007, Konstantin Scheglov DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: ActiveObjects: An Easier Java ORM

In pure JDBC or some less intrusive ORM - no problem. In Hibernate documentation specifies that after exception you have to close session (yes, this rollbacks transaction) and create new session. Note, not just rollback transaction, but recreate session.
Konstantin Scheglov - Java GUI developer
SWT/Swing/ GWT Designer - SWT/Swing/GWT GUI designer for Eclipse
13 . At 9:20 AM on Sep 16, 2007, Alexander Hristov DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: ActiveObjects: An Easier Java ORM

> after exception you have to close session (yes, this
> rollbacks transaction) and create new session. Note,
> not just rollback transaction, but recreate session.

Do you have a link for that part of the doc?
Planetalia - Cursos de Java
14 . At 9:31 AM on Sep 16, 2007, Konstantin Scheglov DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: ActiveObjects: An Easier Java ORM

http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html

If the Session throws an exception, the transaction must be rolled back and the session discarded. The internal state of the Session might not be consistent with the database after the exception occurs.

Or even better this:

http://www.redhat.com/docs/manuals/jboss/jboss-eap-4.2/doc/hibernate/Hibernate_Reference_Guide/Database_transaction_demarcation-Exception_handling.html

If the Session throws an exception (including any SQLException), you should immediately rollback the database transaction, call Session.close() and discard the Session instance. Certain methods of Session will not leave the session in a consistent state. No exception thrown by Hibernate can be treated as recoverable. Ensure that the Session will be closed by calling close() in a finally block.
Konstantin Scheglov - Java GUI developer
SWT/Swing/ GWT Designer - SWT/Swing/GWT GUI designer for Eclipse

thread.rss_message