Friday, April 16, 2010

Hibernate ManyToOne Association With Annotations

It took me a while to get rid of a:
ERROR org.hibernate.util.JDBCExceptionReporter -
Unknown column 'applicatio0_.domain_domainID' in 'field list'

on a simple many-to-one association.
I had an Application object with a Domain field where many Applications could have the same Domain (standard many-to-one association).
The annotations on the field were:

@ManyToOne(cascade = CascadeType.ALL)
public Domain getDomain() {
return domain;
}

Class Domain started with:

@Entity
@Table(name="domain")
public class Domain implements Serializable {
// DB name: domainID
private Long id;
private String name;

Hibernate couldn't correctly map the domain ID until I added the JoinColumn annotation:

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="domainID")
public Domain getDomain() {
return domain;
}

which in my opinion could be inferred from class Domain:

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "domainID", unique = true,
updatable = false, nullable = false)
public Long getId() {
return this.id;
}

but it wasn't.
Always use JoinColumn.

1 comment:

Gabriel said...

Hi man, im sorry did you find the solution about this? i mean i have the same problem.