根据@vlad Mihalcea提供的示例,我已经用嵌入式类实现了@manytomany映射。它有效,但是我需要为此可连接实体创建和使用春季的REST存储库。
通常,我会做这样的事情来启用存储库:
@CrossOrigin
@RepositoryRestResource(collectionResourceRel = "companyproducts", path = "companyproducts")
public interface CompanyProductRepository
extends PagingAndSortingRepository<CompanyProduct, CompanyProduct.CompanyProductId> {
}
,但是,由于在这个实体(共同)中,我没有经典的id
,因此:
public class FooBar {
....
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
}
但是我有:
public class FooBar {
....
@EmbeddedId
private FooBarId id;
...
}
我不能使用Long
或说int
,因为标识符不在这些类型中,它基本上是FooBarId
类型。
顺便说一句,我还将提供大部分类别:
@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;
}
...
}
当然,我确实尝试过将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是可嵌入的类,因此请使用它代替经典原始类。
响应中的问题可能与您的保存/检索逻辑有关
edit :基于响应是类,而不是数字,我希望您从存储库中检索ID,但是显然您应该从ID中获得数值,因为现在ID是ID嵌入式类