5 Replies Latest reply on Jul 29, 2005 5:08 PM by msly

    Using @Id to set autoicrement field

      I have a Sybase ASA database
      I need to have the a column named survey_id autoincrement. Autoincrement is the type for ASA not Identity like ASE.

      Here is the field definition
      survey_id autoincrement

      I have this working in a CMP 2.1 implementation but I am trying to get this going with EJB 3.0.

      Here is the Entity Bean

      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratorType;
      //import javax.persistence.GeneratorType;
      import javax.persistence.Id;
      import javax.persistence.Table;

      @Entity
      @Table(name = "survey")
      public class Survey {
      private Integer surveyId;
      private String surveyName;
      private String surveyDescription;
      private int surveyQuestionCount;
      private String surveyPasswordProtect;
      private String surveyPassword;
      private String surveyActive;
      private String userUsername;

      public Survey () { }

      public Survey(String active, String description, String name, String password, String protect, int count, String username) {
      surveyActive = active;
      surveyDescription = description;
      surveyName = name;
      surveyPassword = password;
      surveyPasswordProtect = protect;
      surveyQuestionCount = count;
      userUsername = username;
      }
      @Column(name = "survey_active")
      public String getSurveyActive() {
      return surveyActive;
      }
      public void setSurveyActive(String surveyActive) {
      this.surveyActive = surveyActive;
      }
      @Column(name = "survey_description")
      public String getSurveyDescription() {
      return surveyDescription;
      }
      public void setSurveyDescription(String surveyDescription) {
      this.surveyDescription = surveyDescription;
      }
      @Id(generate = GeneratorType.AUTO)
      @Column(name = "survey_id")
      public Integer getSurveyId() {
      return surveyId;
      }
      public void setSurveyId(Integer surveyId) {
      this.surveyId = surveyId;
      }
      @Column(name = "survey_name")
      public String getSurveyName() {
      return surveyName;
      }
      public void setSurveyName(String surveyName) {
      this.surveyName = surveyName;
      }
      @Column(name = "survey_password")
      public String getSurveyPassword() {
      return surveyPassword;
      }
      public void setSurveyPassword(String surveyPassword) {
      this.surveyPassword = surveyPassword;
      }
      @Column(name = "survey_password_protect")
      public String getSurveyPasswordProtect() {
      return surveyPasswordProtect;
      }
      public void setSurveyPasswordProtect(String surveyPasswordProtect) {
      this.surveyPasswordProtect = surveyPasswordProtect;
      }
      @Column(name = "survey_question_count")
      public int getSurveyQuestionCount() {
      return surveyQuestionCount;
      }
      public void setSurveyQuestionCount(int surveyQuestionCount) {
      this.surveyQuestionCount = surveyQuestionCount;
      }
      @Column(name = "user_username")
      public String getUserUsername() {
      return userUsername;
      }
      public void setUserUsername(String userUsername) {
      this.userUsername = userUsername;
      }

      }

      1)Here are the errors with the following setting: @Id(generate = GeneratorType.IDENTITY)
      15:18:44,015 WARN [JDBCExceptionReporter] SQL Error: 504, SQLState: 52W09
      15:18:44,015 ERROR [JDBCExceptionReporter] ASA Error -265: Procedure 'identity' not found

      2) Here is the error with the following setting: @Id
      ids for this class must be manually assigned before calling save():

      3) Here are the errors with the following setting @Id(generate = GeneratorType.AUTO)
      16:07:27,515 WARN [JDBCExceptionReporter] SQL Error: 504, SQLState: 52W09
      16:07:27,515 ERROR [JDBCExceptionReporter] ASA Error -265: Procedure 'identity' not found

      What am I missing here? Any help would be appreciated.

      Do i need some kind of override in an XML file?

      Here is one more clue

      If I convert the surveyId field of the bean to int and set the @Id(generate = GenratorType.NONE) it inserts a row with survey_d 0. It fails the second time of course.

      Mike

        • 1. Re: Using @Id to set autoicrement field
          epbernard

          do you use the ASA dialect?

          • 2. Re: Using @Id to set autoicrement field

            I am new to this. Do you mean the SQL syntax used in ASA versus ANSI SQL, or is the dialect something different. I have a CMP 2.1 EJB working and it automatically increments the id field, but the setting are specified in the XML file for the beans. I am trying to get the same result using the annotations.

            Here is an excerpt of the ejb-jar.XML :

            <display-name>Survey</display-name>
            <ejb-name>Survey</ejb-name>
            <local-home>surveyportalcomponents.SurveyHome</local-home>
            surveyportalcomponents.Survey
            <ejb-class>surveyportalcomponents.SurveyBean</ejb-class>
            <persistence-type>Container</persistence-type>
            <prim-key-class>java.lang.Integer</prim-key-class>
            False
            <cmp-version>2.x</cmp-version>
            <abstract-schema-name>Survey</abstract-schema-name>
            <cmp-field>
            <field-name>surveyId</field-name>
            <column-name>survey_id</column-name>
            </cmp-field>
            <cmp-field>

            Here an excerpt from JBossCMP-JDBC.xml:

            <unknown-pk>
            <unknown-pk-class>java.lang.Integer</unknown-pk-class>
            <column-name>survey_id</column-name>
            <jdbc-type>INTEGER</jdbc-type>
            <sql-type>INTEGER</sql-type>
            <auto-increment/>
            </unknown-pk>
            <entity-command name="sybase-fetch-key"/>

            How do I specify these things in EJB 3 with annotations?





            • 3. Re: Using @Id to set autoicrement field

              Epbernard any clues? How would you do this using hibenate?

              • 5. Re: Using @Id to set autoicrement field

                Thanks epbernard.

                For anyone else working with EJB3 you set this property in the persistance.xml file in your par archive for the entity beans.

                Here is the file:

                <?xml version="1.0" encoding="UTF-8"?>
                <entity-manager>
                 <name>survey</name>
                 <jta-data-source>java:/jdbc/SybaseDB</jta-data-source>
                 <properties>
                 <property name="hibernate.hbm2ddl.auto"
                 value="create-drop"/>
                 <property name="hibernate.dialect" value="org.hibernate.dialect.SybaseAnywhereDialect"/>
                 <!-- This is for ASE -->
                 <!-- property name="hibernate.dialect" value="org.hibernate.dialect.SybaseDialect"/ -->
                 </properties>
                </entity-manager>