Tagebuch eines Technikers

Sunday, February 22, 2009

Display result of select in vertical format

Sometimes the default way of displaying query results in tabular format can be quite annoying. Escpecially, when you have to view them i a small CL window. Fortunately, there is a workaroung:

SELECT * FROM USER\G

By replacing the default ; with \G, the result gets displayed in vertical format. Of course, this needs more vertical space in the CL window. On the other hand, you do not have to deal with annoying line wrapping and stuff.
Be sure to use a capital G though, otherwise this will not work and result in the default tabular view instead.

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).