Java 弹簧靴。如何将 Optional<> 传递给实体类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50185164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot. how to Pass Optional<> to an Entity Class
提问by kurt estacion
i am currently making a website using spring and i stumble upon this basic scenario that i don't have any idea on how to solve this specific code: Entity = Optional;
我目前正在使用 spring 制作一个网站,我偶然发现了这个基本场景,我对如何解决这个特定代码没有任何想法:Entity = Optional;
RoomEntity roomEntity = roomRepository.findById(roomId);
ReservationResource(API request Class):
ReservationResource(API请求类):
public class ReservationResource {
@Autowired
RoomRepository roomRepository;
@RequestMapping(path = "/{roomId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<RoomEntity> getRoomById(
@PathVariable
Long roomId){
RoomEntity roomEntity = roomRepository.findById(roomId);
return new ResponseEntity<>(roomEntity, HttpStatus.OK);}
}}
RoomRepository Class:
RoomRepository 类:
public interface RoomRepository extends CrudRepository<RoomEntity, Long> {
List<RoomEntity> findAllById(Long id);
}
RoomEntity
房间实体
@Entity
@Table(name = "Room")
public class RoomEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
private Integer roomNumber;
@NotNull
private String price;
public RoomEntity() {
super();
}
}
采纳答案by Hiren
According to your error you are getting Optional<RoomEntity>
from repository's findAll method and you are casting it to RoomEntity
.
根据您的错误,您是Optional<RoomEntity>
从存储库的 findAll 方法中获取的,并将其转换为RoomEntity
.
Instead of RoomEntity roomEntity = roomRepository.findById(roomId);
do this
而不是RoomEntity roomEntity = roomRepository.findById(roomId);
这样做
Optional<RoomEntity> optinalEntity = roomRepository.findById(roomId);
RoomEntity roomEntity = optionalEntity.get();
Optional<RoomEntity> optinalEntity = roomRepository.findById(roomId);
RoomEntity roomEntity = optionalEntity.get();
回答by Sébastien Helbert
First solution
第一个解决方案
You can implement JpaRepository
instead of CrudRepository
which provide a getOne method that returns an RoomEntity as you expect. (JpaRepository
for JPA or MongoRepository
for MongoDB) :
您可以实现JpaRepository
而不是CrudRepository
提供一个 getOne 方法,该方法按您的预期返回 RoomEntity。(JpaRepository
对于 JPA 或MongoRepository
对于 MongoDB):
public interface RoomRepository extends JpaRepository<RoomEntity, Long> {
List<RoomEntity> findAllById(Long id);
}
and
和
RoomEntity roomEntity = roomRepository.getOne(roomId);
Note that an EntityNotFoundException
will be thrown if there's no RoomEntity for this roomId.
请注意,EntityNotFoundException
如果此 roomId 没有 RoomEntity,则会抛出 。
Second solution
第二种解决方案
The findById method of CrudRepository returns an optional so you must deal with it properly to get an RoomEntity if there is one. For example :
CrudRepository 的 findById 方法返回一个可选项,因此您必须正确处理它才能获得 RoomEntity(如果有)。例如 :
RoomEntity roomEntity = optionalEntity.roomRepository.findById(roomId).get();
In this case .get()
will throw a NoSuchElementException
if there's no RoomEntity for this roomId.
在这种情况下,如果此 roomId 没有 RoomEntity,.get()
则会抛出一个NoSuchElementException
。
This article may help to understand optionals : http://www.baeldung.com/java-optional
这篇文章可能有助于理解可选项:http: //www.baeldung.com/java-optional
回答by fahed ghodhbane
Try this, it works for me
试试这个,它对我有用
RoomEntity roomEntity = roomRepository.findById(roomId).orElse(null);
RoomEntity roomEntity = roomRepository.findById(roomId).orElse(null);
回答by Jan Bodnar
The answers lack some job to do. Before you call get()
, you should do some checking with isPresent()
. Like so:
答案缺乏一些工作要做。在你打电话之前get()
,你应该做一些检查isPresent()
。像这样:
Optional<RoomEntity> optionalEntity = roomRepository.findById(roomId);
if (optionalEntity.isPresent()) {
RoomEntity roomEntity = optionalEntity.get();
...
}
Read this great article about optionals: https://dzone.com/articles/using-optional-correctly-is-not-optional
阅读这篇关于可选项的精彩文章:https: //dzone.com/articles/using-optional-correctly-is-not-optional