Tagebuch eines Technikers

Showing posts with label hibernate. Show all posts
Showing posts with label hibernate. Show all posts

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

Monday, May 19, 2008

By-directional one-to-many relations with Hibernate and annotations

If have a POJO User and Home. One user can have multiple homes, and homes should know their user. Thus, one user should have many homes, and homes have one owner. Since I ran into some trouble while doing this, this is a short example of what to do. Another simple example (in German) helped me to come to a correct solution

My POJOs look like:
If have a POJO User and Home. One user can have multiple homes, and homes should know their user. Thus, one user should have many homes, and homes have one owner. Since I ran into some trouble while doing this, this is a short example of what to do. Another simple example (in German) helped me to come to a correct solution

My POJOs look like:

@Entity
@Table(name=\"app_user\")
public class User implements Serializable {

private Long id;

private Set<Home> homes = new HashSet<Home>();

/**
* Default constructor - creates a new instance with no values set.
*/
public User() {}

@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}

@OneToMany(mappedBy=\"owner\")
public Set<Home> getHomes()
{
return homes;
}

// setters, ...
}
and
@Entity @Table(name=\"home\")
public class Home extends BaseObject {

private Long id;


private User owner;


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}

@ManyToOne
@JoinColumn(name=\"user_id\")
public User getOwner() {
return owner;
}

// setters, ...
}
The relationship is saved in Home in the property owner (Home.owner). The foreign key is saved in the field user_id of table home.

When you get the error
mappedBy reference an unknown target
this means that the mappedBy is not set correctly. Ensure that the target is set correctly -- in this scenario, mappedBy specifies field owner of class Home as target.