Tagebuch eines Technikers

Saturday, February 21, 2009

Many-to-one and tinyblob

I had the weirdest bug the other day. I was creating a data model for a Java application using JPA and Hibernate as provider.

I was implementing a many-to-one-relationship. Even though I'm no pro at JPA, I never experienced big trouble. Until now. Suddenly, my relations did not turn out in the database model I expected. Instead of creating a foreign key, I always had a tinyblob field. The relevant parts of the class were something like:

protected FinanceProfile financeProfile;

// ...

@ManyToOne
public void setFinanceProfile(FinanceProfile financeProfile) {
this.financeProfile = financeProfile;
}

public FinanceProfile getFinanceProfile() {
return financeProfile;
}


I tried to use this class, but always received an error like:
org.hibernate.exception.DataException: could not insert [com.mypackage.model.FinanceAccount][SQL: 0, 22001]
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'finance_profile' at row 1

Now, do you see the error? Because... well, I didn't. Took me several hours to figure it out. The culprit is... tadaa... I had switched the order of the getter/setter-methods. Well, Eclipse did this for me, and I didn't check. After I changed the order of the two methods (and put the annotation above the getter), everything was ok.

I think I should start annotating the members themselves. That way, this won't happen again (at least I hope so).

4 comments:

Anonymous said...

Jeez, did the same thing and struggled for hours on it too before I found this (thanks BTW).

Anonymous said...

O problema possivelmente é que não foi incluída a anotação @AccessType("field") na classe. Por isso, ele se baseia nos gets e sets. Colocando essa anotação e incluindo normalmente as anotações de relacionamentos nos atributos da classe, deve funcionar.

Anonymous said...

The problem occurs probably because you didn't declared the @AccessType("field") annotation in the class. Thus, the ddl creation is based on properties (gets and sets). Including such annotation will allow you to annotate the relationship attributes.

Vijay Raghavan said...

Thanks.. this did save my few brain cells.. all others were fried by this weird bug.. thanks to ur blog.. how did u even figure this out?