我正在使用spring jpa存储库和hibernate一起将实体保存到我的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);
}
}
}
感谢任何形式的帮助..
为什么?将正确的注释应用到'@ ID'字段,Hibernate将为你做这一切。例如,请参阅:https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Sequence_objects http://tech.zooplus.com/sequences-in-hibernate/ –
我希望整个系列(系列)具有相同的值可以是4-5个事件),所以当我创建'Event'实体时不能生成它。我必须在'createSeriesOfEvents'方法 –