4 Replies Latest reply on Apr 30, 2008 1:20 PM by mnrz

    regarding @PrePersist and ID assignment

    mnrz

      Hi all
      I have an entity in which I need to assign my own ID to its primary key,
      I put the code to extract the id and set it to the field in a PrePersist method, however, the ejb validates the entity before calling that callback method so the exception will be thrown

      my question is where should I put this code or how can I say the EJB not to validate before persisting!

      any comment would be of great help

      thanks

        • 1. Re: regarding @PrePersist and ID assignment
          jaikiran

           

          "mnrz" wrote:
          Hi all
          I have an entity in which I need to assign my own ID to its primary key,
          I put the code to extract the id and set it to the field in a PrePersist method,


          You should be setting the id before calling persist. Why are you trying to do that in a callback method?


          • 2. Re: regarding @PrePersist and ID assignment
            mnrz

             

            "jaikiran" wrote:

            You should be setting the id before calling persist. Why are you trying to do that in a callback method?


            thanks for your reply, the thing is that I want to calculate the ID from a formula in which I use an algorithm. however, if I set any field other than ID it won't work as well

            • 3. Re: regarding @PrePersist and ID assignment
              jaikiran

               

              "mnrz" wrote:

              the thing is that I want to calculate the ID from a formula in which I use an algorithm. however, if I set any field other than ID it won't work as well


              I probably am not understanding the requirements of your application correctly. Can't you calculate the ID from the formula using your algorithm, before calling the persist method on entity manager? What i am trying to understand is why does the ID calculation or setting any other field be part of the callback method.

              • 4. Re: regarding @PrePersist and ID assignment
                mnrz

                well, because my DAO classes may be called from any client and each developer may call them from there codes so I don't want to compel them to calculate the id each time they are using my service classes or force them to set some fields that are not necessary from their point of view.