Using Hibernate3 AS A JDBC Framework (Reprinted from TSS)

xiaoxiao2021-03-06  95

There's been a certain amount of noise recently surrounding simple JDBC frameworks like iBATIS. I've liked the idea of ​​iBATIS myself, for use in applications which do not need an object-oriented domain model, and do not work with deep graphs of . associated entities in a single transaction A JDBC framework also makes good sense if you are working with some kind of "insane" legacy database; ORM solutions tend to assume that associations are represented as nice clean foreign keys with proper referential integrity constraints (Hibernate3 much Less So Than Hibernate 2.x).

Some people even suggest that JDBC frameworks are a suitable alternative to ORM, even for those systems to which ORM is best suited: object-oriented applications with clean relational schemas They argue that you are always better off with hand-written SQL than generated SQL. . Well, I do not think this is true, not only because the overwhelming bulk of SQL code needed by most applications is of the tedious kind, and simply does not require human intervention, but also because a JDBC framework operates at a different semantic level to ORM. A solution like iBATIS knows a lot less about the semantics of the SQL it is issuing, and of the resulting datasets. This means that there is much less opportunity for performance optimizations such as effficent caching. (By "efficient", I am referring mainly to efficient cache invalidation strategies, which are crucial to the usefulness of the cache.) Furthermore, whenever we have seen handwritten SQL, we have seen N 1 selects problems. It is extremely tedious to write a new SQL query for each combination of associations I might need to fetch together. HQL helps significantly here, since HQL is much less verbose than SQL. For a JDBC framework to be able to make the kind of optimizations that an ORM can make, it would have to evolve to a similar level of sophistication. Essentially, it would need to become an ORM, minus SQL generation. in fact, we already start to see this evolution taking place in existing JDBC frameworks. This begins to erode one Of The Stated Benefits: The Claimed SIMPLITY.

It also raises the following interesting thought: if, by gradually adding stuff, a JDBC framework will eventually end up as ORM, minus SQL generation, why not just take an existing ORM solution like, ooh, um ... Hibernate, maybe .. . and subtract the SQL generation? The Hibernate team has long recognized the need to mix and match generated SQL with the occasional handwritten query. In older versions of Hibernate, our solution was simply to expose the JDBC connection Hibernate is using, so you can execute your own prepared statement. This started to change a while ago, and Max Andersen has recently done a lot of work on this. Now, in Hibernate3, it is possible to write an entire application with no generated SQL, while still taking advantage of all Of Hibernate's Other Features.

Do we really expect or intend people to use Hibernate in this way Well, not really -?. I doubt there are many people out there who really enjoy writing tedious INSERT, UPDATE, DELETE statements all day On the other hand, we do think that QUITE A FEW People NEED TO CUSTOMIZE The OccAsional Query. But to Prove A Point, I'll show you how you can do it, if you really want to.

Let's take a simple Person-Employment-Organization domain model (You can find the code in the org.hibernate.test.sql package, so I'm not going to reproduce it here.) The simplest class is Person;. Here's the mapping :

INSERT INTO PERSON (Name, ID) VALUES (Upper (?),?) Update Person Set Name = Upper (?) where id =?

delete from person where id =?

The first thing to notice is the handwritten INSERT, UPDATE and DELETE statements. The? Order of the parameters matches to the order in which properties are listed above (we'll have to eventually support named parameters, I suppose). I guess there is Nothing especially intending there.

More interesting is the tag: it defines a reference to a named query which is to be used anytime we load a person using get (), load (), or lazy association fetching In particular, the named query might be a. Native SQL Query, Which it is, in this case:

SELECT NAME AS {p.Name}, ID as {p.id} from person where id =? For Update

(A Native SQL Query May Return Multiple "Column" of Entities; this is the simplest case, where just one entry is returned.)

Employment is A Bit More Complex, In Particular, Not All Properties Are Included in INSERT and UPDATETETENTS:

INSERT INTO EMPLOYMENT

(Employee, Employer, StartDate, Regioncode, ID)

VALUES (?, Current_date, upper (?),?)

UPDATE EMPLOYMENT SETDATE =? where id =?

DELETE FROM EMPLOYMENT WHERE ID =?

Select Employee As {Emp.employee}, Employer As {Emp.employer},

Startdate as {Emp.StartDate}, enddate as {emp.Enddate},

Regioncode as {emp.regioncode}, id as {emp.id}

From Employment

WHERE ID =?

The mapping for Organization Has A Collection of Employments:

Lazy = "true"

Inverse = "True">

INSERT INTO Organization (Name, ID) VALUES (Upper (?),?)

Update Organization Set Name = Upper (?) where id =?

delete from organizance where id =?

NOT Only Is There a Query for Organization, But Also for ITS Collection of Employments:

Select name as {org.name}, id as {Org.id} from Organization

WHERE ID =?

Select {EMPCOL. *},

Employer as {Emp.employer}, Employee as {Emp.Employee},

Startdate as {Emp.StartDate}, enddate as {emp.Enddate},

Regioncode as {emp.regioncode}, id as {emp.id}

From Employment EMPCOL

WHERE EMPLOYER =: id and deleted_datetime is null

When I was writing this code, I really started to feel the advantages of having Hibernate write the SQL for me. In just this simple example, I would have eliminated more than 35 lines of code that I would have to later maintain.

Finally, for ad hoc querying, we can use a native sque (a named query, or one embedded in the java code). For example:

Select distinct name as {org.name}, ID as {org.id}

From organization org

Inner Join Employment E on E.employer = Org.ID

Personally, I Prefer to Program in Java Than, So All this stuff is much too xml-heavy for my liking. I think I'll Stick with SQL Generation, Wherever i CAN, Which is Almost Everywhere. It's not that i don 't like SQL. In fact, I am a great fan of SQL, and just love watching the queries scroll past when I turn Hibernate's logging on. It's just that Hibernate is much better at writing SQL than I am.About the author

Gavin King (gavin@hibernate.org) Blog: http://blog.hibernate.org/Gavin King is the founder of the Hibernate project He is co-author of the book Hibernate in Action, to be published by Manning Publications.. HE is currently involved in the jdo excert group and is employed by jboss inc, Where he is redesigning the jboss cmp engine.

转载请注明原文地址:https://www.9cbs.com/read-122614.html

New Post(0)