Java, SQL, and the ORM Mismatch

I just can’t take it any more.

There is an incorrect perception floating around out there in Java world about the issue of mapping Java classes to SQL tables.  This issue falls within a larger topic often referred to as the object-relational mapping (ORM) mismatch.  This “mismatch” is based on the fact that Java is object-oriented, and SQL is not, so using Java classes to represent SQL tables involves some issues, and requires some clever work.  That is true.  But it’s led to a major misperception about SQL being supposedly “defective”.  It is not, but this notion has crept its way into two books I’ve read lately, books by otherwise very smart people, published by major cutting edge IT book publishers.

(For the record, no, neither publisher in question is my own publisher, the incomparable McGraw-Hill Education and their awesome imprint with Oracle Corporation, Oracle Press.)

I’m not going to verbally quote either work, but the idea goes like this:

  • To support persistence (storing data), you map a Java class (ex., “Customer”) to a corresponding SQL table (ex., “CUSTOMERS”)
  • When creating two Java classes, it’s possible to design them with a many-to-many relationship to each other
  • SQL doesn’t support the creation of tables with many-to-many relationships
  • Therefore SQL is defective.

Uh – no.

Folks, the presence of a many-to-many relationship in your data model is a sign that you aren’t finished with your data model.

This isn’t a Java thing, or a SQL thing, it’s a real world thing.

It’s possible to build a defective data model in Java.  You can do a lot of stupid stuff in Java.  You can also build bad data models in SQL, and technically you actually can create a many-to-many in SQL, but it won’t work.  That’s a good thing.  I’ll explain shortly with an example.

But first, a word about the classic mismatch.

THE ORM MISMATCH

The authors of these two books I’ve read are Java developers first and foremost.  I get that, and I know where they’re coming from.  I’m a Java developer myself, and have been since Java was first introduced into the Oracle database as an alternative to PL/SQL, which I also love – so much that my first book was the official Oracle Press certification exam guide OCP Developer PL/SQL Program Units Exam Guide.

But about the time that book hit the shelves, I began working with Java, which had become available as an in-database alternative to PL/SQL.  I soon thereafter began teaching Java.  In fact, this is me with then-CEO of Sun Microsystems, on the first day I launched my first ever course in Java:

Scott McNealy, Steve O'Hearn, at the National Press Club

That was taken at the National Press Club.  McNealy was hysterically funny that day and a great guy. He’s responsible for, and oversaw, a lot of great developments in Silicon valley that reverberate to this day – like Java.

And back at that time, the first question about Java was – how do we get Java and SQL to interact?  That, after all, was Oracle’s whole point of embedding Java in the database.

Java has come a long, long way since then, and all for the better.  There is a well established and growing library of packages and tools for implementing various interactions between Java and SQL.  And at the heart of them all is often the same issue:

Java is object oriented, and SQL isn’t.

True.  So what?  Newcomers to Java act like this is some kind of ultimate nightmare scenario, a sign that SQL is out of sync with the world.  Nonsense.  SQL isn’t inherently object-oriented, but neither is your file system.  And yet Java interacts with files just fine.  Now granted, the idea behind SQL is more sophisticated than file systems.  But on the other hand, SQL isn’t in the same category as a third-generation language (3GL) like Java, SQL is a fourth generation language (4GL). It might not support object-oriented dynamics, but neither does it clash with them.  This is demonstrably obvious – otherwise we wouldn’t have JDBC, the Java Persistence API (JPA), and Hibernate.  So the two can and do work together, and effectively.  But it takes a bit of work; SQL doesn’t necessarily provide built-in support for all object-oriented concepts, such as inheritance. (Caveat: Inheritance actually can be supported in data modeling – see this article – and there’s a way to do it within SQL itself, sort of, but as to whether it’s helpful or not, that’s a different story, and perhaps the topic of a future blog post.  Or book.  Hm …. )

Frankly, when I hear about the “mismatch” between Java and SQL, I think of the issue of transactions, that’s the only real issue to me. The object life cycle and nature of Java is such that it introduces challenges in the way persistence to the database is best done in a multi-user/multi-threaded environment.

But let’s get back to topic and dispel this “SQL is defective” myth.

DATA MODELING

Data modeling is the act of representing real world business processes through diagrams of the things (“entities”) that comprise a real world process, and the relationships (or “associations”) among those entities.  The most obvious entities are easy to identify – customers, products, office buildings, vendors, cars, etc.  But the more abstract entities are not always so obvious – work schedules, change orders, reservations, that sort of thing.  This is where the importance of understanding many-to-many relationships can be very helpful.

AN EXAMPLE

I’m going to use an example taken from the airline industry, as I’ve been meeting lately with Lisa Ray, a lifelong aviation data expert closely involved with a series of legacy migrations in that area. (And for the record, she fully gets this.)

Let’s start with two obvious entities:

  • Planes – as in “individual planes”, not just types of planes.
  • Pilots – individual people.

Every one plane might be flown by more than one pilot.  Every one pilot will be qualified to fly more than one plane.  Sounds like a many-to-many relationship, right?  And as you might know, an entity-relationship diagram illustrates the “many” side of a relationship with the classic crow’s-feet line:

ERD Diagram A

So that’s our initial logical diagram for these two entities.

Now folks, this blog post is not going to be a complete tutorial on how to perform data modeling, but we will explore one key aspect of modeling: whenever you encounter a many-to-many relationship, you ALWAYS transform it using the following steps:

  • Add a third entity in between – “Entity_3” in our diagram below.
  • Create foreign keys within the new entity for the primary keys of the existing entities – in this case, PLANE_ID as a foreign key to the PLANES entity’s PLANE_ID primary key, and PILOT_ID as a foreign key to the PILOTS entity’s PILOT_ID primary key.
  • Establish “one-to-many” relationships between the new entity and the existing entities, using the “single-line” to indicate the “1” side, and the “crow’s foot” to indicate “many” side of the relationship.

Like this:

ERD Diagram B

The reason we do that is simple:  the presence of a many-to-many relationship is a clear indicator that something else goes in between the two entities.  The question is not “if” something belongs there.  Something does belong there.  The only question is – what exactly is it?

In our example, it’s going to be something like a “roster”, or a “schedule”, or “flight assignments”, or “rentals”:

ERD Diagram C
Call it what you will, but something goes in between and it must be included in your data model.  That new entity will have its own attributes.  Perhaps:

  • A start and end date to the time of assignment
  • A name of a key staff member authorized to approve the assignment

Who knows what might be there? But something is there, and the presence of the many-to-many relationship is your clue to its existence.

That third entity is often a bit more abstract.  You can see pilots and planes, but a “flight assignment” isn’t necessarily as obvious.  But it’s an important entity in the data model nonetheless.

One more point:  this third entity isn’t some necessary crutch to get around a “SQL defect”. I can’t believe what I’ve read in certain Java books that suggest such a thing.  No.  This third entity is a real world “thing” that definitely exists in order to enable the relationship with the real world, and it’s up to you to identify it. Whether you figure it out or not, it exists nonetheless.

The rule is simple: no complete data model exists with any “many-to-many” relationships.  If such are present, you aren’t done modeling yet.

So now that you understand this, imagine reading a Java manual in which someone explains that SQL is “defective” because it “doesn’t support many-to-many relationships”.

No.

Now you know!

O’Hearn is the Java leader of the only officially recognized JUG for the DC metro area.  He is also the author of the first-ever expert-level certification exam guide from Oracle Press, titled OCA Oracle Database SQL Certified Expert Exam Guide (Exam 1Z0-047).  The 2nd edition of his SQL Expert book, revised for Oracle 12c, is due out in 2016.

Number one again in time for OOW 2014

A great thank you to my readers!  See below!

SQL Expert book at number one again on Amazon's Oracle booklist

I just grabbed the above image from the Amazon website earlier tonight, September 24, 2014, at 11:52 p.m.

It shows that my book – OCA Oracle Database SQL Certified Expert Exam Guide (Exam 1Z0-047) – is number one again at Amazon.com’s Oracle category, less than one week prior to the Oracle Open World / JavaOne 2014 conference in Silicon Valley.

A hearty thank you to all my readers on behalf of me and the writing and editorial team at McGraw-Hill and Oracle Press!  Thank you thank you!

Congratulations to jaramill “gjaram”!

A hearty congratulations to jaramill “gjaram” and a big thank you as well for the excellent review he posted of my SQL Expert book at Amazon.com.  He wrote:

“This is my review 3 months after I bought the book but just 2 days after taking the exam……and PASSING it on the first try!”

So welcome jaramill to the world of certified SQL experts! And thank you for the very nice review.

If you wish to see jaramill’s complete review click here, it’s titled “Worthwhile guide to help you for the exam” and gives my book five out of five stars.

“Mind blowing book”

I’m sending out a huge thank you to Gobikrishnan Srinivasan for this great comment about my book that he posted online:

“I did my certification on last saturday being a sql expert u r given a class A seat and my suggestion is go for steve o hearn books its simply mind blowing book i learnt a lot from it it helped me to pass the exam”1

You can see this comment for yourself on LinkedIn, in a discussion among the elite members of the Oracle Database SQL Certified Experts group.  Gobikrishnan is a member of that group and lists himself as a Compliance Analyst at IBM.

The book he’s talking about of course is OCA Oracle Database SQL Certified Expert Exam Guide (Exam 1Z0-047).

Thank you Gobikrishnan for the great endorsement!

SQL Expert is number one again

SQL Expert book at number one on Amazon's Orace booklist

Some friends have drawn my attention to the fact that my 2009 book OCA Oracle Database SQL Certified Expert Exam Guide (Exam 1Z0-047) has been back at number one in Amazon’s “Oracle” books list.

I’ve observed that Amazon’s best-seller lists appear to be updated as often as hourly.  So it might be at number one as I type this, but elsewhere – such as 4th or 7th – later in the day.  This latest book of mine has been bouncing around in the top 10 of all Oracle books for a few months now.  Every now and then it will show up as number one, perhaps about once every few days in my observation.  Now – if I had privileges to query Amazon’s database, I could produce an accurate and comprehensive report.  If you’re a reader of my book, you could create such a query too!

On the book’s main page, Amazon displays a “Best Seller!” flag when it hits number one, with a link to the list.

I’m sure the interest in the book is largely driven by the exam itself, of course, which is a fantastic professional credential for anyone to get.  When you can add a blurb to your resume with the keywords “Oracle” and “expert” together, you’re doing treat.  That being said, I’ve also received plenty of email from readers who are just honing their skills and filling in gaps in their knowledge, with or without plans to take the exam.  The book is uniquely useful for that as well.

So – a huge “thank you” to all my readers!  You’re the reason this is all happening, and I thank you.

Conference in Williamsburg, Sqoop, and Big Data Connectors

Yesterday  – April 24, 2014 – I had the privilege of presenting at the Virginia Oracle User Group (VOUG) annual Oracle Conference on the James, or OCOJ for short.  The “James” is the James River right there in Williamsburg, Virginia.  I promised my audience I would publish the Power Point slides, so here they are:

Yesterday could not have been a more glorious day. The Doubletree Hotel in Williamsburg is really a conference center, with a variety of beautiful meeting rooms and great skylights and large glass walls.  The room in which I presented was unusual in that it had one door that led directly outside.  The hotel propped it open and the beautiful air flowed it, it was remarkably refreshing.  I particularly enjoyed it, given the many snow days we’ve had this past winter and even into the spring.  The air was fresh and the temperatures just perfect, what a great day.

I was definitely in with some illustrious company – other presenters throughout the day included Mary Gable, David Mann, Craig Shallahammer, Greg Mays, Scott Poteet, Bill Myers, and Oracle’s own Bob Bunting as well as Robert Freeman.  The legendary Tom Kyte (of Ask Tom fame) was the keynote speaker.  Brilliant talent was on display everywhere.

For more information about VOUG, visit their website here:  http://www.voug.org.

Here’s a copy of the full conference agenda:  VOUG OCOJ Conference Agenda.

Thanks to one of my great audience members who took the photo.  And a huge thanks to Linda Hoover for making the entire event possible – thank you Linda!

Thanks to “bigdelboy” at Oracle Technology Forum

I just posted the following comment in a thread at the Oracle Technology Network forum, and I thought I’d repost it here, see below.

= = =

This is Steve O’Hearn, and thanks to bigdelboy for reposting the URL for the script download for my book, OCA Oracle Database SQL Expert Exam Guide: Exam 1ZO-047. I didn’t realize my earlier posts providing the URL were flagged as spam. You’re correct that I posted that URL in several forums at once. It’s unfortunate that whoever or whatever may have deleted my entries would’ve done so on behavior alone without regard for the content of the message.

Anyone can visit my blog at blog.corbinian.com and look for the “1ZO-047 SQL SCRIPTS” link and follow the instructions. And yes, the script download is limited to owners of the book itself. The script is worthless to anyone else anyway.

For anyone wondering about preparation for the exam, please note that my book includes 728 pages in 18 chapters, including one for each of the exam objectives, and each of those chapters includes the following:

– Text to address the exam objectives in detail
– A detailed chapter summary (“certification summary”)
– A through “two minute drill” reviewing the concepts of the chapter
– A “self-test” on the chapter, including 15 questions each, and each question is designed to match the pattern of the actual certification exam. This means that many questions are based on code samples, and all offer multiple choice answers.
– Detailed explanations for each “self-test” question and each and every answer – including an explanation as to why each answer is either right or wrong

In addition to all of this, the book also includes a complete 70-question practice exam at the end of the printed book.

And in addition to that, there’s another complete 70-question practice exam available online to owners of the book.

These exams are not included in the 728 pages of text. Altogether you’re getting over 1,000 pages of material with which to prepare for the exam.

The book really is a complete package for exam preparation. And judging by the volume of email I get from happy readers who are now certified, I dare say its very effective.

Good luck to you if you are studying for the exam! And please feel free to contact me with any comments or questions you may have. I’ve read that I’m apparently not allowed to post my email address here, but my blog website is listed above, and you can contact me via the blog.

– Steve

Another Great Book Review

Here’s another great review of my book OCA Oracle Database SQL Expert Exam Guide, this from R.C. Roper of Topanga Canyon, California, who says:

This is a great book, covering all of the relevant topics in detail. The chapters and sections are in the order the certification objectives appear on Oracle’s ‘Exam Topics’ page for this test, so it’s easy to navigate and find your trouble spots. There are over 200 end-of-chapter questions in the book, and that’s all I used to study for this exam. After only a week of study, I passed the exam with no other preparation except for the experience I’ve gained over the short time I’ve held my job. I can only imagine how well I would have done if I registered later for the test and gave myself more time.

Thanks, R.C.! The outline and overall structure was a major topic of discussion and debate in the original formation of the book, and frankly it made the job a bit challenging. It’s one thing to write a book that mirrors the exam objectives outline, and it’s another to create a book that conveys the information sequentially from start to finish in a way that initially made the most sense to me. Creating a single volume that does both simultaneously was tricky – but I think we did it, and did a great job. By “we”, I’m including the excellent team at McGraw-Hill, including Tim Green, Meghan Manfre (formerly Meghan Riley), and Molly Sharp, as well as the outstanding and brilliant technical editor Alistair Grieve who was tremendous and really went above and beyond with this effort.  And even the legendary Kevin Loney provided some key input.  So it was quite the team effort.  (Click here for the complete Acknowledgements section from the book!)

Announcing the winner of the Cartesian Product Challenge

Hello friends!

I announced the winner of the Cartesian Product Challenge at this year’s Oracle Open World 2012, here’s the formal announcement:

Steve O’Hearn – The Challenge from Oracle Certification on Vimeo


And yes, what I say above is true – this is the winner of the FIRST annual Cartesian Product Challenge! More on that soon, in the meantime, to all my American colleagues, a very happy Thanksgiving!

Here’s a little background on this video: this was shot at the Oracle Open World Certification Lounge, in the Moscone Center, on Monday, October 1, 2012. However – it wasn’t really planned. I was there to do a question-and-answer session with Oracle Open World attendees on issues pertaining to certification in general, and the SQL Expert exam in particular. The outstanding Carey Hardey made a lot of the arrangements, and the fantastic Harold Green of Oracle Corp. was there as well – he does Oracle TV spots for the company and had an impressive setup in the back of the room. I’d already brought up the idea of making the announcement of the Cartesian Product Challenge winner there, and Harold and Carey were both supportive of it, and when I was there, Harold had the idea of filming it. So I made some notes on my iPad (that’s what you see me glancing down to read) and grabbed some coffee, and with maybe five minutes of prep, we shot it. Harold is equipped with a tremendous portable recording system, complete with a high quality camera and excellent lighting system, and you can see the results above.

 

A few minutes later, we shot a second video for the Meet the Author series, which I’ll post tomorrow. After that, Harold folded up shop and was gone within about five minutes, to travel a few blocks and set up all over again for another shoot – I think that was scheduled to be at the nearby companion conference, JavaOne.

Stay tuned for more!