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

huangapple go评论82阅读模式
英文:

org.hibernate.AssertionFailure Issue when getting sequence value

问题

我正在尝试使用循环向表中插入多条记录,并使用以下方法获取其序列号。仅在第一次时获取序列号,下一次迭代期间出现以下异常。请帮忙解决。

14:03:51.928 [http-nio-8080-exec-5] ERROR org.hibernate.AssertionFailure - HHH000099: 出现断言失败这可能是 Hibernate 的错误但更可能是由于会话的不安全使用而引起的):org.hibernate.AssertionFailure可能存在非线程安全的会话访问
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|可能存在非线程安全的会话访问
@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;
}
英文:

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;
}

答案1

得分: 0

你需要自动生成序列,手动操作是不良实践,可能会在将来带来问题。有几种 JPA(Java Persistence API)策略可以自动生成序列,例如,这是序列策略

@Entity
// 定义一个序列 - 也可以在另一个类中定义:
@SequenceGenerator(name="seq", initialValue=1, allocationSize=100)
public class EntityWithSequenceId {
    // 使用上面定义的序列:
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
    @Id long id;
}

你还可以使用自动策略:

@Entity
public class EntityWithAutoId1 {
    @Id @GeneratedValue(strategy=GenerationType.AUTO) long id;
}
英文:

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

@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;
     
}

huangapple
  • 本文由 发表于 2020年8月26日 16:36:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63593750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定