我已经定义了一组表 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 ?