spring-data-jpa 具有PK对象Map的Spring Data JPA XRef

hwamh0ep  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(211)

我很难让Spring Data将值保存到XREF表中。我试图与现有的DB对话,所以我不能更改模式,这将使这变得更容易。

课程

@Entity
@Table(name="user_type")
class UserType {
   @Id
   @Column(name="name")
   private String name;
}

@Entity
@Table(name="user_role")
class UserRole {
   @Id
   @Column(name="name")
   private String name;
}

@Entity
@Table(name="company")
class Company{
   @Id
   @Column(name="id")
   private UUID id;

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

@Entity
@Table(name="user")
class User {
   @Id
   @Column(name="id")
   private UUID id;

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

   @ManyToOne
   @JoinTable
   private UserType userType;

   //not sure what to do here or which JoinTable/ManyToOne/Etc
   private UserCompanyAccess userCompanyAccess;
}

@Entity
@Table(name="user_company_access")
class UserCompanyAccess {

    @EmbeddedId
    private UserCompanyAccessId userCompanyAccessId;

    // not sure of relationships here either
    private User user;
    private Company company;
    private UserRole userRole;

    @Embeddable
    static class UserAccessCompanyId implements Serializable {
        @Column(name="id")
        private UUID id;
    } 
}

我尝试了许多不同的@JoinColumns组合来指定用户(id)和用户访问公司(user_id)以及company。代码编译,但在运行时休眠抛出一个错误,说company_id没有提供或其他随机异常,试图比较uuid和字符变化等,这真的很奇怪。任何帮助将不胜感激。我已经做了许多@OneToMany/@ManyToOne/@ ManyToMany,但不知何故,从来没有通过XREF表的Spring Data ,其中PK不是2个连接表的组合。这就是我感到困惑的地方。否则,@EmbeddedId将是两者的组合。

zvokhttg

zvokhttg1#

电子表格的深棕色部分令人困惑,因为它表明有一个您在其他地方没有提到的CompanyUser表。这是一个问题吗?
无论如何,很遗憾您不能调整模式,因为这种设计有很多缺陷。但是,它仍然可以使用。我对Java和Spring/ Boot 有点陈旧,我没有一个有用的方法来轻松测试它们,但您可能会有更好的运气:

@Entity
@Table(name="user")
class User {
    @Id
    @Column(name="id")
    private UUID id;

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_type", table="user_type", referencedColumnName = "name")
    private UserType userType;
}

@Entity
@Table(name="user_type")
class UserType {
    @Id
    @Column(name="name")
    private String name;
}

@Entity
@Table(name="user_role")
class UserRole {
    @Id
    @Column(name="name")
    private String name;
}

@Entity
@Table(name="company")
class Company{
    @Id
    @Column(name="id")
    private UUID id;

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

@Entity
@Table(name="user_company_access", uniqueConstraints = {
        @UniqueConstraint(columnNames = { "user_id", "company_id" })
})
class UserCompanyAccess {

    @Id
    @Column(name="id")
    private UUID id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "role", table="user_role", referencedColumnName = "name")
    private UserRole userRole;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", table="user", referencedColumnName = "id")
    private User user;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", table="company", referencedColumnName = "id")
    private Company company;
}

说明:user_company_access表是所有内容最终联系在一起的地方,它的设计使得用户和公司的任何特定组合都是唯一的,而@UniqueConstraint在代码中实现了这一点;实际上并不需要一个可嵌入对象。

相关问题