使用Hibernate的Spring Boot应用程序如何与多个表进行交互,所有这些表的结构都相同?

我有一个名为“货币”的MySQL数据库,其中包含一百多个表,每个表用于一种货币,并且都具有相同的结构(2列,时间戳的日期时间和速率的浮点数)。


Spring Boot 应用程序如何与此结构交互?


我只想有一个带有字段和 getter/setter 方法的类 CurrencyRate。如果我标记这个类 ,那么我只能访问一个表。我已经研究了该标记,但它似乎仅适用于我们希望同时与多个表进行交互的情况,而不仅仅是一个表。@Entity@Table@SecondaryTable


import java.io.Serializable;

import java.util.Date;


import javax.persistence.Id;

import javax.validation.constraints.NotBlank;


import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import com.fasterxml.jackson.databind.ser.std.DateSerializer;

import com.fasterxml.jackson.databind.ser.std.NumberSerializers.FloatSerializer;


public abstract class CurrencyRate implements Serializable {


    @Id

    @JsonSerialize(using=DateSerializer.class)

    private Date datetime;


    @NotBlank

    @JsonSerialize(using=FloatSerializer.class)

    private float rate;


    Date getDate () { return this.datetime; }

    void setDate (Date timestamp) { this.datetime = timestamp; }

    float getRate () { return this.rate; }

    void setRate (float rate) { this.rate = rate; } 

}

我的想法是,我可以将CurrencyRate作为一个抽象类(如提供的图像所示),然后为从该类继承的每种货币创建一个类。这将是非常乏味的,但至少它可能会起作用。但是,似乎我不仅要为每个货币模型创建一个类,还要为每个存储库创建一个类。


除非有办法在不做所有这些事情的情况下为这个结构服务?有什么想法吗?目前正在查看文档并搜索具有此问题的其他文档,但这种结构似乎并不常见。


编辑:


我想添加控制器,因为我相信它是区分抽象类的子类的逻辑问题发生的位置。


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;


@RestController

@RequestMapping("/rates")

class CurrencyRateController {


    @Autowired

    private CurrencyRateService service;


    @GetMapping("/allEUR")

    public List<CurrencyRate> getAllEUR() {

        return service.findAll();

    }


    @GetMapping("/allCAD")

    public List<CurrencyRate> getAllCAD() {

        return service.findAll();

    }

}


上面的控制器返回 /allCAD 和 /allEUR 的 CAD 值,我假设这是因为 CurrencyRateService 使用 CurrencyRateRepository。在某个地方,Hibernate试图区分EUR子类和CAD子类,但它们是相同的。因此,为所有请求提供 CAD 值。


慕村225694
浏览 204回答 2
2回答

慕标5832272

在您的场景中,我认为应该使用单表策略,因为它将为每个类层次结构创建一个表。这也是 JPA 选择的默认策略,如果我们没有显式指定一个。您可以通过将@Inheritance注释添加到超类来定义要使用的策略:-@Entity@Inheritance(strategy = InheritanceType.SINGLE_TABLE)public class CurrencyRate&nbsp; {&nbsp; &nbsp; @Id&nbsp; &nbsp; @JsonSerialize(using=DateSerializer.class)&nbsp; &nbsp; private Date datetime;&nbsp; &nbsp; @NotBlank&nbsp; &nbsp; @JsonSerialize(using=FloatSerializer.class)&nbsp; &nbsp; private float rate;&nbsp; &nbsp; // constructor, getters, setters}没有字段的子类:-@Entitypublic class Rupee extends CurrencyRate {}@Entitypublic class Dollar extends CurrencyRate {}由于所有实体的记录将位于同一个表中,因此 Hibernate 提供了一种区分这些表行的方法。在这种情况下,将添加一个名为 DTYPE 的鉴别器列,该列将实体的名称作为值。您可以为 CurrencyRate 创建存储库,然后基于 DType 列查询 CurrencyRate 表。

四季花海

可能您必须使用正确的继承策略。喜欢这个:@Entity@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)public abstract class CurrencyRate {&nbsp; (... omitted for brevity ...)}子:@Entity@Table(name="USDOLLAR")public class UnitedStatesDollar extends CurrencyRate {}@Entity@Table(name="EURO")public class Euro extends CurrencyRate {}其他货币也是如此。您应该能够针对类进行查询,从所有表中检索所有结果,并且必须使用来区分货币。此外,您还可以针对单一货币执行查询。CurrencyRateinstanceof如果您想了解更多关于继承的信息,请查看此处。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java