Pierre
Pierre

Reputation: 35236

JPA : defining a complex @OneToMany relationship

I've defined a set of tables 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:

 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' ....

@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

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 ?

Upvotes: 0

Views: 251

Answers (2)

JB Nizet
JB Nizet

Reputation: 691635

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):

@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;
}

Upvotes: 1

Michal
Michal

Reputation: 631

I think you should model tables using inheritance.

So that you have 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. 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/

Upvotes: 2

Related Questions