Using a database sequence to generate primary key values for entity beans in Java

Generating PDF 417 in Java Using a database sequence to generate primary key values for entity beans

Using a database sequence to generate primary key values for entity beans
Encode PDF 417 In Java
Using Barcode generator for Java Control to generate, create PDF 417 image in Java applications.
www.OnBarcode.com
PDF417 Decoder In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
contains the partial source from the SequenceBean BMP entity bean, illustrates acquiring a primary key value in the ejbCreate() method.
EAN128 Creator In Java
Using Barcode generator for Java Control to generate, create EAN 128 image in Java applications.
www.OnBarcode.com
PDF 417 Generator In Java
Using Barcode generator for Java Control to generate, create PDF417 image in Java applications.
www.OnBarcode.com
Listing 3.7 SequenceBean.java
GS1 DataBar Limited Printer In Java
Using Barcode creation for Java Control to generate, create DataBar image in Java applications.
www.OnBarcode.com
Printing EAN128 In Java
Using Barcode creation for Java Control to generate, create EAN128 image in Java applications.
www.OnBarcode.com
public class SequenceBean implements EntityBean { public Integer ejbCreate( String name ) throws CreateException{ PreparedStatement ps = null; Connection con = null; ResultSet rs = null; this.name = name; try { String query = "select test_sequence.nextval from dual"; con = getConnection(); ps = con.prepareStatement( query ); ps.executeQuery(); rs = ps.getResultSet();
Making Barcode In Java
Using Barcode generator for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
Printing GTIN - 14 In Java
Using Barcode maker for Java Control to generate, create GTIN - 14 image in Java applications.
www.OnBarcode.com
Builds and executes the sequence query
PDF 417 Scanner In VB.NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
PDF 417 Drawer In None
Using Barcode encoder for Software Control to generate, create PDF417 image in Software applications.
www.OnBarcode.com
Retrieves a if( rs.next() ) value from the { result set sequenceId = rs.getInt(1); } else { String error = "ejbCreate: Sequence error creating"; System.out.println(error); throw new CreateException (error); }
Making Data Matrix ECC200 In Visual Studio .NET
Using Barcode generator for Reporting Service Control to generate, create ECC200 image in Reporting Service applications.
www.OnBarcode.com
Creating Code39 In Visual Studio .NET
Using Barcode creation for Reporting Service Control to generate, create Code 3 of 9 image in Reporting Service applications.
www.OnBarcode.com
ps.close(); rs.close(); query = "insert into ejbSequence( sequenceId, name ) " + values ( , )"; ps = con.prepareStatement(query); Creates the ps.setInt(1, sequenceId); entity data ps.setString(2, name); if ( !(ps.executeUpdate() > 0) ) { String error = "ejbCreate: SequenceBean (" + name + ") not created"; System.out.println(error); throw new NoSuchEntityException (error); } ps.close(); } catch(SQLException sqe) { throw new EJBException (sqe); }
Decoding European Article Number 13 In .NET Framework
Using Barcode reader for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Reading Universal Product Code Version A In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Working with data
Drawing EAN / UCC - 13 In None
Using Barcode generation for Font Control to generate, create EAN-13 Supplement 5 image in Font applications.
www.OnBarcode.com
Scan UPCA In .NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
finally { try{ if(ps!=null) ps.close(); if(rs!=null) rs.close(); if(con!=null) con.close(); } catch(SQLException e){} } return new Integer( sequenceId ); } }
Making Barcode In VS .NET
Using Barcode creation for Visual Studio .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
Barcode Generator In None
Using Barcode generation for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Returns the primary key
GTIN - 13 Encoder In Objective-C
Using Barcode printer for iPad Control to generate, create EAN13 image in iPad applications.
www.OnBarcode.com
Generate DataMatrix In Visual Studio .NET
Using Barcode maker for ASP.NET Control to generate, create DataMatrix image in ASP.NET applications.
www.OnBarcode.com
CMP recipe For CMP entity beans, the solution is much simpler. Rewriting the SequenceBean, we need to add only a couple of data methods instead of including all the necessary JDBC code in the bean source:
abstract public class SequenceBean implements EntityBean { abstract public Integer getSequenceId(); abstract public void setSequenceId(Integer val); }
Next, we need to declare the deployment descriptor for the entity bean that signifies its primary key value and type (see listing 3.8).
Listing 3.8 Deployment descriptor
<ejb-jar> <enterprise-beans> <entity> <ejb-name>oracleSequence</ejb-name> <home>CMPsequence.SequenceHome</home> <remote>CMPsequence.Sequence</remote> <ejb-class>CMPsequence.SequenceBean</ejb-class> <persistence-type>Container</persistence-type> <prim-key-class>java.lang.Integer</prim-key-class> <reentrant>False</reentrant> Specifies the primary key type <cmp-version>2.x</cmp-version> <abstract-schema-name>SequenceBean</abstract-schema-name> <cmp-field> Describes <field-name>sequenceId</field-name> the </cmp-field> persistent <cmp-field> fields <field-name>name</field-name> Identifies the </cmp-field> primary key field <primkey-field>sequenceId</primkey-field>
Using a database sequence to generate primary key values for entity beans
</entity> </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <ejb-name>oracleSequence</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar>
Discussion
In the case of the BMP bean, acquiring a primary key value from a sequence is just a matter of completing an additional JDBC call. And after retrieving a primary key value, the BMP bean goes on to insert the new entity data into the database (because this is an ejbCreate() method). The CMP bean relies on the EJB container to provide the value of the primary key. In this case, we simply set up the bean and its deployment descriptor in the normal CMP entity bean way. However, in the vendor-specific descriptor, we indicate how the primary key value is acquired (and we also set up the schema mapping of the bean). For instance, we used a Weblogic container for this example. One of the vendor files, weblogic-cmp-rdbms-jar.xml, contains a snippet of XML specifying a database sequence for generating our primary key:
<automatic-key-generation> <generator-type>ORACLE</generator-type> <generator-name>test_sequence</generator-name> <key-cache-size>10</key-cache-size> </automatic-key-generation>
Check your vendor s documentation for your specific setup.
See also
2.5 Generating a primary key class 3.1 Using a data source 3.2 Creating EJB 2.0 container-managed persistence 3.5 Using a compound primary key for your entity beans
Working with data
3.5 Using a compound primary key for your entity beans
Problem
You want to use a combination of column values for a primary key for an entity bean.
Background
Typically, primary key values are more complex than a single column. Compound primary keys are an excellent way to drill down to specific data. Using a compound primary key is more complex for both CMP and BMP entity beans. For both types of beans, you must create a primary key class that follows specific rules, and also configure your entity bean to properly use an instance of the class.
Recipe
To provide a compound primary key for an entity bean, you must create a primary key class. In this recipe, we define a complex key for a portfolio holdings table. This key consists of a String for the portfolio name as well as a String for the symbol of an equity in the portfolio holding. Listing 3.9 shows a primary key class that meets the EJB 2.0 rules for primary key classes (and models the equity situation described).
Copyright © OnBarcode.com . All rights reserved.