[英]org.hibernate.AssertionFailure: possible non-threadsafe access to the session
[英]org.hibernate.AssertionFailure Issue when getting sequence value
我正在尝试使用循环在表中插入多条记录,并使用以下方法获取序列号。 它是第一次单独获取序列号,并且在下一次迭代期间出现以下异常。请帮助解决这个问题
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;
}
您需要自动生成序列,手动生成是一种不好的做法,并且将来会给您带来问题。 有几种 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;
}
您还可以使用自动策略
@Entity
public class EntityWithAutoId1 {
@Id @GeneratedValue(strategy=GenerationType.AUTO) long id;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.