简体   繁体   中英

JPA - 2 @ManyToMany relationships between 2 entities (one of them with extra Entity)

I am trying to solve JPA problem. I have 2 main entities - CameraItem and Chain (which represents ordered list of cameras)

Now there have to be 2 @ManyToMany relationships between CameraItem and Chain.

  1. Each CameraItem has at least one parent Chain. As one CameraItem can belong to different Chains, and each Chain can have multiple CameraItems this is the first simple direct @ManyToMany relationship.

  2. Chains can be connected with each other via CameraItem. In other words, CameraItem is holding the connection between Chains. But this is not simple @ManyToMany relationship, because we also need information about direction of the Chains connection. So it is @ManyToMany relationship with new Entity as Baeldung describes here https://www.baeldung.com/jpa-many-to-many . Entity ConnectionPoint is holding the information about the direction as a String.

I paste the classes here:

CHAIN CLASS:

@Entity
@Table(name = "chain")
public class Chain {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @NotBlank(message = "Chain name is mandatory")
    private String name;

    @Column(name = "PLANT_NAME")
    private String plantName;

    private String description;

    private String status;

    private Boolean hasPlant;

    @CreationTimestamp
    @Column(name = "creation_time")
    private LocalDateTime creationTime;

    @OneToMany(mappedBy = "camera_item")
    private List<CameraItem> cameraItems = new ArrayList<>();

    @OneToMany(mappedBy = "chain")
    Set<ConnectionPoint> connectionPoints;

CAMERA ITEM CLASS:

@Entity
@Table(name = "camera_item")
public class CameraItem {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @ManyToOne
    @JoinColumn
    private Camera camera;

    private String name;

    private Integer positionInChain;

    @ManyToMany(mappedBy = "cameraItems", fetch = FetchType.LAZY)
    private List<Chain> parentChainIds;
    

    @OneToMany(mappedBy = "cameraItem")
    Set<ConnectionPoint> connectionPoints;

CONNECTION POINT CLASS:

@Entity
@Table(name = "connection_point")
public class ConnectionPoint {

    @Id
    private Long id;

    @Column(name = "direction")
    private String direction;

    @ManyToOne
    @JoinColumn(name = "chain")
    private Chain chain;

    @ManyToOne
    @JoinColumn(name = "camera_item")
    private CameraItem cameraItem;

When I run the application I get this error:

org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: no.trafsys.videodashboard.model.entity.CameraItem.camera_item in no.trafsys.videodashboard.model.entity.Chain.cameraItems

Does somebody know where the problem can be?

I use @OneToMany annotations in Chain and CameraItem entities and @ManyToOne in ConnectionPoint like Baeldung in his tutorial.

Thank you in advance for any help

@Entity
@Table(name = "chain")
public class Chain {
    //..

    @OneToMany(mappedBy = "camera_item")
    private List<CameraItem> cameraItems = new ArrayList<>();

    //..
}

mappedBy parameter can only be in one side of the relation. I suspect camera_item is database table column name. So your cameraItems needs @JoinTable(name = "camera_item"... annotation

I don't think there is issue in ConnectionPoint . I think the issue is that:

In Chain class,

@OneToMany(mappedBy = "camera_item")  // One-to-Many defined here
private List<CameraItem> cameraItems = new ArrayList<>();

while in CameraItem class, corresponding property is defined as follow:

@ManyToMany(mappedBy = "cameraItems", fetch = FetchType.LAZY) // Many-To-Many
private List<Chain> parentChainIds;

Try changing the mapping type to @ManyToMany in Chain class as well. It might work.

PS: I am not entirely sure of this, but this feels like the issue[incorrect mapping type]. Wanted to add this as a comment, but due to space issues, adding it as an answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM