我已經定義了一組表T1,T2的複雜關係@OneToMany,... TN:JPA:定義
mysql> desc t1;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
(...)
+-------------+------------------+------+-----+---------+----------------+
(...)
mysql> desc tN;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
(...)
+-------------+------------------+------+-----+---------+----------------+
和一張桌子,將存儲一些評論有關在每個記錄表T1,... TN:
mysql> desc postit;
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| foreign_id | int(10) unsigned | NO | MUL | NULL | |
| foreign_table | varchar(20) | NO | MUL | NULL | |
| content | text | NO | | NULL | |
(....)
+---------------+------------------+------+-----+---------+----------------+
現在我應該怎麼定義表 'T1' 的@Entity ....
@Entity(name="T1")
@Table(name="t1")
public class T1
{
(...)
public List<PostIt> getComments() { ... }
}
來檢索表「postit」作爲表達其所有的意見應該是
select P from postit as P where P.foreign_table="t1" and P.foreign_id= :t1_id
是否有一個具體的JPA註解來定義這種關係,或者我應該注入(如何?)的的EntityManager中的每個實例T1並調用@NamedQuery?
好吧,據我所知,我需要爲我的'N'表創建'N'類。謝謝。 – Pierre