动态选择 Spring Data JPA 存储库

发布于 2025-01-11 23:09:56 字数 1369 浏览 0 评论 0原文

我对 Java 中的类型转换和 Spring 概念很陌生。

我有以下实体:

@MappedSuperclass
public class Fish {}

@Entity
public class Whale extends Fish{
  // There is no other property here..
}

@Entity
public class Shark extends Fish{
  // There is no other property here..
}

我已经为这些实体创建了相应的存储库:

public interface WhaleRepository extends CrudRepository<Whale, String> {}

public interface SharkRepository extends CrudRepository<Whale, String> {}

我有一个控制器,根据端点我想保存数据..

@RestController
public class FishController {

    @ResponseBody
    @RequestMapping(value = "{fish-type}")
    public ResponseEntity<Long> create(@PathVariable("fish-type") String fishType, @RequestBody Fish fish){
        if(fishType.equals("whale"}{
           // Error: The method save(S) in the type CrudRepository<Whale,Long> is not applicable for the arguments (Fish)
           new WhaleRepository().save(fish); 
        }
        else if(fishType.equals("shark"}{
           // Error: The method save(S) in the type CrudRepository<Shark,Long> is not applicable for the arguments (Fish)
           new SharkRepository().save(fish);
        }
    }
}

有没有一种方法可以动态选择存储库并保留数据。

I am new to the concept of type casting and Spring in Java.

I have below entities:

@MappedSuperclass
public class Fish {}

@Entity
public class Whale extends Fish{
  // There is no other property here..
}

@Entity
public class Shark extends Fish{
  // There is no other property here..
}

I have created corresponding repositories for these entity:

public interface WhaleRepository extends CrudRepository<Whale, String> {}

public interface SharkRepository extends CrudRepository<Whale, String> {}

I have a single controller where depending on the endpoint I want to save the data ..

@RestController
public class FishController {

    @ResponseBody
    @RequestMapping(value = "{fish-type}")
    public ResponseEntity<Long> create(@PathVariable("fish-type") String fishType, @RequestBody Fish fish){
        if(fishType.equals("whale"}{
           // Error: The method save(S) in the type CrudRepository<Whale,Long> is not applicable for the arguments (Fish)
           new WhaleRepository().save(fish); 
        }
        else if(fishType.equals("shark"}{
           // Error: The method save(S) in the type CrudRepository<Shark,Long> is not applicable for the arguments (Fish)
           new SharkRepository().save(fish);
        }
    }
}

Is there a way by which I can dynamically pick the repository and persist the data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晰传感 2025-01-18 23:09:56

是的,你可以。

首先,您必须创建一个抽象实体和存储库。

其次,您需要在 Jackson 中使用继承来在请求中发送通用实体。

例如,类似这样的内容:

FishRepository 类:

public interface FishRepository extends JpaRepository<Fish, Long> {}

Fish 类:

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY,
        property = "fishType")
@JsonSubTypes({
        @JsonSubTypes.Type(value = Whale.class, name = "whale"),
        @JsonSubTypes.Type(value = Shark.class, name = "shark")
})
@MappedSuperclass
public abstract class Fish {

    @Id
    private Long id;

    public Fish() {
    }

    // getter/setter ..
}

Controller 类:

@PostMapping(value = "/fish")
public String create(@RequestBody Fish fish) {
    ...
    fishRepository.save(fish);
    ...
}

现在您需要在请求中发送fishType。例如:

{
    ...,
    "fishType": "shark"
}

参考文献:

Spring @RequestBody 继承

具有抽象类/继承的 Spring Data Rest Repository

Yes you can.

Firstly, you have to create a abstract entity and repository.

Secondly, you need to use inheritance in Jackson to send generic entity in request.

For example, something like this:

FishRepository class:

public interface FishRepository extends JpaRepository<Fish, Long> {}

Fish class:

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY,
        property = "fishType")
@JsonSubTypes({
        @JsonSubTypes.Type(value = Whale.class, name = "whale"),
        @JsonSubTypes.Type(value = Shark.class, name = "shark")
})
@MappedSuperclass
public abstract class Fish {

    @Id
    private Long id;

    public Fish() {
    }

    // getter/setter ..
}

Controller class:

@PostMapping(value = "/fish")
public String create(@RequestBody Fish fish) {
    ...
    fishRepository.save(fish);
    ...
}

Now you need to send the fishType inside your request. E.g:

{
    ...,
    "fishType": "shark"
}

References:

Spring @RequestBody inheritance

Spring Data Rest Repository with abstract class / inheritance

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文