Managing Unmapped Tables with Hibernate
There’s an underused feature of Hibernate that I’ve been using recently called “auxiliary database objects“. With these guys, you can finally let Hibernate manage all of your schema creation and deletion. No more using Hibernate to generate mapped tables and then coming behind with another tool to finish the job.
A good example of where this is useful is in the fairly common case of using Quartz for scheduling with a JDBC job store. Quartz provides a SQL script for generating its tables. Before auxiliary database objects, the most common way to get the Quartz tables into the database was to include a separate *.sql file in your build and run it using Ant’s sql task or some other SQL executor. Setting it up this way means there are two steps to either creating or dropping your schema: the Hibernate schema export and the Ant task. That ties this mechanism specifically to your build, eliminating a very useful feature of Hibernate.
With auxiliary database objects, you can include the Quartz create and drop scripts directly in your Hibernate mappings and have Hibernate run those scripts along with its normal schema export. Then there’s only one step to creating and dropping your schema, and more importantly, Hibernate is completely in control of the schema. This gives you the immense benefit of being able to set the SessionFactory’s “hbm2ddl.auto” property to “create” and run an in-memory database–like the awesome H2 database–for development and unit testing, and the schema will be generated for you on startup. This is what you lose if you have scripts outside of Hibernate.
Auxiliary database objects have actually been around for a few years, but it only made it into a general availability release a year and a half ago with version 3.2.6.GA. Below are a couple of simple examples on how to use them.
Besides Quartz tables, I’ve used auxiliary database objects to make a fake “dual” table. It’s common to use the “select * from dual” query on Oracle databases as a validation query for your connection pool. When you run a different database for development, like H2 or MySQL, you’ll have to manually add a dual table so that your validation queries won’t fail. Here are the two ways that you can do that using Hibernate’s auxiliary database objects:
First, you can embed the SQL directly in the mapping file:
<hibernate-mapping>
<database-object>
<create>
create table dual (x int);
</create>
<drop>
drop table dual;
</drop>
<dialect-scope name="org.hibernate.dialect..." />
</database-object>
</hibernate-mapping>
Note that you can have zero to many of the dialect-scope elements, indicating what database platforms this database object applies to. Listing zero dialects means apply to all databases.
The second way to do it is to implement the AuxiliaryDatabaseObject interface. To make it simple, use the AbstractAuxiliaryDatabaseObject subclass:
public class SampleObject extends AbstractAuxiliaryDatabaseObject {
public String sqlCreateString(...) {
return "create table dual (x int)";
}
public String sqlDropString(...) {
return "drop table dual";
}
}
Then you have to specify your class in your mapping file, like so:
<database-object>
<definition class="rds.hibernate.AuxDBObjectTest" />
<dialect-scope name="org.hibernate.dialect..." />
</database-object>
The examples should be pretty self explanatory. Create statements are run after Hibernate’s own generated create statements, and drop statements are run after Hibernate’s generated drop statements. I’ll make one additional recommendation: while it’s technically possible to put multiple SQL statements separated by semicolons in a single create or drop block, it’s best to only put one. There are two reasons for this. First, multiple statements in a single block won’t work with Oracle, so if any of your target environments are Oracle, that’s right out. The second reason is that Hibernate runs each create and/or drop using one JDBC Statement, so if you pack 100 create statements into one create block, and one of them fails, then none of the other 99 will take affect, either. If, on the other hand, you use one statement per block, you guarantee that all statements will run, and only the ones that fail will not take effect.
That’s all! It’s very handy to have all your extra SQL managed by Hibernate. All you have to do is point Hibernate at a database, tell it to create your schema, and all your tables, views, stored procedures, etc. will appear.
If You Want to Have Cities You’ve Got to Build Roads
I wrote this for consideration for the upcoming 97 Things Every Programmer Should Know book. Alas, I didn’t submit it before the deadline. I’m OK with that that because I had another submission that I think has a good chance to make it in the book.
I particularly like to apply this phrase in every-day software development and could easily apply it to entrepreneurship.
The city of Rome has a history of over two and a half thousand years. The city was the center of the Roman Empire and was the capital of the civilized world. It supported hundreds of thousands of inhabitants only by providing the proper foundation for the city. Its roads.
The roads of the Roman city were essential for the growth of the Empire. These roads were used to move goods and services enabling its citizens to prosper. The Roman armies used these roads to defend the Roman territories and to conquer new lands. The roads provided a network for information to be gathered and disseminated. The Roman roads were critical in for its expansion. Roads were the foundation of this great city.
When you think of Rome, you don’t think about its roads. But what would Rome have been without its roads. How big could it have grown? How could the armies defend the Empire? The Roman roads were required for the city to thrive. The roads are not what made the city great, but were what made the city’s success a possibility.
Every organization has its roads and your organization is no exception. What infrastructure have you put in place to foster the growth of your business and lead to its success? Do you have any infrastructure at all? How far can your organization proceed without this infrastructure?
Perhaps for you this is how you communicate. Not just among your team but with your organization, with your customers, and with your community. It could be how you notify your users of new features, how your users submit bugs, or how your managers communicate deadlines.
Maybe its not communication but your product management. From your code repositories and automated build processes to your deployment and monitoring suites. These might be the factors that contribute to the ultimate success of your organization. Without the proper infrastructure in place the city will fail or cease to grow. Without the roads Rome could not prosper and without a strong foundation your organization will fail or hit a wall.
If your users can not submit bugs they will be forced to use your product day after day with known issues. They will become frustrated and find alternative software. If your organization can not communicate its goals and strategies with you then you can not support your organization and so it will fail.
Find out what it is the supports your organization and fortify it. Be aware of its presence and its importance.
With that said, also consider this: the Roman’s roads played an important part in the Roman military defeat by offering avenues of invasion to the barbarians.
From If You Want to Have Cities You’ve Got to Build Roads
Outliers – A non technical book every freelancer/developer should read
Outliers “The Story of Success” by Malcolm Gladwell is a popular book. I heard about the book in a freelance discussion group at a developer conference. I quickly ordered a used copy from Amazon. It’s popularity became very apparent when multiple people, that saw me reading it in an airport terminal, asked if it was any good because they had heard it was. My answer is yes. The book has been reviewed many times (795 on amazon alone) so to avoid duplicate statements I will give my brief summation along with my comments about how the theories in the book apply to software development. If you have read the book please comment with your thoughts. If not I suggest you do because it’s a quick, thought provoking read.
Summary: The book is NOT a 12 steps to success type book, I really hate those books. Gladwell presents the theory that success is not a trait or a gene that only some people have. Gladwell proposes that there are no simple rags to riches stories where a person builds an empire solely on their own. With scientific studies and research to back up the theories Gladwell explores how opportunities and advantages provide the gateway to success.
- 10,000 hours (very popular topic from the book)
- No one succeeds without many, many hours of preparation. No one.
- Examples from Bill Joy(co founded Sun) to The Beatles are given.
- My thoughts: Turn off the xbox360 and the TV and learn a new language, framework, or pattern. Then repeat.
- Luck
- Some people that prepare will be rewarded with incredibly rare opportunities.
- My favorite example in the book is a list of the richest people ever. A large number were from a 10 year period in the mid 1800’s. That time period gave a few lucky individuals the opportunity to become tycoons because of culminating economic and technical advances.
- My thoughts: So if you are not lucky enough to be learning the types of technical skills that will benefit you, then take it upon yourself to make your own luck and find a position that will.
- IQ is not all you need
- The book covers in depth the story of Chris Langan who has an IQ of 195, but has never attained a college degree or high profile professional career.
- My thoughts: To succeed in software development you need communication skills, imagination, and technical skills(IQ). You need to know how to inform/educate non technical people and how to debate effectively with colleagues.
- It takes a village
- No body makes it alone. We all need support from family, friends, and colleagues.
- Every freelance book or article I read says to have a partner.
- Love what you do
- Great quote from the book: “Hard work is a prison sentence only if it has no meaning”
- My thoughts: You know if you love your job or not. Stop thinking about finding a satisfying job. Update your resume and go find a job you will love. It won’t find you, unless you are one of those really lucky people the book mentions.
Another excellent review of Outliers: http://www.markhneedham.com/blog/2009/01/06/outliers-book-review/
