如何在MySql

时间:2016-03-02 18:49:30

标签: mysql database hibernate

如果我有多个对象,任何人都可以映射到多个其他对象。

A -> B, C, D, E
B -> A, E, F, C
C -> B

如何将这些实体存储在MySql中并使用JPA + Hibernate来管理它们。例如,如果我搜索“A”,它将返回“B,C,D,E”,如果我搜索“C”,它将返回“B”等。

编辑:

按照建议,我做了以下事情:

[test]> describe relation;
+----------+---------+------+-----+---------+----------------+
| Field    | Type    | Null | Key | Default | Extra          |
+----------+---------+------+-----+---------+----------------+
| id       | int(11) | NO   | PRI | NULL    | auto_increment |
| id_start | int(11) | YES  |     | NULL    |                |
| id_end   | int(11) | YES  | MUL | NULL    |                |
+----------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

MariaDB [test]> describe entity;
+-------+------------+------+-----+---------+----------------+
| Field | Type       | Null | Key | Default | Extra          |
+-------+------------+------+-----+---------+----------------+
| id    | int(11)    | NO   | PRI | NULL    | auto_increment |
| name  | varchar(5) | YES  |     | NULL    |                |
+-------+------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

MariaDB [test]> select * from entity;
+----+------+
| id | name |
+----+------+
|  1 | A    |
|  2 | B    |
|  3 | C    |
|  4 | D    |
|  5 | E    |
|  6 | F    |
+----+------+
6 rows in set (0.00 sec)

MariaDB [test]> select * from relation;
+----+----------+--------+
| id | id_start | id_end |
+----+----------+--------+
|  1 |        3 |      1 |
|  2 |        2 |      1 |
|  3 |        4 |      1 |
|  4 |        5 |      1 |
|  5 |        1 |      2 |
|  6 |        5 |      2 |
|  7 |        6 |      2 |
|  8 |        3 |      2 |
|  9 |        2 |      3 |
+----+----------+--------+
9 rows in set (0.00 sec)



   [test]> select E.name, R.id_start from relation R join entity E on E.id=R.id_end where E.name="A";
+------+----------+
| name | id_start |
+------+----------+
| A    |        3 |
| A    |        2 |
| A    |        4 |
| A    |        5 |
+------+----------+

问题:

此查询返回相关的“A”ID,这是正确的,但实际上不是B,C,D,E。如何从实体表中获取实际的相关名称?是数据库设计还是SQL语句错误?

0 个答案:

没有答案