File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes TableGenerator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "TableGenerator" Watch "TableGenerator" New topic
Author

TableGenerator

James Clarke
Ranch Hand

Joined: Oct 04, 2004
Posts: 148
Hi all,

Can someone explain how the TableGenerator works with EJB, I am not sure how its used to generate primary key. I have some doubts mainly :

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?

2) what type of databases allow this or is the increment dependent on the application server ?

3) What is the advantage of this rather than using a database sequence for example ?

Below is an example I found in another discussion, can we use this as an example to understand this:

@Entity
@TableGenerator(name="CUST_GENERATOR"
table="GENERATOR_TABLE"
pkColumnName="PRIMARY_KEY_COLUMN"
valueColumnName="VALUE_COLUMN"
pkColumnValue="CUST_ID"
allocationSize=1)
public class Customer implements java.io.Serializable {
...
}

@Entity
@TableGenerator(name="CUST_GENERATOR"
table="GENERATOR_TABLE"
pkColumnName="PRIMARY_KEY_COLUMN"
valueColumnName="VALUE_COLUMN"
pkColumnValue="PROD_ID"
allocationSize=1)
public class Product implements java.io.Serializable {
...
}

As you can see we want the both entities to use the same table GENERATOR_TABLE to hold their Primary-Key-Next-Value.

Suppose also we've just persisted 3 Customer entities and 8 Product entities into the DB.

In this case the GENERATOR_TABLE table would look like:

code:
------------------------------------------------
| PRIMARY_KEY_COLUMN | VALUE_COLUMN |
------------------------------------------------
| CUST_ID | 4 |
| PROD_ID | 9 |
------------------------------------------------

Thanks in advance,

James
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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 ?

Not all databases support sequences.


[My Blog]
All roads lead to JavaRanch
Sushma Mummasani
Greenhorn

Joined: May 26, 2010
Posts: 1
Hi there,

Could anyone explain me how Primary key generation stratgies (like Identity, Sequence, table) works internally in EJB3??

What are the differences between them??

Especially whats the difference between @GeneratedValue(strategy=GenerationType.IDENTITY) and @GeneratedValue(strategy=GenerationType.IDENTITY) and @GeneratedValue(strategy=GenerationType.TABLE) ??

Sushma
Akshay Sahu
Greenhorn

Joined: Sep 04, 2009
Posts: 26

Dear Sushma,

The following is an explanation to all the GenerationTypes supported in EJB3.

1. GenerationType.AUTO:

Whenever the container sees this generation type, it automatically assigns a value to the column annotated with it.
No special treatment is required.

2. GenerationType.IDENTITY:

In MS SQL Server, you have a constraint by name Identity.

Sample:
CREATE TABLE EmpTable(
EMPID bigint identity(1,1),
EMPNAME varchar(40) not null
);

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.

Sample:
INSERT INTO EmpTable values ("AkshaySahu");
GO
INSERT INTO EmpTable values ("SachinTendulkar");
GO


SELECT * FROM EmpTable
GO

Output:
**********************
EMPID | EMPNAME
**********************
1 AkshaySahu
2 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.

3. GenerationType.TABLE:

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:

a. GenerationType.TABLE:
- 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.

b. GenerationType.IDENTITY:
- 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.

4. GenerationType.SEQUENCE:

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
MINVALUE value
MAXVALUE value
START WITH value
INCREMENT BY value
CACHE value;

For example:

CREATE SEQUENCE customer_id_sequence
MINVALUE 1
MAXVALUE 999999999999999999999999999
START WITH 1
INCREMENT BY 1
CACHE 20;

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.

Thanks,
Akshay Sahu



Akshay Sahu
OCBCD (EJB 3), OCJP (Java SE 6), MCTS (MS SQL Server 2005)
 
Don't get me started about those stupid light bulbs.
 
subject: TableGenerator