1

I am using hibernate annotations. I want to make a column as autoincremented. I have created a sequence in database(oracle) and mapped that sequence in java POJO class. Do I need to create trigger for that sequence too? I want to know how we can make a column autoincremented while using hibernate anotation? What changes i have to do in java and as well as database side? Please help me in this. Following is the part of code where I have mapped the sequence.

public class SimRuns implements Serializable {

    private static final long serialVersionUID = 8698324570356602407L;

    @Id @Column(name = "RUN_ID")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_run_id")
    @SequenceGenerator(name="seq_run_id", sequenceName="seq_run_id")
    private Long runId;
}
1
  • 1
    Do you have a trigger/sequence created on the database side? Commented May 6, 2013 at 6:47

4 Answers 4

1

This works for me:

@Id
@GeneratedValue(generator = "nosicSeq")
@SequenceGenerator(name = "nosicSeq", sequenceName = "NOSIC_SEQ", allocationSize = 1)
@Column(name = "SID")
private BigDecimal sid;

no triggers in DB needed, just sequence.

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

Comments

0

Try removing the generator and setting to auto the generationType

Comments

0
@Id @Column(name = "RUN_ID")
@GeneratedValue(strategy=GenerationType.AUTO)
@SequenceGenerator(name="seq_run_id", sequenceName="seq_run_id")
private Long runId;

Hibernate is probably the only JPA Provider that selects the right Generationstrategy based on your databasetype. Using the GenerationType.AUTO statement hibernate will try to select the best strategy to implement an increasing row id.

Comments

0
  1. In Oracle : It's no need for an Oracle trigger, but the sequence is must.

  2. In pojo: you can use Annotations like this:

    @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="majorsSeq") @SequenceGenerator(name="majorsSeq", sequenceName="MAJORS_SEQ", allocationSize = 1,initialValue = 1) public int getId() { return id; }

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.