The Part Time Developer 4: Hibernate reverse engineering and JUnit4

I was under the weather toward the end of the week, but I was still able to find some time for some professional development. I’m basically done with all the Hibernate tutorials that I have. I also looked into the reverse engineering offerings in Hibernate. Not that I’m going to need this sort of thing in the near future, but it’s good to know these things exist. It basically let’s you take a legacy (read existing) database and reverse engineer your Hibernate mapping files out of it. It seems like the basic mappings it comes up with wouldn’t be what you’d want to use, but you can set up a reverse engineering file to tell it how you want things. But if you’re going into that much depth, it almost seems like you should just build the mappings yourself. You can further help your legacy cause by then generating your Java entity classes from the mapping files. Pretty cool. It tempts me to come up with my DB schema, work on my mappings, and then generate my code from them. It wouldn’t be exactly the way I’d want them probably, but should be close. Though most of the time, I tend to come up with the data I need from the code I right.

I created a basic project template that I plan on using for new projects that I start. I can just copy the set of files in and modify them to suit what I’m doing. Basically, it’s just the folder structure, Ant build file, and some config files. But it should speed me along.

I don’t know what started me down the path, but I upgraded my project to JUnit4. They switched from making you inherit from JUnit classes to using annotations which allows you to indicate which functions are what. That part was pretty easy to figure out, but I couldn’t find a nice example on how you run the tests of multiple classes in a suite() like fashion. I finally pieced it together from a variety of sources and here’s how the JUnit4 suite type class would look.

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ Test1.class, Test2.class, Test3.class })
public class AllTestSuite {
}

Within Eclipse, you can run the any of the classes as a JUnit test or run the suite to run them all that you’ve wired up. Eventually, I’ll get this added into Ant to automate things with a build.

So what’s left with Hibernate? Well, I feel I’ve done enough in the way of tutorials and reading. I’ve still got the documentation to read when I’m not at the laptop, and the Caveat Emptor application to dig through. But what I really need to do is to get some hands on and perform some of my own mappings, so I need to start my own project. Coming up with a meaningful project idea is always a problem for me. I need something that will enable to stretch the boundaries of the what I’m using to enable me to really get a practical understanding of it. I also need something that will hold my attention, something that I want to do. So that usually falls into some kind of gamey type development. Not any whiz-bang graphical masterpiece, but probably something more akin to the PBEM game that I’ve been running for the past 7 years for my friends. So I think my first stab at this will maybe be a space exploration/conquest game. As always, I dream of having it hosted on the web and allowing players to interact with the site to enter their orders for the turn, but most likely the first iteration or twelve would have turns emailed to me. Not that this may ever see the light of day, but who knows. If anybody knows of an internet host that you can run Tomcat hosted Java applications on, let me know.

Once I get comfortable wiring my classes up to my tables, it will definitely be time to move on to Spring. I’m hoping when I look back a couple weeks from now, that’s what I’m digging into.

Related posts:

  1. The Part Time Developer 6: Adding Hibernate to the Spring Tutorial The code formatting in this post is crap. I've been...
  2. The Part Time Developer 2: The Beginning So here's a recap of my first week of renewing...
  3. The Part Time Developer 3: Hibernation Mode I got probably a good 3-5 hours in this weekend...
  4. The Part Time Developer 5: Spring So now that I'm relatively back in the land of...
  5. The Part Time Developer 1: Explanation I apologize for the incoherence of my last post. I...

Related posts brought to you by Yet Another Related Posts Plugin.

Tags:

2 Responses to “The Part Time Developer 4: Hibernate reverse engineering and JUnit4”

  1. gojim Says:

    There is a Hibernate Tools plugin for Eclipse that we use to generate our entity classes from our database (it actually connects to our MySQL DB and grabs the details that way). We’d never be able to generate the detailed mappings that this tool does (we have it generate the entity classes fully annotated, no more xml mapping files for me!!!). This is great if you have someone who is very DB knowledgeable, but doesn’t excel in OO design (like our DB guy).

    Obviously the preferred way is to let your object design drive the DB design, not the other way around, but it’s working pretty well so far.

  2. FloydWing Says:

    I think I actually installed that plugin at one time, but haven’t really explored what it can do. Sounds like I should. Can you use annotated classes without using JPA/EJB?

    I want to get a firm understanding of the xml mappings before I move past them, but looking at some of the mapping examples in the docs can really make my head spin. Of course, I would think that most projects wouldn’t call for some of the more complicated features. I am seeing from the docs, though, that the way you set up the mappings can have an effect on performance, for example causing two update statements when a different mapping would cause only one in some cases.

Leave a Reply