[英]org.hibernate.AssertionFailure Issue when getting sequence value
I am trying to insert multiple records in a table using loop and getting sequence number for that using below method.我正在尝试使用循环在表中插入多条记录,并使用以下方法获取序列号。 It is getting sequence number for very first time alone and during next iteration below exception is coming.Please help in resolving this
它是第一次单独获取序列号,并且在下一次迭代期间出现以下异常。请帮助解决这个问题
14:03:51.928 [http-nio-8080-exec-5] ERROR org.hibernate.AssertionFailure - HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: possible non-threadsafe access to session
14:03:51.938 [http-nio-8080-exec-5] ERROR u.s.m.e.p.o.b.c.ORBudgetController - 2020/08/26_14:03:51.938|1|pa23690|bearer 6d7417d8-6835-485e-956d-c362cb7bce2b|createRecord|possible non-threadsafe access to session
@Override
public int getNextSequenceNumber(String seqName) {
int nextValue = 0;
String strQuery = "SELECT " + seqName + ".NEXTVAL FROM DUAL";
Query q = entityManager.createNativeQuery(strQuery);
BigDecimal bd = (BigDecimal) q.getSingleResult();
nextValue = bd.intValue();
return nextValue;
}
You need to generate the sequence automatically, do it manually is a bad practice and can bring you problems in the future.您需要自动生成序列,手动生成是一种不好的做法,并且将来会给您带来问题。 There are several JPA strategies to automatically generate the sequence, this, for example, is The Sequence Strategy
有几种 JPA 策略可以自动生成序列,例如, The Sequence Strategy
@Entity
// Define a sequence - might also be in another class:
@SequenceGenerator(name="seq", initialValue=1, allocationSize=100)
public class EntityWithSequenceId {
// Use the sequence that is defined above:
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
@Id long id;
}
You can also use The Auto Strategy您还可以使用自动策略
@Entity
public class EntityWithAutoId1 {
@Id @GeneratedValue(strategy=GenerationType.AUTO) long id;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.