【问题标题】:Composite Primary Key usage in REST Spring BootREST Spring Boot 中的复合主键用法
【发布时间】:2016-02-29 20:09:55
【问题描述】:

我从来没有看到这个表格对我有帮助。我希望是现在。

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

TransId 和 PrsnRef

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

PersonCore(众所周知,这包含所有其余数据)

/**
 * 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(这有两个Id的transid和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

【问题讨论】:

标签: java spring rest spring-boot


【解决方案1】:

您所拥有的是对PersonCorePK 对象的toString() 调用的输出。如果要更改此对象表示为 String 的方式,则需要重写 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 为指导。

【讨论】:

  • 我覆盖 transid 字符串。但是,当我尝试使用该链接进行重定向时,我收到了这个错误。“org.springframework.core.convert.ConverterNotFoundException:没有找到能够从 [java.lang.String] 类型转换为 [com..Methods. PersonCorePK]"
  • 请练习你的语法。请参阅答案中的编辑。如上所述,toString 方法位于 PersonCorePK 类中。
  • 太棒了。你是我的英雄尼古拉斯...... :) 对不起我的语法差。
  • 不客气,如果对您有帮助,请采纳。
  • 现在我知道如何接受 Nichol 的回答了。对不起!不过谢谢。不过。
猜你喜欢
  • 1970-01-01
  • 2020-07-13
  • 2019-09-01
  • 1970-01-01
  • 2017-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
相关资源
最近更新 更多