Sunday, July 18, 2010

Hibernate (2)

We use dbunit to populate the database with some data in testing phase. I tried to generate some new dbunit export with new "order" value. It always fail to load, I have to revert the data back to the time without any order data. It said it could not find the new column for "order". I finally decided to sit down and work on it. After maybe 20 minutes guessing, I figure it is probably the maven-hibernate3 plugin generated the ddl without those column. I delete this plug-in and it goes well.

I think I also found a bug in their documentation. In their example,
(3.5.1Final), the id field is declared non-nullable.

@Entity
public class Parent {
@OneToMany
@OrderColumn(name="order")
@JoinColumn(name="parent_id", nullable=false)
private List<Child> children;
...
}

@Entity
public class Child {
...
@ManyToOne
@JoinColumn(name="parent_id", insertable=false, updatable=false, nullable=false)
private Parent parent;
...
}

I have something very similar, a MirexSubmission with a list of MirexNote's. I also make the cascade from MirexSubmision.


@Entity
@Table(name="mirex_submission")
public class MirexSubmission implements Serializable {
...
@OneToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.EAGER)
@OrderColumn(name="note_order")
@JoinColumn(name="submission_id")
public List<MirexNote> getNotes() {
return notes;
}

.....
}



@Entity
@Table(name="mirex_note")
public class MirexNote implements Serializable{
...
@ManyToOne
@JoinColumn(name="submission_id",insertable=false, updatable=false,nullable=true)
public MirexSubmission getSubmission() {
return submission;
}
...
}


However, it always give me some error when I try save (Hibernate merge()) about "no default value for "submission_id" column. It does not matter I merge the MirexNote or MirexSubmission object, in which case it cascade to MirexNote. I check the schema, "submission_id" column does not allow null value, but with out the default. And obviously, it is no wonder the insert on this table fail. Finally I change to nullable=true and things worked out.

Correction (Aug. 3rd): It seems to work fine if both ends (many-to-many, one-to-many) set nullable=false.

Friday, July 16, 2010

Hibernate

Recently I start to build an sub system for MIREX submission. It is not too complicated, it should accept submission of programs with certain metadata, such as contributors, notes, etc... And Mirex runner (NEMA system, or a human runner) should be able to change the status, leave note and inform the submitter. It is not simple when you need to consider every detail. And I have already spent about two weeks on it.

One thing I need to learn is Hibernate. We used that already in our system. But Amit and others have implemented it and provided me with a DAO object. So I have no direct interaction with it. This time, I have to write every bit from the scratch. Well, that is not totally true. I have their old code for reference. I use the annotation mapping, and spring hibernate template. I spent quite some time to read "Java Persistence with Hibernate", not the best choice. The book was written by one of the authors of Hibernate, and is very detailed and not really good for the first-timer. After couple of days, I finished about half the books and started to read the hibernate documentation from their site. It turns out much more accessible. It has some good example and explains things pretty clearly and I really like it.

Today, I realize that I need to have bi-direction many-to-one and many-to-many association with list. The order is important. I looked though the book and again, the documentation gives me an easier answer. @OrderColumn. And here comes the problem. We are using a quite mixed version of hibernate, some modules use 3.2.6, some uses 3.3.0GA and some use 3.4.0GA, with JPA 1. None of them support @OrderColum. It took me a while to figure out that this is the new annotation for JPA2. I have to upgrade hibernate in order to use this feature. Otherwise, I can use some hibernate annotation. I figured to whatever, just upgrade. So the latest 3.5.1Final we go.

The first problem I run into is that I cannot find it in the main maven repoistory. After some search, I found it is only in JBoss's repository. I need to add it into our own proxy repository server. I have to ask Amit for the username/password for it. After some talk, I found out we have already had it in the proxy list. I SHOULD HAVE tried before ask...

And soon, I found the module structure changes. Hibernate-annotation depends on hibernate-core. So I only need to include hibernate-annotation. But soon I run into the next problem, Spring cannot build the session-factory for Hibernate, it cannot find some classes about slf4j. I am confused. Hibernate already has slf4j dependence. After some 20 minutes on google, I finally figured out that Hibernate has slf4j-api. But I also need to make my code depends on slf4j12. So include it and I got new different error.

This time is ehcache. The old code has hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider. I looked at hibernate-core.jar, and sure, it dose not have such class. After a while, I realized it might not in core. I check the repository, there is one module, hibernate-ehcache. Here we go. But again, new problem pops up.

It is something about assistance. I am confused again. Hibernate does have dependency on javaassistence. A close look says that is is in test scope. Maybe that is the reason. Add the new dependency, and finally my unit tests sort of run.

My old database schema does not work with the new ordercolumn. The project has dependency on hibernate3-maven plug-in. I used to just copy their output ddl into mysql to generate the new database. But this time after I import the ddl, the test always tell me some field "note_Orders" is missing. I am really confused. I gives the @OrderColumn a new name. After run the maven test several times and I started to read the ddl more carefully and sure, the order columns are missing. WTF!!! Well, it turns out that hibernate3-maven plug-in depends on hibernate-3.4.0GA. It is probably does not understand this new annotation.

I add hibernate.hbm2ddl.auto=update into the hibernate config. And it seems to update my schema fine.

WHEW, quite some work!

Thursday, July 1, 2010

Java enum

Java 5 enum is nice, but it is not terribly well documented. The useful information is scattered around. I did a careful study maybe 2 years ago but I am a bit loss when I try to look around for its information now. So maybe it is better to document everything I need here for later reference.


A few useful links:
Type-Safe Enumerations
Using the built-in enumeration methods: values( )
Sun's Tutorial
Spring MVC form tags for enum