a-sak
a-sak

Reputation: 1340

Hibernate -- Using @SecondaryTable, @OneToMany

(Simplified Example below)

I'm trying to map one POJO class with 2 DB tables and also creating a OneToMany association. Here is how the class looks like..

@Entity
@Table(name = "external_data")
@SecondaryTable(name = "external_data_hierarchy")
public class ExternalData  {

  private int externalDataId;

  @OneToMany(fetch = FetchType.LAZY)
  @JoinTable(name = "external_data_hierarchy")
  @JoinColumn(name = "external_data_parent_id")
  private List<ExternalData> children;
..

}

Here are the tables..

mysql> desc external_data;
+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| external_data_id | int(11)      | NO   | PRI | NULL    | auto_increment |
+------------------+--------------+------+-----+---------+----------------+
5 rows in set (0.06 sec)

mysql> desc external_data_hierarchy;
+----------------------------+---------+------+-----+---------+----------------+
| Field                      | Type    | Null | Key | Default | Extra          |
+----------------------------+---------+------+-----+---------+----------------+
| external_data_hierarchy_id | int(11) | NO   | PRI | NULL    | auto_increment |
| external_data_parent_id    | int(11) | YES  | MUL | NULL    |                |
| external_data_child_id     | int(11) | YES  | MUL | NULL    |                |
+----------------------------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

Note: "external_data_parent_id" and "external_data_child_id" columns refer external_data.external_data_id column.

As you can tell, I'm trying to store parent-child relationship in a different table. Now, when I try create() on ExternalData without setting the children, it works. When I try to fetch this created data using ExternalData.getChildren() I get the following exception..

[main] WARN  in [org.hibernate.util.JDBCExceptionReporter.logExceptions(JDBCExceptionReporter.java:100)] - SQL Error: 1054, SQLState: 42S22
[main] ERROR in [org.hibernate.util.JDBCExceptionReporter.logExceptions(JDBCExceptionReporter.java:101)] - Unknown column 'externalda0_1_.external_data_id' in 'on clause'

What am I doing wrong?

Upvotes: 2

Views: 2836

Answers (1)

JB Nizet
JB Nizet

Reputation: 691765

It's not clear which relation there is between the tables. What's the use of the external_data_child_id column?

What I can say is that:

  • You don't have a @OneToMany annotation on the children field
  • This OneToMany is either mapped thanks to a join column, or thanks to a join table. Not both.
  • Since you're using a secondary table, you should specify in which table each column or JoinColumn is, by using the table attribute of the annotation (or at least, you should specify it when the column or join colums is not in the primary table).

See http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#mapping-declaration-join for explanations on how SecondaryTable should be used, and http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#collections-mapping for explanations on OneToMany associatios.

Upvotes: 2

Related Questions