简体   繁体   English

org.hibernate.AssertionFailure 获取序列值时的问题

[英]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.

相关问题 org.hibernate.AssertionFailure:可能对会话进行非线程安全的访问 - org.hibernate.AssertionFailure: possible non-threadsafe access to the session org.hibernate.AssertionFailure: null 标识符@OneToOne 关系 - org.hibernate.AssertionFailure: null identifier @OneToOne relationship org.hibernate.AssertionFailure:集合被flush()处理了两次 - org.hibernate.AssertionFailure: collection was processed twice by flush() org.hibernate.criteria.uniqueResult期间的AssertionFailure - org.hibernate.AssertionFailure during criteria.uniqueResult Hibernate:org.hibernate.AssertionFailure:com.xxx.Bean条目中的null id - Hibernate: org.hibernate.AssertionFailure: null id in com.xxx.Bean entry Spring JPA 共享主键导致 org.hibernate.AssertionFailure: null identifier - Spring JPA Shared primary key causes org.hibernate.AssertionFailure: null identifier Spring JPA/Hibernate org.hibernate.AssertionFailure: null id in Entity(异常发生后不刷新Session) - Spring JPA/Hibernate org.hibernate.AssertionFailure: null id in Entity (don't flush the Session after an exception occurs) Hibernate 断言失败 - Hibernate AssertionFailure AssertionFailure:当Hibernate查询对象时,有时会抛出null id - AssertionFailure: null id sometimes thrown when Hibernate query objects Hibernate AssertionFailure在不同的线程中 - Hibernate AssertionFailure in different Threads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM