An incompatible mapping has been encountered between [class Entity.Classification] and [class Entity.Cls_area_map]

StackOverflow https://stackoverflow.com/questions/21548025

  •  06-10-2022
  •  | 
  •  

Question

I'm new to JPA and I am having some difficulty creating some entities.

In the application I am building it is possible to classify some entities according to some area and subarea defined in a database.

The relevant tables are these four ones:

1) classification

+-------------+-----------------------+------+-----+---------+----------------+
| 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) area

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

4) subarea

+-------------+------------------+------+-----+---------+----------------+
| 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 |
+-------------+------------------+------+-----+---------+----------------+

In classification I store general classification information, and in cls_area_map I try to connect the general information to the areas of classification (defined in area and subarea).

When I try to add the classification-area mapping information to my Classification and my Cls_area_map entity classes I run into trouble.

I get the error:

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

I'm not sure what I'm doing wrong about the cardinality. This is what I added to Classification to create the relationship:

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

and in Cls_area_map:

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

Any explanations/hints?

(and what is meant by backpointer?)

Was it helpful?

Solution

mappedBy indicates that the entity in this side is the inverse of the relationship. So entity name should be used instead of foreign key.

The doc says

mappedBy refers to the property name of the association on the owner side.

It is classy in your case, so use

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

See also:

mappedBy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top