Ruby has been a growing blip on my radar for a while. In this show I describe my first real production Ruby application at work — and it’s small enough to talk about a few details. I cover how we use Ruby to automate some interactions with Subversion and Maven. I’ve done some Rails stuff too, but that’s a whole different topic. Conclusion: Ruby should be something you use to complement Java. If you want a Ruby vs. Java fight, you won’t find it here.
I got the following question from a friend the other day.
I have a parent and a child object with a one-to-many relationship by the child’s table having a column that points to it’s parent. What are the correct settings for the mapping in this situation where I need to be able to remove children from the parent’s set, and save the updated parent. It seems like I’ve been through all sorts of combinations, and I was wondering if you could lend me your advice.
The same day I also had a similar question from a colleague at work. I figured I’d post my response here so I could easily find it in the future. I’ve tried to make the code reasonably generic, but watch out for dumb typos.
Notice that the column name in each refers to THE SAME COLUMN. You’re just connecting the link.
From the parent class:
/** The set of children of this object. */
privateSet<ChildClass> children = newHashSet<ChildClass>();
private voidsetChildren(Set<ChildClass> children) {
this.children = children;
}
public voidremoveChild(ChildClass child) {
children.remove(child);
child.setParent(null);
}
public voidaddChild(ChildClass child) throwsIllegalArgumentException{
if (child ==null) {
thrownewIllegalArgumentException("Child may not be null");
}
if (child.getParent() !=null) {
child.getParent().removeChild(child);
}
child.setParent(this);
children.add(child);
}
From the child class:
/** The parent. */
privateParentClass parent;
This simple example seems to illustrate the basics. My friend later pointed out that one of the things tripping him up was the cascade option. Recognizing that it’s completely separate from the inverse property was a big step. That’s an important point – the inverse just means that the child objects have a property that refers back to the parent. It helps resolve object relationships on load, but the cascade and lazy attributes tell Hibernate when to save and load the objects to and from the database.
Just wanted to jot down a quick Subversion recipe. I’m doing a merge today, and I wanted to know which of the 25 folders on my branch actually changed. Here’s what I used to figure it out.
Maven is an interesting tool. It can be both wonderful and viciously annoying. Today’s show discusses one of its strengths – modularizing the build process. I give a brief overview of the basic components of our build system, which includes CruiseControl and how the pieces depend upon each other.
I’ve had my head down on a project for the last few months — thus the silence. Martin Fowler’s recent note about Anecdotal Evidence brought me out of it.
MF Bliki: AnecdotalEvidence
Some people would extend this – saying you can’t really talk about an idea until you’ve seen it on multiple projects. While this is nice, I don’t agree that it’s necessary. Just reporting on one particular thing you discovered on a project is useful because it provides raw material for others. Someone else might be in a similar position and your idea gives them something to try. Someone else may have done a similar thing and when they write about their experiences they report theirs on top of yours.
I’ve always been a bit hesitant to share stuff I’m in the middle of doing. Revisions throughout the process often make the end result quite different from where I started. Instead of holding back until I’ve found something I like, I’ll start talking about stuff I’m in the middle of. Hopefully you’ll help me out as you have advice for me to take.