gpt4 book ai didi

java - Spring MVC CrudRepository findByIn

转载 作者:行者123 更新时间:2023-11-29 08:49:23 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com