Reputation: 99
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
Reputation: 1
In Oracle : It's no need for an Oracle trigger, but the sequence is must.
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
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
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