Spring boot MappedSuperclass中的OneToMany

Spring boot MappedSuperclass中的OneToMany,spring-boot,hibernate,spring-data-jpa,Spring Boot,Hibernate,Spring Data Jpa,我试图使用@MappedSuperclass在本文后面()的两个实体之间共享同一个表 因此,我有以下三个课程: @MappedSuperclass abstract class UserDao { @Id @Column(name = "username", nullable = false, unique = true) var username: String? = null @OneToMany(fetch = FetchType.E

我试图使用
@MappedSuperclass
在本文后面()的两个实体之间共享同一个表

因此,我有以下三个课程:

@MappedSuperclass
abstract class UserDao {

    @Id
    @Column(name = "username", nullable = false, unique = true)
    var username: String? = null

    @OneToMany(fetch = FetchType.EAGER)
    var groups: Set<GroupDao>? = null

}
以及:

我尝试的是在不需要用户配置文件时保存一些加载用户配置文件的查询开销,但现在当我尝试运行应用程序时,我得到以下错误:

could not execute statement; SQL [n/a]; constraint [full_auth_user_username" of relation "users_groups];

不确定Hibernate为什么创建此关系,因为它们共享同一个表。

我建议您不要在实体级别共享类型。当涉及多个这样的实体时,共享一对多关联可能不会像您预期的那样在刷新/同步方面起作用。在我看来,您应该尝试DTO方法

我认为这是一个完美的用例

我创建了这个库,以便在JPA模型和自定义接口或抽象类定义的模型之间进行简单的映射,类似于类固醇上的Spring数据投影。其思想是以您喜欢的方式定义目标结构(域模型),并通过JPQL表达式将属性(getter)映射到实体模型

在Blaze持久性实体视图中,您的用例的DTO模型可能如下所示:

@EntityView(User.class)
public interface BasicUserDao {
    @IdMapping
    String getUsername();
    Set<GroupDao> getRoles();

    @EntityView(Group.class)
    interface GroupDao {
        @IdMapping
        Long getId();
        String getName();
    }
}
@EntityView(User.class)
public interface FullUserDao extends BasicUserDao {
    @Mapping("profileJpa")
    ProfileDao getProfile();

    @EntityView(Profile.class)
    interface ProfileDao {
        @IdMapping
        Long getId();
        String getName();
    }

}
最好的一点是,它只获取实际需要的数据

could not execute statement; SQL [n/a]; constraint [full_auth_user_username" of relation "users_groups];
@EntityView(User.class)
public interface BasicUserDao {
    @IdMapping
    String getUsername();
    Set<GroupDao> getRoles();

    @EntityView(Group.class)
    interface GroupDao {
        @IdMapping
        Long getId();
        String getName();
    }
}
@EntityView(User.class)
public interface FullUserDao extends BasicUserDao {
    @Mapping("profileJpa")
    ProfileDao getProfile();

    @EntityView(Profile.class)
    interface ProfileDao {
        @IdMapping
        Long getId();
        String getName();
    }

}
@Repository
interface UserRepository {
    List<BasicUserDao> findAll();
}