This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
1) I understand theres some table created which holds the primary key values but how is the table created and how is the value incremented?
The table you have drawn at the end of your post illustrates this well. Look at your Product definition :
If the container is set to autocreate tables during deployment, when he sees this, he will generate a table called CUST_GENERATOR, whose primary key is PRIMARY_KEY_COLUMN. The VALUE_COLUMN holds the next value to be generated. The pkColumnValue attribute allows you to tell the container which primary key to look for in the generator table. Here, it take the value of the primary key whose value is "PROD_ID". When a new entity is created, the container will execute a SELECT VALUE_COLUMN FROM CUST_GENERATOR WHERE PRIMARY_KEY_COLUMN = 'PROD_ID', and update the table with the incremented value.
2) what type of databases allow this or is the increment dependent on the application server ?
I've read that it was completely portable.
3) What is the advantage of this rather than using a database sequence for example ?
The clause identity(1,1) means, that the value of the column EMPID for the first record should be 1. And for the next record it should be 2 and so on. These values are generated by MS SQL Server itself. We do not pass this value to the query while inserting the record.
INSERT INTO EmpTable values ("AkshaySahu");
INSERT INTO EmpTable values ("SachinTendulkar");
So as you have seen, you do not supply values for the identity columns in your query.
So when you annotated a column as @GeneratedValue(strategy=GenerationType.IDENTITY), then the container will not supply any value for that column, and the value generation for that column will be taken care by the vendor database server itself.
This tells the persistence engine (Container) to keep a track of the primary key value increments in a separate table. The table is generated by the container and the values are stored in it. And for any new record, the primary key value is taken from the generated table. Rather than depending on the vendor database server.
Comparing GenerationType.TABLE and GenerationType.IDENTITY:
- In GenerationType.TABLE, the primary key values are tracked in a separate table maintained by the container.
- The column annotated with GenerationType.TABLE will contain a value in the query, which is passed to the database server for further processing.
- In GenerationType.IDENTITY, the primary key values are tracked by the database server itself. Nothing is done with that column by the container.
- The column annotated with GenerationType.IDENTITY will not contain a value in the query, which is passed to the database server for further processing.
In Oracle, you can create an autonumber field by using sequences. A sequence is an object in Oracle that is used to generate a number sequence. This can be useful when you need to create a unique number to act as a primary key.
The syntax for a sequence is:
CREATE SEQUENCE sequence_name
START WITH value
INCREMENT BY value
CREATE SEQUENCE customer_id_sequence
START WITH 1
INCREMENT BY 1
This would create a sequence object called customer_id_sequence.
The first sequence number that it would use is 1
and each subsequent number would increment by 1 (ie: 2,3,4,...}.
It will cache up to 20 values for performance.
To support this feature in EJB 3, we have GenerationType.SEQUENCE.
If you like this post, then pray for all those poor in India, who still have to strive hard for a single meal per day.
OCBCD (EJB 3), OCJP (Java SE 6), MCTS (MS SQL Server 2005)