8 Replies Latest reply on Jan 22, 2009 10:04 PM by dmrc

    Can't update primary key fields

    infprog2

      Hello everybody,


      I've generated my application with seam-gen from a database, because it's my first project with seam.
      Little by little I'm improving my application but there is one thing that I can't solve.


      When I try to update a record, if I've changed a primary key field, it seems that all is ok, even show me 'Successfully updated' but the change doesn't take place in the database.


      However, if I change any other field, all is ok.


      How can I modify the PK field of a record?


      Thanks in advance

        • 1. Re: Can't update primary key fields
          tom_goring

          I'm not sure I'd recommend (or even if it's valid) to change the PK of an EJB3 bean. 


          What I do in this situation is delete the old one and create a clone with the new key.

          • 2. Re: Can't update primary key fields
            tom_goring

            also depends on your reason to update the key.... it may be a better idea to have a technical key (e.g. a long) and a business key (which is the one you update).


            • 3. Re: Can't update primary key fields
              andygibson.contact.andygibson.net

              Chances are that you have the primary key or Id field marked with the @GeneratedValue annotation meaning that the value is generated from the database. I'm guessing that since the value is generated from the database, then JPA has no need to be concerned with updating it.


              Some code relating to the entity bean might help,


              Cheers,


              Andy Gibson



              • 4. Re: Can't update primary key fields
                dmrc

                Hello


                i have same problem. my entity have id and i want its generated from database.



                   @Id @GeneratedValue
                    public Long getId() {
                        return id;
                    }



                And i want to update in user.xhtml but its doesnt work.



                action="#{userHome.update}"



                How can i solve this problem ?


                • 5. Re: Can't update primary key fields
                  stefanotravelli

                  From Mapping entities with identity, Hibernate In Action, p. 163



                  Hibernate (and JPA) doesn't allow you to change the identifier value of a persistent instance after it's first assigned. A primary key value never changes - otherwise the attribute wouldn't be a suitable primary key candidate!



                  • 6. Re: Can't update primary key fields
                    dmrc

                    Hello i dont want update id value, i want update name and code values.



                       @Id @GeneratedValue
                        public Long getId() {
                            return id;
                        }
                    
                        public void setId(Long id) {
                            this.id = id;
                        }
                    
                        @NotNull
                        @Length(max = 20)
                        public String getName() {
                            return name;
                        }
                    
                        public void setName(String name) {
                            this.name = name;
                        }
                    
                        @NotNull
                        @Length(max=10)
                        public String getCode() {
                         return code;
                        }
                    
                         public void setCode(String code) {
                         this.code = code;
                        }
                    




                          <rich:panel>
                                <f:facet name="header">fakulte</f:facet>
                    
                                <s:decorate id="nameField" template="layout/edit.xhtml">
                                    <ui:define name="label">Fakulte Adi</ui:define>
                                    <h:inputText id="name" required="true"
                                                 value="#{fakulteHome.instance.name}"/>
                                </s:decorate>
                                
                                 <s:decorate id="codeField" template="layout/edit.xhtml">
                                    <ui:define name="label">Fakulte Kodu</ui:define>
                                    <h:inputText id="code" required="true"
                                                 value="#{fakulteHome.instance.code}"/>
                                </s:decorate>
                    
                                <div style="clear:both"/>
                    
                            </rich:panel>
                            <h:commandButton id="update"
                                              value="Update"
                                             action="#{fakulteHome.update}"
                                           rendered="#{fakulteHome.managed}"/>



                    But its doenst work.



                    • 7. Re: Can't update primary key fields

                      Nazmi Demirci, the person that initiated this thread wrote as subject: Can't update primary key fields, if you write: i have same problem and the write: i don't want update id value, i want update name and code that is a contradiction, you do not have the same problem, and that means you should have started your own thread.


                      Also, saying it does not work is not the way to get help in an internet forum. You have to be specific, post the stack trace, or something that we can use to understand your problem.

                      • 8. Re: Can't update primary key fields
                        dmrc

                        Francisco Peredo wrote on Jan 22, 2009 21:06:


                        Nazmi Demirci, the person that initiated this thread wrote as subject: Can't update primary key fields, if you write: i have same problem and the write: i don't want update id value, i want update name and code that is a contradiction, you do not have the same problem, and that means you should have started your own thread.

                        Also, saying it does not work is not the way to get help in an internet forum. You have to be specific, post the stack trace, or something that we can use to understand your problem.


                        You are rigt Francisco Peredo. i am newbie. i have a litte time and i didnt careful. When i click update button , its doesnt update value in database.And its not giving error.