我是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
是什么意思?)
答案 0 :(得分:1)
mappedBy
表示此方面的实体是关系的倒数。因此,应使用实体名称而不是外键。
医生说
mappedBy是指所有者上关联的属性名称 侧。
在您的情况下它很优雅,所以请使用
@OneToMany(mappedBy = "classy")
private List<Cls_area_map> cls_area;
另见: