[英]JPA : defining a complex @OneToMany relationship
I've defined a set of tables t1, t2, ... tN: 我定义了一组表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 |
(...)
+-------------+------------------+------+-----+---------+----------------+
and a table that will store some comments about each records in the tables t1, ... tN: 还有一个将在表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 | |
(....)
+---------------+------------------+------+-----+---------+----------------+
Now how should I define an @Entity for the table 't1' .... 现在我应该如何为表't1'定义一个@Entity...。
@Entity(name="T1")
@Table(name="t1")
public class T1
{
(...)
public List<PostIt> getComments() { ... }
}
to retrieve all its comments from the table 'postit' as the expression should be 从表“ postit”检索其所有注释,因为表达式应为
select P from postit as P where P.foreign_table="t1" and P.foreign_id= :t1_id
Is there a specific JPA annotation to define this relation or should I inject (how?) an EntityManager in each instance of T1 and call a @NamedQuery ? 是否有特定的JPA注释定义此关系,还是应该在T1的每个实例中注入(如何?) EntityManager并调用@NamedQuery?
I think you should model tables using inheritance . 我认为您应该使用继承对表进行建模。
So that you have AbstractTable 这样就有了AbstractTable
@Entity
@DiscriminatorColumn(name="type_id")
@DiscriminatorValue("-1")
...
class AbstractTable {
@OneToMany(mappedBy="foreign_id")
public List<PostIt> getComments() { ... }
}
...
}
and also: 并且:
@Entity
@DiscriminatorValue("1")
...
class Table1 {}
@Entity
@DiscriminatorValue("2")
...
class Table2 {}
If you need bi-directional relation then you need to figure out how to refer from Postit entity to tables. 如果需要双向关系,则需要弄清楚如何从Postit实体引用表。 I think somothing like 我觉得像
@ManyToOne
public AbstractTable getTable() { ... }
should work. 应该管用。
More info can be found here http://wleeper.com/2009/12/17/jpa-onetomany-with-inheritance/ 可以在这里找到更多信息http://wleeper.com/2009/12/17/jpa-onetomany-with-inheritance/
Your design corresponds to an inheritance tree, where all the subclasses of the root AbstractComment entity would be stored in the same table, using a discriminator column ( foreign_table
): 您的设计与一棵继承树相对应,在该继承树中,使用Discriminator列( foreign_table
)将根AbstractComment实体的所有子类存储在同一表中:
@Entity
@Table(name = "postit")
@Inheritance(strategy = SINGLE_TABLE)
@DiscriminatorColumn(name = "foreign_table", discriminatorType = STRING)
public abstract class AbstractComment {
// ... fields, getters, setters
}
@Entity
@DiscriminatorValue("t1")
public class T1Comment extends AbstractComment {
}
@Entity
@Table(name="t1")
public class T1 {
@OneToMany
@JoinColumn(name = "foreign_id")
private Set<T1Comment> comments;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.