java - REST Spring Boot 中的复合主键用法

标签 java spring rest spring-boot

我从未发现此表格对我有帮助。我希望是现在。

在我的 spring-boot 中,我的数据库有两个主键

TransId and PrsnRef

现在,当我为名为 PersonCore 的数据库表创建实体时,我将获得 2 个类

PersonCore (As known this has all rest of data)

/**
 * The persistent class for the PERSON_CORE database table.
 * 
 */
@Entity
@Table(name="PERSON_CORE")
@NamedQuery(name="PersonCore.findAll", query="SELECT f FROM PersonCore f")
public class PersonCore implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private PersonCorePK id;

    @Column(name="PRSN_AGE")
    private BigDecimal prsnAge;

    @Column(name="PRSN_AMER_INDIAN_ALASKA_NTV")
    private String prsnAmerIndianAlaskaNtv;

    @Column(name="PRSN_DOB")
    private String prsnDob;

PersonCorePK (This has two Id's transid and personref)

    /**
 * The primary key class for the PERSON_CORE database table.
 * 
 */
@Embeddable
public class PersonCorePK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="TRANS_ID")
    private String transId;

    @Column(name="PRSN_REF")
    private String prsnRef;

    public FdshPersonCorePK() {
    }
    public String getTransId() {
        return this.transId;
    }
    public void setTransId(String transId) {
        this.transId = transId;
    }
    public String getPrsnRef() {
        return this.prsnRef;
    }
    public void setPrsnRef(String prsnRef) {
        this.prsnRef = prsnRef;
    }

我正在尝试使用 spring-boot 创建 RESTFul 服务 我使用 @RepositoryRestResource(bla bla bla) 创建了存储库

@RepositoryRestResource(path = "person", collectionResourceRel = "FdshPersonHDR")
public interface PersonCoreRepository extends PagingAndSortingRepository<PersonCore, PersonCorePK> {
    Collection<PersonCore > findAll();
    Collection<PersonCore > findAllByIdTransIdAndIdPrsnRef(@Param("transid")String transId, @Param("prsnRef") String prsnRef);
}

Spring boot 太棒了,把我照顾得很好。 让我们看看...

当我尝试从数据库中获取所有记录时,一切都很好,除了最终的链接是这样的

http://localhost:8080/person/com.----.Application.Methods.PersonCorePK@83e14116

这让我发疯。我知道它读的是我想要的内容,但没有准确地向我展示我需要的内容。我需要帮助弄清楚如何转换该 "com.----.Application.Methods.PersonCorePK@83e14116"。它实际上应该显示该记录的 transid。

请告诉我是否可以将这两个复合键添加到该 o/p 格式中,因为我正在获取我想要的所有数据,除了这两个对我来说很重要的数据。我不确定是否可以在单个实体中使用两个@Id。但我会检查一下。

Sample Screenshot

最佳答案

您拥有的是对 PersonCorePK 对象调用 toString() 的输出。如果要更改此对象表示为字符串的方式,则需要重写 PersonCorePK 类的 toString() 方法。像这样的事情:

编辑:新的 toString() 方法。

@Override
public String toString() {
    return "/search/findAllByIdTransIdAndIdPrsnRef?transId="+transId+"&prsnRef="+prsnRef;
}

我尝试过它对我有用。放慢速度并确保您了解正在发生的事情。另请注意,我将 (@Param("transid") 更改为 (@Param("transId") 并使用大写的 I

此外,我还为 PersonCorePK 添加了一个构造函数:

public PersonCorePK(String transId, String prsnRef) {
    this.transId = transId;
    this.prsnRef = prsnRef;
}

最后,当我测试应用程序时,我确保将 application/jsonContent-Type 放入 POST 请求中,以便系统理解它正在接收的数据。

我用了Accessing JPA Data with REST作为指导。

关于java - REST Spring Boot 中的复合主键用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35708682/

相关文章:

java - azure 函数在 IntelliJ IDEA 中调试错误

java.sql.SQLException : Too many connections (MariaDB)

rest - Watson Visual Recognition 创建分类器 413 请求实体太大

rest - Outlook.com REST API - 在没有动态登录的情况下获取 token

java - 如何在 Java 中针对此 LDAP 进行 LDAP 搜索/身份验证

java - 如何从 catch 语句中的 Java 异常对象中检索多个 SSMS 查询错误消息?

java - 使用 Swagger 进行身份验证登录

spring - 如何在 Spring 中注入(inject)基于类型安全限定符的 bean 类型映射?

spring - ComponentScan 和 Autowired 不适用于依赖的 Spring 项目?

java - 在 Spring 中,可以从 @Controller 类中的单个方法调用两个不同的服务吗?