JoinTable没有一个常规的L​​ong id,而是EmbeddedId,我该如何创建一个Spring存储库? [英] JoinTable doesn't have a regular Long id, but rather EmbeddedId instead, how do I create a Spring repository with that?

查看:116
本文介绍了JoinTable没有一个常规的L​​ong id,而是EmbeddedId,我该如何创建一个Spring存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据@Vlad Mihalcea提供的示例,我已经实现了@ManyToMany与EmbeddedId类的映射。它可以工作,但是我需要为这个JoinTable实体创建和使用Spring的REST存储库。



通常,我会这样做来启用存储库:

  @CrossOrigin 
@RepositoryRestResource(collectionResourceRel =companyproducts,path =companyproducts)
public interface CompanyProductRepository
扩展了PagingAndSortingRepository< CompanyProduct,CompanyProduct.CompanyProductId> {

}

但是,由于在此实体(JoinTable)没有经典的 id ,就像这样:

  public class FooBar {
....

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private长ID;

...
}

但是我有:

  public class FooBar {
....

@EmbeddedId
私人FooBarId ID;

...
}

我无法使用 Long 或者让我们说 int ,因为标识符不是这些类型中的任何一个,它基本上是类型 FooBarId



顺便说一下,我还会提供大部分的类:



< pre $ @Embeddable
public class FooBarId implements Serializable {

@Column(name =foo_id)
private Long fooId;

@Column(name =bar_id)
private长barId;

private FooBarId(){}

public FooBarId(Long fooId,Long barId){
this.fooId = fooId;
this.barId = barId;
}
...
}

当然,在存储库声明中放置 FooBarId 而不是 Long ,但是在我的REST json响应中,这:

  http:// localhost:8080/api/foobar/com.example.springrest.entities.foobarid@g43 

(注意类本身的路​​径,而不是数字参数,例如5)。



我的问题是:是否可以使用 EmbeddedId 而不是 Long int 等等。

解决方案

当然

  public interface FooBarRepository extends PagingAndSortingRepository< FooBar,FooBarId> {} 

id是可嵌入类,因此可以使用它来代替经典基元。



响应中的问题可能与您的保存/检索逻辑有关

编辑:基于响应是类,而不是数字我期望你从存储库中检索到的id,但你显然应该从id获得数值,因为现在这个id是可嵌入类


I've implemented @ManyToMany mapping with an EmbeddedId class, as per the example provided by @Vlad Mihalcea. It works, however I need to create and use Spring's REST repository for this JoinTable Entity.

Usually, I would do something like this to enable the repository:

@CrossOrigin
@RepositoryRestResource(collectionResourceRel = "companyproducts", path = "companyproducts")
public interface CompanyProductRepository
    extends PagingAndSortingRepository<CompanyProduct, CompanyProduct.CompanyProductId> {

}

But, since in this Entity (JoinTable) I don't have a classic id, like this:

public class FooBar {
    ....

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;    

    ...
}

but instead I have:

public class FooBar {
    ....

    @EmbeddedId
    private FooBarId id;    

    ...
}

I cannot use Long or let's say int because the identifier is not of any of those types, it's basically of type FooBarId.

By the way, I will also provide most of that class:

@Embeddable
public class FooBarId implements Serializable {

    @Column(name = "foo_id")
    private Long fooId;

    @Column(name = "bar_id")
    private Long barId;

    private FooBarId() {}

    public FooBarId(Long fooId, Long barId) {
        this.fooId= fooId;
        this.barId= barId;
    }
...
}

I did try, of course, putting FooBarId instead of Long in the repository declaration, but then in my REST json response I get something like this:

http://localhost:8080/api/foobar/com.example.springrest.entities.foobarid@g43

(note the path to the class itself, not a numeral parameter, eg. 5).

My question is: Is it possible to use repositories with an EmbeddedId instead of Long, int, etc.?

解决方案

Yes of course

public interface FooBarRepository extends PagingAndSortingRepository<FooBar, FooBarId > { }

the id is the embeddable class so use that instead of the classic primitive.

the problem in the response is probably something related to your save/retrieve logic

Edit: based on the response being the class instead of the numeral i expect you retrieved the id from the repository, but you should obviously get the numerical value from the id since now that id is the embeddable class

这篇关于JoinTable没有一个常规的L​​ong id,而是EmbeddedId,我该如何创建一个Spring存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆