- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 CrudRepository,它应该使用数组 (findByIn
) 进行查询。在我的存储库测试中它有效,但是当我尝试在我的服务中使用查询时,它不起作用。有人可以解释为什么它不起作用吗?这是我的设置(不包括一些与问题无关的代码)
数据库模型:
@Entity
@Table(name="Place")
public class Place implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "placeId", nullable = false)
private Long placeId;
@Column(name = "owner", nullable = false)
private String owner;
public Long getPlaceId() {
return placeId;
}
public void setPlaceId(Long placeId) {
this.placeId = placeId;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
}
存储库:
@Repository
public interface PlaceRepository extends CrudRepository<Place, Long> {
List<Place> findByPlaceIdIn(Long[] placeId);
}
服务(这是不工作的部分):
@Service
public class PlaceService {
@Autowired
private PlaceRepository placeRepository;
public List<Place> getPlaces(Long[] placeIds) {
return placeRepository.findByPlaceIdIn(placeIds);
}
}
问题是在我的服务 placeRepository.findByPlaceIdIn(placeIds)
中,如果 placeIds
包含多个项目,则返回 0 个对象。如果 placeIds
仅包含一项,则查询工作正常。我尝试用这段代码替换 return placeRepository.findByPlaceIdIn(placeIds)
逐一查询每个数组项(这确实有效,但我想让查询正常工作应该):
ArrayList<Place> places = new ArrayList<Place>();
for (Long placeId : placeIds) {
Long[] id = {placeId};
places.addAll(placeRepository.findByPlaceIdIn(id));
}
return places;
我知道存储库应该可以工作,因为我有一个工作测试:
public class PlaceRepositoryTest {
@Autowired
private PlaceRepository repository;
private static Place place;
private static Place place2;
private static Place otherUsersPlace;
@Test
public void testPlacesfindByPlaceIdIn() {
place = new Place();
place.setOwner(USER_ID);
place2 = new Place();
place2.setOwner(USER_ID);
place = repository.save(place);
place2 = repository.save(place2);
Long[] ids = {place.getPlaceId(), place2.getPlaceId()};
assertEquals(repository.findByPlaceIdIn(ids).size(), 2);
}
}
我还有其他模型的另一个存储库,它也使用 findByIn
并且工作正常。我看不到存储库之间的任何相关差异。我认为它可能会提供更多详细信息来显示工作存储库,因此我将其包含在下面:
数据库模型:
@Entity
@Table(name="LocalDatabaseRow")
@JsonIgnoreProperties(ignoreUnknown=false)
public class LocalDatabaseRow implements Serializable {
public LocalDatabaseRow() {}
public LocalDatabaseRow(RowType rowType) {
this.rowType = rowType;
}
public enum RowType {
TYPE1,
TYPE2
};
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
@JsonProperty("id")
private Long id;
@JsonProperty("rowType")
@Column(name = "rowType")
private RowType rowType;
public Long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public RowType getRowType() {
return rowType;
}
public void setRowType(RowType rowType) {
this.rowType = rowType;
}
}
存储库:
@Repository
public interface LocalDatabaseRowRepository extends CrudRepository<LocalDatabaseRow, Long> {
List<LocalDatabaseRow> findByRowTypeAndUserIdIn(RowType type, String[] userId);
}
最佳答案
尝试使用列表代替:
findByPlaceIdIn(列表 placeIdList);
关于java - Spring MVC CrudRepository findByIn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23514544/
我正在阅读 JPA docs在 Spring ,我正在尝试重组我的代码。 我现在所拥有的: BrewerRepository @Repository public class BrewerReposi
我正在使用 Maven 开发一个 Spring Boot 项目(由 Spring Initializr 生成)。我想创建一个 CrudRepository,但我收到错误“CrudRepository
是否可以为 CrudRepository 添加默认排序方法?喜欢: interface PersonRepository extends CrudRepository { @SortDefaul
我创建了一个像这样的计划类 计划: @Entity(name = "Plan") @Data @NoArgsConstructor @AllArgsConstructor @EqualsAndHash
我有以下实体 请求对象 AdditionalReqObj。 两个实体均与 REQ_ID 链接表中的列(reqId 字段)。我正在使用Spring data CrudRepository interfa
我有两个实体客户和订单: @Entity public class Customer { @Id @GeneratedValue(strategy=GenerationType.IDENTIT
我的行为很奇怪。我创建了扩展 CrudRepository 的存储库。除保存方法外,所有默认方法都可以正常工作。它不会将新实体保存到数据库。我需要使用我提供的 id 保存新实体。如果我在数据库中提供现
我的域名和日期字段已更新,我想搜索 @Column(name = "updated") Date updated; 我有一个代表一天的 Java Date 对象,它由我的端点的 Controller
我有表格绑定(bind) @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "boundId", unique =
我使用 spring boot 1.2.5.RELEASE。我定义了一个扩展 CrudRepository 的接口(interface) public interface SampleEntitySe
我正在阅读有关 Crudrepository 的信息,它是针对特定类型的存储库进行通用 CRUD 操作的接口(interface)。 但我们可以创建自定义界面并扩展 CrudRepository。 我
我正在使用 Spring 并在其中 spring-data-jpa:1.7.0.RELEASE 和 hibernate-jpa-2.1-api:1.0.0.Final。我的数据库是MySQL。在集成测
我想显示一张人员表。用户应该能够发送查询并按大多数可选的属性进行过滤。 问题:对于每个要过滤的属性,我必须在 spring-data-jpa 中使用 `CrudRepository 引入一个额外的方法
我是 Spring 的新手。我的 GCGood 类使用 CrudRepository 保存到 MySQL-DB。而且效果很好。 现在我尝试编写 JUnit 测试。当然,我不希望任何测试数据出现在我的数
请原谅我犯的任何错误,因为这是我在这里的第一个问题。 我有一个包含两个表的数据库,其中一个表名为:PERSON 具有以下实体: @Entity class Person { @Id p
我有一个基本的 SpringBoot 应用程序。使用 Spring Initializer、JPA、嵌入式 Tomcat、Thymeleaf 模板引擎,并打包为可执行 JAR 文件。我创建了这个 Re
我想在我的 neo4j 数据库中存储一些数据。为此,我使用 spring-data-neo4j。 我的代码如下: for (int i = 0; i risk = new HashSet()
我使用 Angular、SpringBoot 和 MySQL 数据库构建了一个应用程序。它使用 CrudRepository 但我不明白它(一切正常)。 Controller /存储库如何知道从哪个表
我正在尝试获取对我的存储库接口(interface) (UserRepository) 的引用,该接口(interface)按顺序在我的自定义实现 (UserRepositoryExtensionIm
我有两个具有@ManyToOne 关系的实体类,如下所示。 @Entity public class Student { @Id private Integer studentId; @C
我是一名优秀的程序员,十分优秀!