我正在使用带有 hibernate 的 spring jpa 存储库将实体保存到我的 oracle 数据库中。如何使用 Spring-Hibernate 获取 oracle 数据库序列的下一个值?
这是我的事件类:
@Entity
public class Event {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private Long seriesId;
private String description;
public Event() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getSeriesId() {
return seriesId;
}
public void setSeriesId(Long seriesId) {
this.seriesId = seriesId;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
我需要为事件解析器中的所有事件系列获取一次序列的下一个值。
public class EventResolver {
@Autowired
private EventRepository eventRepository;
public void createSeriesOfEvents(List<EventAPI> eventsToCreate){
Long seriesId = null; // TODO: Get the series id from database sequence
for (EventAPI currEvent : eventsToCreate){
Event newEvent = new Event();
newEvent.setDescription(currEvent.description);
newEvent.setSeriesId(seriesId);
eventRepository.save(newEvent);
}
}
}
感谢您提供任何帮助..
原文由 Ron Badur 发布,翻译遵循 CC BY-SA 4.0 许可协议
最后我以 Spring 的方式解决了我的问题,你所需要的只是在 JpaRepository 中添加一个本地查询,如下所示: