This content has been marked as final.
Show 2 replies
-
1. Re: firebird&jboss - entity-command an pk generation
petrkalina Mar 29, 2006 11:33 AM (in response to petrkalina)I solved the problem myself.
The simpliest solution for me seemed indeed to create my own entity command:public class JDBCFirebirdCreateCommand extends JDBCInsertPKCreateCommand { private String generator; private String generatorSQL; protected JDBCCMPFieldBridge pkField; public JDBCFirebirdCreateCommand() { super(); } protected void initGeneratedFields() throws DeploymentException { super.initGeneratedFields(); pkField = getGeneratedPKField(); } public void init(JDBCStoreManager manager) throws DeploymentException { super.init(manager); } protected void initEntityCommand(JDBCEntityCommandMetaData entityCommand) throws DeploymentException { super.initEntityCommand(entityCommand); generator = entityCommand.getAttribute("generator"); String pkFieldName; JDBCFieldBridge[] pkFields = entity.getPrimaryKeyFields(); if ( pkFields.length != 1 ) throw new DeploymentException("More htan one or missing primary key field"); pkFieldName = pkFields[0].getFieldName(); if (generator == null) { generator = entity.getQualifiedTableName() + "_" + pkFieldName.toUpperCase() + "_GEN"; } generatorSQL = "SELECT GEN_ID("+generator+",1) FROM RDB$DATABASE;"; if (debug) { log.debug("SEQUENCE SQL is :"+generatorSQL); } } protected void generateFields(EntityEnterpriseContext ctx) throws CreateException { super.generateFields(ctx); Connection con = null; Statement s = null; ResultSet rs = null; try { if(debug) { log.debug("Executing SQL: " + generatorSQL); } DataSource dataSource = entity.getDataSource(); con = dataSource.getConnection(); s = con.createStatement(); rs = s.executeQuery(generatorSQL); if(!rs.next()) { throw new CreateException("Error fetching next primary key value: result set contains no rows"); } pkField.loadInstanceResults(rs, 1, ctx); } catch(SQLException e) { log.error("Error fetching the next primary key value", e); throw new CreateException("Error fetching the next primary key value:" + e); } finally { JDBCUtil.safeClose(rs); JDBCUtil.safeClose(s); JDBCUtil.safeClose(con); } } }
the "path" to the command has to be added to standardjbosscmp-jdbc.xml or to some other place that siuts you (jbosscmp-jdbc.xml..)<!-- retrieves the currval of the the sequence associated with a PostgreSQL SERIAL column --> <entity-command name="firebird-generate-pk" class="cz.jlabs.bookstore.ejb.integration.JDBCFirebirdCreateCommand"> <!-- uncomment to change the name of the sequence; default is ${table}_${pkColumn}_seq <attribute name="sequence"></attribute> --> </entity-command>
in a jbosscmp-jdbc.xml the bean definition could look like this:<jbosscmp-jdbc> <defaults> <datasource>java:/FirebirdDS</datasource> <datasource-mapping>Firebird</datasource-mapping> <create-table>false</create-table> </defaults> <enterprise-beans> <!-- To add beans that you have deployment descriptor info for, add a file to your XDoclet merge directory called jbosscmp-jdbc-beans.xml that contains the <entity></entity> markup for those beans. --> <entity> <ejb-name>Book</ejb-name> <table-name>books</table-name> <cmp-field> <field-name>pk</field-name> <column-name>pk</column-name> <auto-increment/> </cmp-field> <cmp-field> <field-name>author</field-name> <column-name>author</column-name> </cmp-field> <cmp-field> <field-name>title</field-name> <column-name>title</column-name> </cmp-field> .... <entity-command name="firebird-generate-pk"> </entity-command> </entity> ....
and that's all there is.. -
2. Re: firebird&jboss - entity-command an pk generation
petrkalina Mar 29, 2006 11:34 AM (in response to petrkalina)oh, sorry about the PostgreSQL comment..