0 Replies Latest reply on May 10, 2005 5:48 PM by Roland Brown

    An example of using an identity column for a primary key in

    Roland Brown Newbie

      I spent several days trying to figure this out and have finaly succeeded. I could not find any place that had all this information in one spot so here it is.

      I was trying to create an entity bean that used an identity column in an MSSQL table for it's primary key. Here is what I finaly came up with.

      I have finally successfully inserted a row into a MSSQL database table using an entity bean whose primary key is bound to an identity column.

      The code has two entiy EJB's. Look for the PropsIdentiyEJB. The trick was to set the prim-key-class to java.lang.Object, the return type of the ejbCreate() method to Object, the findByPrimaryKey parmeter to Object, the unknown-pk-class to java.lang.Integer and add the field-name tag set to key.

      Here's the script I used to create the table. Look for the PropsIdentityEJB ejb.

      CREATE TABLE [dbo].[PropsTableIdentity] (
       [key] [int] IDENTITY (1, 1) NOT NULL ,
       [value] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
      ) ON [PRIMARY]
      GO
      



      Jbosscmp-jdbc.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE jbosscmp-jdbc PUBLIC "-//JBoss//DTD JBOSSCMP-JDBC 4.0//EN"
       "http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_4_0.dtd">
      <jbosscmp-jdbc>
       <enterprise-beans>
       <entity>
       <ejb-name>PropsIdentityEJB</ejb-name>
       <datasource>java:/webquotes</datasource>
       <datasource-mapping>MS SQLSERVER</datasource-mapping>
       <pk-constraint>false</pk-constraint>
       <table-name>PropsTableIdentity</table-name>
       <cmp-field>
       <field-name>value</field-name>
       <column-name>PropsTableIdentity.value</column-name>
       </cmp-field>
       <unknown-pk>
       <unknown-pk-class>java.lang.Integer</unknown-pk-class>
       <field-name>key</field-name>
       <column-name>key</column-name>
       <jdbc-type>INTEGER</jdbc-type>
       <sql-type>INTEGER</sql-type>
       <auto-increment/>
       </unknown-pk>
       <entity-command name="mssql-fetch-key"/>
       </entity>
       <entity>
       <ejb-name>PropsTableEJB</ejb-name>
       <datasource>java:/webquotes</datasource>
       <datasource-mapping>MS SQLSERVER</datasource-mapping>
       <table-name>PropsTest</table-name>
       <cmp-field>
       <field-name>keyid</field-name>
       <column-name>PropsTest.keyid</column-name>
       </cmp-field>
       <cmp-field>
       <field-name>value</field-name>
       <column-name>PropsTest.value</column-name>
       </cmp-field>
       </entity>
       </enterprise-beans>
      </jbosscmp-jdbc>
      


      ejb-jar.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
      <ejb-jar>
       <enterprise-beans>
       <session>
       <ejb-name>HelloEJB</ejb-name>
       <home>com.dynix.webquote.HelloHome</home>
       <remote>com.dynix.webquote.Hello</remote>
       <ejb-class>com.dynix.webquote.HelloBean</ejb-class>
       <session-type>Stateless</session-type>
       <transaction-type>Container</transaction-type>
       </session>
       <entity>
       <description>test of entity</description>
       <display-name>PropsIdentityEJB</display-name>
       <ejb-name>PropsIdentityEJB</ejb-name>
       <home>com.dynix.webquotes10.PropsIdentityHome</home>
       <remote>com.dynix.webquotes10.PropsIdentity</remote>
       <ejb-class>com.dynix.webquotes10.PropsIdentityBean</ejb-class>
       <persistence-type>Container</persistence-type>
       <prim-key-class>java.lang.Object</prim-key-class>
       <reentrant>False</reentrant>
       <cmp-version>2.x</cmp-version>
       <abstract-schema-name>PropsIdentityEJB</abstract-schema-name>
       <cmp-field>
       <description>value type</description>
       <field-name>value</field-name>
       </cmp-field>
       </entity>
       <entity>
       <description>PropsTableEJB description</description>
       <display-name>PropsTableEJB</display-name>
       <ejb-name>PropsTableEJB</ejb-name>
       <home>com.dynix.webquotes10.PropsTableHome</home>
       <remote>com.dynix.webquotes10.PropsTable</remote>
       <ejb-class>com.dynix.webquotes10.PropsTableBean</ejb-class>
       <persistence-type>Container</persistence-type>
       <prim-key-class>java.lang.String</prim-key-class>
       <reentrant>False</reentrant>
       <cmp-version>2.x</cmp-version>
       <abstract-schema-name>PropsTableEJB</abstract-schema-name>
       <cmp-field>
       <description>Keyid col</description>
       <field-name>keyid</field-name>
       </cmp-field>
       <cmp-field>
       <description>value col</description>
       <field-name>value</field-name>
       </cmp-field>
       <primkey-field>keyid</primkey-field>
       </entity>
       </enterprise-beans>
      </ejb-jar>
      


      Remote file PropsIdentity.java
      package com.dynix.webquotes10;
      
      import javax.ejb.EJBObject;
      import java.rmi.RemoteException;
      import com.dynix.webquotes10.PropsIdentityPK;
      
      /**
       * Created by IntelliJ IDEA.
       * User: rb
       * Date: May 5, 2005
       * Time: 12:43:41 PM
       * To change this template use File | Settings | File Templates.
       */
      
      public interface PropsIdentity extends EJBObject {
      
       String getValue() throws RemoteException;
      
       void setValue(String value) throws RemoteException;
      }
      


      Bean file PropsIdentityBean.java
      package com.dynix.webquotes10;
      
      import javax.ejb.*;
      import com.dynix.webquotes10.PropsIdentityPK;
      
      /**
       * Created by IntelliJ IDEA.
       * User: rb
       * Date: May 5, 2005
       * Time: 12:43:40 PM
       * To change this template use File | Settings | File Templates.
       */
      
      public abstract class PropsIdentityBean implements EntityBean {
      
       public PropsIdentityBean() {
       }
      
       public void setEntityContext(EntityContext entityContext) throws EJBException {
       }
      
       public void unsetEntityContext() throws EJBException {
       }
      
       public void ejbRemove() throws RemoveException, EJBException {
       }
      
       public void ejbActivate() throws EJBException {
       }
      
       public void ejbPassivate() throws EJBException {
       }
      
       public void ejbLoad() throws EJBException {
       }
      
       public void ejbStore() throws EJBException {
       }
      
       public Object ejbCreate(String value) throws CreateException {
       setValue(value);
       return null; //To change body of implemented methods use File | Settings | File Templates.
       }
      
       public void ejbPostCreate(String value) throws CreateException {
       //To change body of implemented methods use File | Settings | File Templates.
       }
      
       public abstract String getValue();
      
       public abstract void setValue(String value);
      }
      



      Home file PropsIdentityHome.java
      package com.dynix.webquotes10;
      
      import javax.ejb.EJBHome;
      import javax.ejb.FinderException;
      import java.rmi.RemoteException;
      import com.dynix.webquotes10.PropsIdentity;
      import com.dynix.webquotes10.*;
      
      /**
       * Created by IntelliJ IDEA.
       * User: rb
       * Date: May 5, 2005
       * Time: 12:43:41 PM
       * To change this template use File | Settings | File Templates.
       */
      
      public interface PropsIdentityHome extends EJBHome {
       PropsIdentity create(
       java.lang.String value
       ) throws javax.ejb.CreateException, java.rmi.RemoteException;
      
       PropsIdentity findByPrimaryKey(Object key) throws RemoteException, FinderException;
      }
      


      web.xml file
      <?xml version="1.0" encoding="UTF-8"?>
      
      <web-app version="2.4"
       xmlns="http://java.sun.com/xml/ns/j2ee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
      
       <security-constraint>
       <web-resource-collection>
       <web-resource-name>Protected Area</web-resource-name>
       <url-pattern>/examples/*</url-pattern>
       <url-pattern>/home/*</url-pattern>
       <url-pattern>/jsp/*</url-pattern>
       </web-resource-collection>
       <auth-constraint>
       <role-name>administrator</role-name>
       <role-name>configurator</role-name>
       <role-name>proposals</role-name>
       <role-name>salesrep</role-name>
       </auth-constraint>
       </security-constraint>
      
       <security-role>
       <role-name>administrator</role-name>
       </security-role>
      
      
       <!-- LOGIN AUTHENTICATION -->
      
       <login-config>
       <auth-method>FORM</auth-method>
       <realm-name>webquotes</realm-name>
       <form-login-config>
       <form-login-page>/login.jsp</form-login-page>
       <form-error-page>/loginerror.jsp</form-error-page>
       </form-login-config>
       </login-config>
      
      
       <servlet>
       <servlet-name>HelloServlet</servlet-name>
       <servlet-class>com.dynix.webquotes.examples.HelloServlet</servlet-class>
       </servlet>
      
       <servlet>
       <servlet-name>action</servlet-name>
       <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
       <init-param>
       <param-name>config</param-name>
       <param-value>/WEB-INF/struts-config.xml</param-value>
       </init-param>
       <init-param>
       <param-name>debug</param-name>
       <param-value>2</param-value>
       </init-param>
       <init-param>
       <param-name>detail</param-name>
       <param-value>2</param-value>
       </init-param>
       <load-on-startup>2</load-on-startup>
       </servlet>
      
       <servlet-mapping>
       <servlet-name>action</servlet-name>
       <url-pattern>*.do</url-pattern>
       </servlet-mapping>
       <servlet-mapping>
       <servlet-name>HelloServlet</servlet-name>
       <url-pattern>/greeting</url-pattern>
       </servlet-mapping>
      
       <!-- The Usual Welcome File List -->
       <welcome-file-list>
       <welcome-file>index.jsp</welcome-file>
       </welcome-file-list>
      
      
      
       <ejb-ref>
       <ejb-ref-name>ejb/hello</ejb-ref-name>
       <ejb-ref-type>Session</ejb-ref-type>
       <home>com.dynix.webquote.HelloHome</home>
       <remote>com.dynix.webquote.Hello</remote>
       </ejb-ref>
       <ejb-ref>
       <ejb-ref-name>ejb/Props</ejb-ref-name>
       <ejb-ref-type>Entity</ejb-ref-type>
       <home>com.dynix.webquotes10.PropsIdentityHome</home>
       <remote>com.dynix.webquotes10.PropsIdentity</remote>
       </ejb-ref>
       <ejb-ref>
       <ejb-ref-name>ejb/PropsTable</ejb-ref-name>
       <ejb-ref-type>Entity</ejb-ref-type>
       <home>com.dynix.webquotes10.PropsTableHome</home>
       <remote>com.dynix.webquotes10.PropsTable</remote>
       </ejb-ref>
      
      </web-app>
      



      jboss-web.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 2.4//EN"
       "http://www.jboss.org/j2ee/dtd/jboss-web_4_0.dtd">
      <jboss-web>
      <!-- <security-domain>java:/jaas/webquotes</security-domain> -->
       <ejb-ref>
       <ejb-ref-name>ejb/hello</ejb-ref-name>
       <jndi-name>hello</jndi-name>
       </ejb-ref>
       <ejb-ref>
       <ejb-ref-name>ejb/Props</ejb-ref-name>
       <jndi-name>PropsIdentity</jndi-name>
       </ejb-ref>
       <ejb-ref>
       <ejb-ref-name>ejb/PropsTable</ejb-ref-name>
       <jndi-name>PropsTable</jndi-name>
       </ejb-ref>
      </jboss-web>
      


      Hope this helps somebody.