1

我是 JPA 的新手,我在创建一些实体时遇到了一些困难。

在我正在构建的应用程序中,可以根据数据库中定义的某些区域和子区域对某些实体进行分类。

相关的表格是这四个:

1)分类

+-------------+-----------------------+------+-----+---------+----------------+
| Field       | Type                  | Null | Key | Default | Extra          |
+-------------+-----------------------+------+-----+---------+----------------+
| ID          | int(11) unsigned      | NO   | PRI | NULL    | auto_increment |
| pID         | int(11) unsigned      | NO   | MUL | NULL    |                |
| reference   | varchar(300)          | NO   |     | NULL    |                |
| link        | varchar(255)          | YES  |     | NULL    |                |
+-------------+-----------------------+------+-----+---------+----------------+

2) cls_area_map

+---------+------------------+------+-----+---------+----------------+
| Field   | Type             | Null | Key | Default | Extra          |
+---------+------------------+------+-----+---------+----------------+
| class   | int(11) unsigned | NO   | MUL | NULL    |                |
| idarea  | int(11) unsigned | NO   | MUL | NULL    |                |
| subarea | int(11) unsigned | YES  | MUL | NULL    |                |
| id      | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
+---------+------------------+------+-----+---------+----------------+

3) 面积

+--------+------------------+------+-----+---------+----------------+
| Field  | Type             | Null | Key | Default | Extra          |
+--------+------------------+------+-----+---------+----------------+
| idarea | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| label  | varchar(255)     | NO   | UNI | NULL    |                |
+--------+------------------+------+-----+---------+----------------+

4) 分区

+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| area_idarea | int(11) unsigned | NO   | MUL | NULL    |                |
| label       | varchar(255)     | NO   | UNI | NULL    |                |
| ID          | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
+-------------+------------------+------+-----+---------+----------------+

classification我存储一般分类信息,并在cls_area_map我尝试将一般信息连接到分类区域(在area和中定义subarea)。

当我尝试将分类区域映射信息添加到我Classification和我的Cls_area_map实体类时,我遇到了麻烦。

我得到错误:

An incompatible mapping has been encountered between [class Entity.Classification] and [class Entity.Cls_area_map]. This usually occurs when the cardinality of a mapping does not correspond with the cardinality of its backpointer..

我不确定我在基数上做错了什么。这是我Classification为创建关系而添加的内容:

@OneToMany(mappedBy = "id")
private List<Cls_area_map> cls_area;

并在Cls_area_map

@JoinColumn(name = "class",referencedColumnName = "ID")
@ManyToOne(optional=false)
private Classification classy;

任何解释/提示?

(这是什么意思backpointer?)

4

1 回答 1

2

mappedBy表示这一侧的实体是关系的逆。所以应该使用实体名称而不是外键。

医生说

mappedBy 指的是所有者侧关联的属性名称。

在你的情况下它很优雅,所以使用

@OneToMany(mappedBy = "classy")
private List<Cls_area_map> cls_area;

也可以看看:

映射依据

于 2014-02-04T09:19:38.623 回答