@ManyToMany without join table (legacy database)

前端 未结 2 709
栀梦
栀梦 2020-12-11 03:07

I have to apply JPA in a legacy database with an awful design. Unfortunately is not possible to change it. Luckily is only for read-only access.

One of the stranges

相关标签:
2条回答
  • 2020-12-11 03:41

    You can try to map it as two read-only one-to-many relationships:

    public class User {
        @Column(name = "ACCESS_GROUP")
        private Integer group;
    
        @OneToMany
        @JoinColumn(name = "ACCESS_GROUP", referencedColumnName = "ACCESS_GROUP",
            insertable = false, updateable = false)
        private List<Access> accessList;
        ...
    }
    
    public class Access {
        @Column(name = "ACCESS_GROUP")
        private Integer group;
    
        @OneToMany
        @JoinColumn(name = "ACCESS_GROUP", referencedColumnName = "ACCESS_GROUP",
            insertable = false, updateable = false)
        private List<User> userList;
        ...
    }
    
    0 讨论(0)
  • 2020-12-11 03:41

    I thiknk the many to many definition suits better in this case than the one to many approach. Although you can choose either but with different behaviour and performance. Reading the javax documentation got through Eclipse, the propper mapping for the without jointable case like that should be:

    public class User {
    
      @Column(name = "ACCESS_GROUP")
      private Integer group;
    
      @ManyToMany(targetEntity=Access.class)
      private List<Access> accessList;
    ...
    }
    
    public class Access {
    
      @Column(name = "ACCESS_GROUP")
      private Integer group;
    
      @ManyToMany(targetEntity=User.class, mappedBy="accessList")
      private List<User> userList;
    ...
    }
    
    0 讨论(0)
提交回复
热议问题