テーブルのセットt1、t2、... tNを定義しました:
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アノテーションはありますか、それともT1の各インスタンスにEntityManagerを注入して@NamedQueryを呼び出す必要がありますか?