使用Spring Hibernate从序列中获取Next值 [英] Getting Next value from sequence with spring hibernate
本文介绍了使用Spring Hibernate从序列中获取Next值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将Spring jpa存储库与hibernate一起使用,以将实体保存到我的oracle数据库中.如何使用Spring-Hibernate获取oracle数据库序列的下一个值?
I am using spring jpa repository with hibernate to save entites to my oracle database. How I can get the next value of my oracle database sequence using Spring-Hibernate?
这是我的Event课:
This is my Event class :
@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;
}
}
我需要为事件解析器中的所有事件系列一次获取序列的下一个值.
I need to get the next value of the sequence once for the all event series in the event resolver.
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);
}
}
}
感谢任何帮助.
推荐答案
最后,我以Spring的方式解决了我的问题,所需要做的就是像这样在JpaRepository中添加本机查询:
Finally I Solved my problem in the Spring way, All you need is to add a native query in the JpaRepository like this:
public interface EventRepository extends JpaRepository<Event, Long> {
@Query(value = "SELECT seq_name.nextval FROM dual", nativeQuery =
true)
Long getNextSeriesId();
这篇关于使用Spring Hibernate从序列中获取Next值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文