我正在使用Spring JPA Repository和Hibernate将实体保存到我的Oracle数据库中。如何使用Spring-Hibernate获取我Oracle数据库序列的下一个值?
这是我的Event类:
@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);
}
}
}
感谢任何形式的帮助。
@ID
字段,Hibernate就会为您完成所有这些操作。例如,请参见:https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Sequence_objects http://tech.zooplus.com/sequences-in-hibernate/ - Alan Hay