0

I want to see the sequence number when I save an object into db using jpa.

Code:

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ")
@SequenceGenerator(name = "SEQ", sequenceName = "Temp_SEQ")

Property:

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE

I see the output as below,

insert into employee (emp_number, acquire, reason) values (null, ?, ?)
2017-12-12 14:36:20.634 TRACE 21048 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [YES]
2017-12-12 14:36:20.635 TRACE 21048 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - [asdf]

but I am expecting the sequence number too as,

insert into case_selection (case_number, acquire, reason) values (null, ?, ?)
binding parameter [1] as [Long] - [1]
binding parameter [1] as [VARCHAR] - [YES]
binding parameter [1] as [VARCHAR] - [asdf]

Is there anyway I can get sequence number printed in case of hibernate or JPA?

Jens Schauder
  • 65,795
  • 24
  • 148
  • 294
Yaswanth
  • 305
  • 1
  • 7
  • 18
  • may be, if you see my question - I have used the properties but still there is no solution. – Yaswanth Dec 12 '17 at 10:11
  • Yes my bad. So you still get `values (null, ?, ?)`? Guess the id is then not showing in bindings since it is null in that phase and populated later. – pirho Dec 12 '17 at 10:13
  • Yes. I think there is a way where we can see that value too. – Yaswanth Dec 12 '17 at 10:15
  • Still you might want to read comments in [accepted answer](https://stackoverflow.com/a/1713464/6413377) if not already. – pirho Dec 12 '17 at 10:18

2 Answers2

0

I actually found the solution in different way.

This is from the database.

SELECT seqname.CURRVAL FROM dual

This query gives us what is the current value.

@Priho: thanks for your help.

Yaswanth
  • 305
  • 1
  • 7
  • 18
0

You did not show code.

(First an aside: you seem to be doing INSERT INTO T(pk, ...) VALUES(NULL, ...); I have never done that, listing the generated primary key.)

I you are using a Repository for saving a new entity (INSERT), then the updated entity is returned. Something in the line of:

assert entity.id = null;
entity = repository.save(entity);
assert entity.id != null;
Joop Eggen
  • 96,344
  • 7
  • 73
  • 121