在 @ManytoMany 关系和具有额外列的查找表的情况下,无法检索 Spring HATEOAS 嵌入资源对象

2023-12-12

我无法检索嵌入的。我正在使用 Spring boot、spring data rest 和 spring JPA。我的数据库中有3张表

  • user
  • 能力
  • user_competency(带有额外列的连接/复合表)

User

@Entity
@Table(name = "\"user\"", schema = "public")
@JsonIdentityInfo(
          generator = ObjectIdGenerators.IntSequenceGenerator.class, 
          property = "userId")
public class User implements java.io.Serializable {

    private Long userId;

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)

    @Column(name = "user_id", unique = true, nullable = false)
    public Long getUserId() {
        return this.userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    private Set<UserCompetency> userCompetencies = new HashSet<UserCompetency>(0);

        @OneToMany(fetch = FetchType.EAGER,cascade = {CascadeType.ALL}, mappedBy = "user")
    public Set<UserCompetency> getUserCompetencies() {
        return this.userCompetencies;
    }

    public void setUserCompetencies(Set<UserCompetency> userCompetencies) {
        this.userCompetencies = userCompetencies;
    }

}

**Competency**



 @Entity
    @Table(name = "competency", schema = "public")
    @JsonIdentityInfo(
              generator = ObjectIdGenerators.IntSequenceGenerator.class, 
              property = "competencyId")
    public class Competency implements java.io.Serializable {


        private Long competencyId;
        private Set<UserCompetency> userCompetencies = new HashSet<UserCompetency>(0);

        @Id @GeneratedValue(strategy = GenerationType.IDENTITY)

        @Column(name = "competency_id", unique = true, nullable = false)
        public Long getCompetencyId() {
            return this.competencyId;
        }

        public void setCompetencyId(Long competencyId) {
            this.competencyId = competencyId;
        }

            @OneToMany(fetch = FetchType.LAZY, mappedBy = "competency")
        public Set<UserCompetency> getUserCompetencies() {
            return this.userCompetencies;
        }

        public void setUserCompetencies(Set<UserCompetency> userCompetencies) {
            this.userCompetencies = userCompetencies;
        }
    }   

用户能力

    @Entity
    @Table(name = "user_competency", schema = "public")
    @JsonIdentityInfo(
              generator =ObjectIdGenerators.IntSequenceGenerator.class, 
              property = "id")
    public class UserCompetency implements java.io.Serializable {
        private UserCompetencyId id;
        private Level level;
        private User user;
        private Competency competency;

        @EmbeddedId

        @AttributeOverrides({
                @AttributeOverride(name = "competencyId", column = @Column(name = "competency_id", nullable = false)),
                @AttributeOverride(name = "userId", column = @Column(name = "user_id", nullable = false)) })
        public UserCompetencyId getId() {
            return this.id;
        }

        public void setId(UserCompetencyId id) {
            this.id = id;
        }

        @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumn(name = "level_id")
        public Level getLevel() {
            return this.level;
        }

        public void setLevel(Level level) {
            this.level = level;
        }

        @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false)
        public User getUser() {
 return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @ManyToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name = "competency_id", nullable = false, insertable = false, updatable = false)
    public Competency getCompetency() {
        return this.competency;
    }

    public void setCompetency(Competency competency) {
        this.competency = competency;
    }
}

用户能力ID

@Embeddable
public class UserCompetencyId implements java.io.Serializable {

    private Long competencyId;
    private Long userId;

    public UserCompetencyId() {
    }

    public UserCompetencyId(Long competencyId, Long userId) {
        this.competencyId = competencyId;
        this.userId = userId;
    }


    @Column(name = "competency_id", nullable = false)
    public Long getCompetencyId() {
        return this.competencyId;
    }

    public void setCompetencyId(Long competencyId) {
        this.competencyId = competencyId;
    }

    @Column(name = "user_id", nullable = false)
    public Long getUserId() {
        return this.userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public boolean equals(Object other) {
        if ((this == other))
            return true;
        if ((other == null))
            return false;
        if (!(other instanceof UserCompetencyId))
            return false;
        UserCompetencyId castOther = (UserCompetencyId) other;

        return (this.getCompetencyId() == castOther.getCompetencyId()) && (this.getUserId() == castOther.getUserId());
    }    
}

用户能力库

  public interface UserCompetencyRepository extends JpaRepository<UserCompetency, UserCompetencyId> {

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>Demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>Demo</name>
    <description>Demo api </description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-hal-browser</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-hateoas</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.restdocs</groupId>
            <artifactId>spring-restdocs-mockmvc</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-webmvc</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

我想使用 URI 执行 GET,它返回嵌入的对象,但无法获取对象属性的实际值
GET http://localhost:8080/userCompetency

enter image description here


如何获取 userId=8 的用户和能力对象的属性值 需要帮助

After implementing suggested Projection Issue still not resolved and here is screen shot
enter image description here


一种方法是使用投影,例如:

@Projection(name = "edit" , types = Employee.class)
public interface EditEmployeeProjection {
    String getFirstName();
    String getLastName();
    Set<Project> getProjects();
}

这样,项目列表将嵌入到结果中http://localhost:8080/api/employee/1?projection=edit

如果您添加,将自动使用投影excerptProjection到您的存储库,如下所述:如何使用 Spring Data REST 和 HATEOAS 公开完整的树结构?

参见此处的示例:https://shinesolutions.com/2015/04/15/spring-data-rest-and-projections/

EDITED

在你的情况下,投影看起来像:

@Projection(name = "edit" , types = UserCompetency.class)
public interface UserCompetencyProjection {
    User getUser();
    Competency getCompetency();
}

With http://localhost:8080/userCompetency?projection=edit然后你就会看到想要的结果。

enter image description here

EDITED 2我使用的代码:

能力等级

@Entity
@Table(name = "competency", schema = "public")
@JsonIdentityInfo(
          generator = ObjectIdGenerators.IntSequenceGenerator.class,
          property = "competencyId")
public class Competency implements java.io.Serializable {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "competency_id", unique = true, nullable = false)
    private Long competencyId;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "competency")
    private List<UserCompetency> userCompetencies = new ArrayList<>();

用户能力.class

@Entity
@Table(name = "user_competency", schema = "public")
@JsonIdentityInfo(
          generator = ObjectIdGenerators.IntSequenceGenerator.class,
          property = "id")
public class UserCompetency implements java.io.Serializable {

    @EmbeddedId
    @AttributeOverrides({
        @AttributeOverride(name = "competencyId", column = @Column(name = "competency_id", nullable = false)),
        @AttributeOverride(name = "userId", column = @Column(name = "user_id", nullable = false)) })
    private UserCompetencyId id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false)
    private User user;

    @ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
    @JoinColumn(name = "competency_id", nullable = false, insertable = false, updatable = false)
    private Competency competency;

用户能力Id.class

@Embeddable
public class UserCompetencyId implements java.io.Serializable {

    @Column(name = "competency_id", nullable = false)
    private Long competencyId;

    @Column(name = "user_id", nullable = false)
    private Long userId;

用户能力存储库.class

@RepositoryRestResource(excerptProjection = UserCompetencyProjection.class)    
public interface UserCompetencyRepository extends JpaRepository<UserCompetency, UserCompetencyId> {

After Implementing This ,In my case its not working to show desired jason   [![enter image description here][2]][2]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 @ManytoMany 关系和具有额外列的查找表的情况下,无法检索 Spring HATEOAS 嵌入资源对象 的相关文章

随机推荐

  • 热通过cmd禁用buildnumber-maven-plugin

    我对 Maven 有疑问 如何通过命令行选项禁用 buildnumber maven plugin 我想在我们的持续集成服务器上运行 mvn test 命令 但是这个命令失败了 因为它试图构建一个版本并且没有访问我们的 vcs 的权限 在标
  • 用Java解析包含JS的HTML页面

    我正在尝试解析一个包含一些 JS 的网页 到目前为止我正在使用Jsoup在Java中解析html 它按预期工作 但我无法解析 JavaScript 下面是 HTML 页面的片段
  • grok 多条消息并用不同的标签处理它们

    我想在 Logstash 版本 2 4 中创建一个过滤器 在同一个 grok 中使用不同的匹配项 我想根据比赛添加不同的标签 基本上 我收到三种不同的消息模式 MAGIC 消息 REAL 消息 信息 我想做的是 grok match gt
  • 一个不错的 Java XML DOM 实用程序

    我发现自己一次又一次地编写同样冗长的 DOM 操作代码 Element e1 document createElement some name e1 setAttribute attr1 val1 e2 setAttribute attr2
  • 表格固定标题和可滚动正文

    我正在尝试使用 bootstrap 3 表制作一个具有固定标题和可滚动内容的表格 不幸的是 我发现的解决方案不适用于引导程序或搞乱风格 这里有一个简单的引导表 但由于某种原因我不知道 tbody 的高度不是 10px height 10px
  • ASP.NET 中的 XML POST 和解析

    如果有人将 XML 从应用程序发布到我的 ASP NET 页面 我如何解析它并以 XML 格式返回响应 将 XML 发布到我的 URL 的示例客户端代码 WebRequest req null WebResponse rsp null st
  • 升级 Android 项目中的领域

    我目前正在我的 Android 项目之一中运行 Realm 版本 0 82 0 我已经有一段时间没有接触 Realm 了 直到最近我注意到它们同时升级到了 2 0 2 版本 我想升级我的 Realm 版本 不幸的是 我不知道从旧版本升级到当
  • 在javafx中将纹理应用于网格

    我在使用 JavaFX 和 FXyz 0 1 1 将纹理应用到网格时遇到问题 I found 这个问题即使有详细的答案也无法弄清楚 我从头开始 准确地复制答案中的代码 场景是黑色的 没有可见的二十面体 我使用的是 Java 8 提供的图像是
  • 启动 asp.net 站点调试时 Visual Studio 2010 挂起

    我在 Windows 7 x64 上使用 Visual Studio 2010 时遇到问题 当我开始 ASP NET 站点调试时 它停止工作 唯一的解决方案是重新启动 IIS 有人有这方面的经验吗 Thanks 正如 Peter 提到的 通
  • 在 .NET Framework 4.0 中使用 TLS 1.2 时出现问题

    我禁用了 TLS 1 0 因此 我们尝试在使用 Net Framework 4 0 的 Net 应用程序中使用 TLS 1 2 我在开始时添加了此代码 System Net ServicePointManager SecurityProto
  • 如何在 JavaScript 中打开新选项卡而不切换到新选项卡?

    如何使用 javascript 打开新选项卡而不切换到新选项卡 例如 当用户单击链接时 将打开一个新选项卡 但用户应留在当前选项卡上 Web 浏览器会自动聚焦在新选项卡上 但您可以回调焦点 function openWindow url w
  • UITableViewCell 中的自动播放视频

    我已阅读了大部分问题StackOverflow用于自动播放视频 我可以在UITableView 但我遇到了一些如下所述的问题 视频开始时滚动会暂停一秒钟 视频播放前闪烁 如果向上滚动 视频不会自动播放 我想要的是像 Facebook 这样的
  • ElementList SimpleXML 中的空条目

    我的问题很简单 但我找不到任何相关信息 我有一个用于 XML 序列化的列表类和入口类 Root name entries public class List ElementList required false entry entry in
  • 如果您在页面中包含 2 个版本的 jQuery,如何将插件限制为仅使用其中一个?

    所以这个问题并不像乍听起来那么疯狂 我正在开发一段 JavaScript 代码以放入我的客户页面中 我担心的是他们是否在他们的网站上使用另一个版本的 jQuery 我知道从Jquery 文档像这样的事情应该有效 var dom dom qu
  • 在安装时创建没有管理员密码的 Windows 窗体应用程序设置

    我在 Visual Studio 中开发了一个 Windows 窗体应用程序 现在 我想在没有管理员权限的情况下在我的电脑上安装该 Windows 应用程序 我怎样才能创建一个设置 我已经在我的电脑上安装了 Photo Pad 图像编辑器应
  • Rc 依赖循环的最小示例是什么?

    我正在尝试编写一个泄漏内存的 Rust 程序由于具有引用计数的循环 下面的示例看起来应该会导致内存泄漏 但根据 Valgrind 的说法 它不会泄漏内存 是什么赋予了 test rs use std cell RefCell use std
  • 无法更新葫芦服务器版本

    更新 Calabash 服务器版本时出现问题 问题是我可以运行检查元素的基本测试 但是 一旦我尝试 触摸 按钮 葫芦就会返回 运行时错误 无法解析响应 该应用程序可能已崩溃 我相信问题的根源是我的服务器版本旧 不兼容 不是火箭科学 警告 服
  • Python 方法解析顺序

    有人可以解释给定代码的输出以及 python MRO 在这种情况下如何工作吗 class A object def go self print go A go class B A def go self super B self go pr
  • 解组切片中的 2 个不同结构

    我的输入 json 数据是这样的 无法更改 来自外部资源 Url test url Name testname FormName Test 2018 FormNumber 43 FormSlug test 2018 我有两个始终与数组中的数
  • 在 @ManytoMany 关系和具有额外列的查找表的情况下,无法检索 Spring HATEOAS 嵌入资源对象

    我无法检索嵌入的 我正在使用 Spring boot spring data rest 和 spring JPA 我的数据库中有3张表 user 能力 user competency 带有额外列的连接 复合表 User Entity Tab