Sophiya
Sophiya

Reputation: 99

Autoincrement column in oracle using hibernate

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

Upvotes: 1

Views: 5887

Answers (4)

user6192626
user6192626

Reputation: 1

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

Upvotes: 0

Lukas Eichler
Lukas Eichler

Reputation: 5913

@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.

Upvotes: 0

Ondrej Bozek
Ondrej Bozek

Reputation: 11511

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.

Upvotes: 1

Jeremie
Jeremie

Reputation: 379

Try removing the generator and setting to auto the generationType

Upvotes: 0

Related Questions