4

I am new to hibernate and I want to insert primary number in my table for unique identification. I am using Oracle as my database so do I need to create sequence in oracle to get auto increment generation number ?

I am using below code but it is not working. I have not created any sequence yet.

 @Id
 @Column(name = "id" )
 @GeneratedValue ( strategy = GenerationType.TABLE)

I have used AUTO, SEQUENCE and IDENTITY but nothing works for me.

6
  • kindly recheck GenerationType.AUTO Commented Mar 10, 2014 at 13:09
  • You should really only need to have @Id and @GeneratedValue, as the default for generated is auto increment. Commented Mar 10, 2014 at 13:10
  • What does it mean "nothing works"...AUTO will do it for you. Commented Mar 10, 2014 at 13:10
  • docs.jboss.org/hibernate/orm/4.1/manual/en-US/… Commented Mar 10, 2014 at 13:11
  • @KishanSarsechaGajjar brother i have already tried with it and it gives me this error."Hibernate: select hibernate_sequence.nextval from dual org.hibernate.exception.SQLGrammarException: could not get next sequence value" Commented Mar 10, 2014 at 13:21

3 Answers 3

8

this is one way of using Oracle sequence in a JPA mapped entity:

@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQUENCE_NAME")
@SequenceGenerator(name = "SEQUENCE_NAME", sequenceName = "SEQUENCE_NAME", allocationSize = 1, initialValue = 1)

In this way your persist() method will ask for the next value of the sequence in order to use it as ID for your entry.

Sign up to request clarification or add additional context in comments.

1 Comment

This causes the following error "sometimes" > error performing isolated work; SQL [n/a]; constraint [SYSTEM.SYS_C0010941]; nested exception is org.hibernate.exception.ConstraintViolationException: error performing isolated work
1

You can ue this @GeneratedValue(strategy=GenerationType.AUTO)

@Id
@Column(name = "id" )
@GeneratedValue(strategy=GenerationType.AUTO)

Comments

0

In GenerationType.TABLE option, ID value will be filled with the column of other table.

If you are using strategy=GenerationType.TABLE you will require to mention the Table from where your ID will be filled.

For example,

@GeneratedValue(strategy=GenerationType.TABLE, generator="course")
@TableGenerator(
    name="course",
    table="GENERATOR_TABLE",
    pkColumnName = "key",
    valueColumnName = "next",
    pkColumnValue="course",
    allocationSize=30
)

And for other option, you can use GenerationType.AUTO option, and let hibernate decide which option to choose according to databse.

 @Id
 @Column(name = "id" )
 @GeneratedValue (strategy = GenerationType.AUTO)

And make sure that you properly configured hibernate.cfg.xml file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.