Java 使用 spring 休眠从序列中获取下一个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46240529/
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
Getting Next value from sequence with spring hibernate
提问by Ron Badur
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?
我正在使用带有 hibernate 的 spring jpa 存储库将实体保存到我的 oracle 数据库中。如何使用 Spring-Hibernate 获取 Oracle 数据库序列的下一个值?
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);
}
}
}
Thanks for any kind of help..
感谢您的任何帮助..
采纳答案by Ron Badur
Finally I Solved my problem in the Spring way, All you need is to add a native query in the JpaRepository like this:
最后我以 Spring 的方式解决了我的问题,您只需要在 JpaRepository 中添加一个本机查询,如下所示:
public interface EventRepository extends JpaRepository<Event, Long> {
@Query(value = "SELECT seq_name.nextval FROM dual", nativeQuery =
true)
Long getNextSeriesId();
回答by Piotr R
You can use this approach in JPA:
您可以在 JPA 中使用这种方法:
Query q = em.createNativeQuery("select seq_name.nextval from dual");
return (Long)q.getSingleResult();
回答by slambeth
Annotate your id property like so:
像这样注释你的 id 属性:
@Id
@GeneratedValue(generator = "idSequence")
@SequenceGenerator(schema = "MYORASCHEMA", name = "idSequence", sequenceName = "MY_ORACLE_SEQ_NAME", allocationSize = 1)
@Column(name="ID")
private Long id;
回答by Gustavo Passini
With Spring 5, you can use one of their built-in classes for this task like OracleSequenceMaxValueIncrementer
使用 Spring 5,您可以使用他们的内置类之一来执行此任务,例如OracleSequenceMaxValueIncrementer
See all the available options in this package: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/support/incrementer/package-summary.html
查看此包中的所有可用选项:https: //docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/support/incrementer/package-summary.html