(下面簡化的例子)休眠 - 使用@SecondaryTable,@OneToMany
我試圖映射與2代DB表一個POJO類,並且還創建一對多關聯。下面是類的樣子..
@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;
..
}
下面是表..
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.
正如你所知道的,我想店親子關係在不同的表。 現在,當我嘗試在ExternalData上創建()而不設置子節點時,它可以工作。 當我嘗試獲取此創建使用ExternalData.getChildren()我得到下面的異常數據..
[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'
我在做什麼錯?
爲代碼片段添加@OneToMany。作爲撰寫描述的一部分,它被錯過了。 –