1 2 Previous Next 17 Replies Latest reply on May 15, 2007 10:28 AM by rhinosystemsinc

    Getting Id of Presisted object.

    waheed.murad

      hii.

      i am using entitymanger to presist Object as:

      Customer cust;
      em.presist(cust);


      the id of customer is auto generated (in my case it is Oracle Sequence).

      after presistance i want to get the id of the presisted Object. like

      Customer cust;
      em.presist(cust);

      // Want to get the Id here;
      cust.getId();


      it returns null (as object is not syncronized with the database).
      thanks for you help.

        • 1. Re: Getting Id of Presisted object.
          christian.bauer

          em.persist(o);
          em.flush();
          o.getId();

          • 2. Re: Getting Id of Presisted object.
            rhinosystemsinc

            Hello, can someone give me some advice on how to setup a generated ID with Oracle, hibernate and SEAM...

            I have been trying to do a variety of ways - like "before insert" triggers and coupled with @GeneratedValue(strategy=GenerationType.IDENTITY) or @GeneratedValue(strategy=GenerationType.AUTO), but that didn't work. I have tried going straight to the sequence with:

            @Id
            @Column(name = "LISTINGTYPE_ID", unique = true, nullable = false, precision = 22, scale = 0)
            @SequenceGenerator(name="identifier", sequenceName="LISTINGTYPE_SEQ", allocationSize=1)
            @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="identifier")
            @NotNull

            That didn't work, it kept complaining the sequence didn't exist, although it did exist.

            I have read a dozen or more postings and can't get the right recipe working for Oracle. I really appreciate your help.

            Thanks!!!

            • 3. Re: Getting Id of Presisted object.
              waheed.murad

              This is how i have used oracle sequence.

              sequenceName="CUSTOMER_SEQ": Is the name is the name of your database sequence.

              name="CUSTOMER_SEQUENCE": U will use in Entity as i have used it with id field.


              /////////////////////////////////

              @Entity
              @Name("Customer")
              @Scope(EVENT)
              @Table(name="Customer")
              @SequenceGenerator(name="CUSTOMER_SEQUENCE", sequenceName="CUSTOMER_SEQ")
              public class ERegCustomer implements Serializable
              {

              private Integer id;

              @Id
              @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="CUSTOMER_SEQUENCE")
              public Integer getId()
              {
              return id;
              }

              public void setId(Integer id)
              {
              this.id = id;
              }

              • 4. Re: Getting Id of Presisted object.
                waheed.murad

                Thanks for your reply christian

                well and injecting EntityManager like this in my action class

                @PersistenceContext(unitName="myDatabase")
                protected EntityManager em;

                i think it will not allow me to use em.flush function as i think so i have tried it before and it throughs exception.(As per my information it is managed by the seam and it does not allow it.)

                • 5. Re: Getting Id of Presisted object.
                  christian.bauer

                  You can call em.flush() whenever you want, nobody can prevent that. However, if "whenever" is a good time to synchronize in-memory persistence context state with the database is another question. An exception would probably indicate that this is not a good time.

                  So no, you don't have any way or guarantee to get an identifier from an entity until it is finally persisted during flushing.

                  One other hand, if you use a sequence generator, Hibernate will assign an identifier to the instance at the time you call persist(o). But this is an implementation detail and not standardized anywhere.

                  • 6. Re: Getting Id of Presisted object.
                    rhinosystemsinc

                    Thanks for the info on the sequence - but it still gives me the same problem:

                    12:15:22,362 ERROR [URLDeploymentScanner] Incomplete Deployment listing:

                    --- MBeans waiting for other MBeans ---
                    ObjectName: persistence.units:ear=naturalmed2.ear,unitName=naturalmed2
                    State: FAILED
                    Reason: javax.persistence.PersistenceException: org.hibernate.HibernateExcepti
                    on: Missing sequence or table: NM.LISTINGTYPE_SEQ
                    I Depend On:
                    jboss.jca:service=DataSourceBinding,name=naturalmed2Datasource

                    --- MBEANS THAT ARE THE ROOT CAUSE OF THE PROBLEM ---
                    ObjectName: persistence.units:ear=naturalmed2.ear,unitName=naturalmed2
                    State: FAILED
                    Reason: javax.persistence.PersistenceException: org.hibernate.HibernateExcepti
                    on: Missing sequence or table: NM.LISTINGTYPE_SEQ
                    I Depend On:
                    jboss.jca:service=DataSourceBinding,name=naturalmed2Datasource


                    And I have double checked that the sequence is there in that NM schema...
                    This is consistent with what I have notice with other attempts too.

                    Any thoughts?
                    Thanks again!

                    • 7. Re: Getting Id of Presisted object.
                      monkeyden

                      Your sequence looks correct. I don't have the code here but I think I'm doing this:

                      em.persist(entity);
                      em.flush();
                      em.refresh(entity);


                      to get the sequence. I don't think EntityManager uses the primary key to find the persistent state.

                      • 8. Re: Getting Id of Presisted object.
                        christian.bauer

                        This is really turning into a Hibernate question. Your entity should have an identifier after you call persist(o), no matter where as long as you have a sequence-style generator. I recommend enabling the Hibernate DEBUG log and posting on the Hibernate forum.

                        • 9. Re: Getting Id of Presisted object.
                          christian.bauer

                          Well, if Hibernate says the sequence isn't there it probably isn't there or can't be seen by the user Hibernate connects with.

                          • 10. Re: Getting Id of Presisted object.
                            monkeyden

                             

                            Your entity should have an identifier after you call persist(o), no matter where as long as you have a sequence-style generator.


                            So Hibernate actually calls the mutator after persisting (or implicitly refreshes)?

                            • 11. Re: Getting Id of Presisted object.
                              christian.bauer

                              Hibernate sets the identifier property, through field or setter access.

                              • 12. Re: Getting Id of Presisted object.
                                christian.bauer

                                (You don't need an INSERT to get the next identifier value from a sequence.)

                                • 13. Re: Getting Id of Presisted object.
                                  rhinosystemsinc

                                  Sorry. I know this is slightly off topic of your question - please forgive me. but I am still having the same problem whereby, it says the sequence does not exists... I am using jboss-4.0.5.GA w/EJB installed on Window XP runing 10g XE on same machine.

                                  This one issue is enough to kill my project in SEAM, and I really wanted to give this a try. I need to be able to use SEQUENCE directly or before insert triggers (which invoke the sequence before actual insert). I don't care which way I go, but if I use the method proposed here, I get:

                                  --- MBeans waiting for other MBeans ---
                                  ObjectName: persistence.units:ear=naturalmed2.ear,unitName=naturalmed2
                                   State: FAILED
                                   Reason: javax.persistence.PersistenceException: org.hibernate.HibernateException: Missing sequence or table: NM.LISTINGTYPE_SEQ
                                   I Depend On:
                                   jboss.jca:service=DataSourceBinding,name=naturalmed2Datasource
                                  
                                  --- MBEANS THAT ARE THE ROOT CAUSE OF THE PROBLEM ---
                                  ObjectName: persistence.units:ear=naturalmed2.ear,unitName=naturalmed2
                                   State: FAILED
                                   Reason: javax.persistence.PersistenceException: org.hibernate.HibernateException: Missing sequence or table: NM.LISTINGTYPE_SEQ
                                   I Depend On:
                                   jboss.jca:service=DataSourceBinding,name=naturalmed2Datasource


                                  and again, the sequence is there (double checked).

                                  Also, I should mention that I am working with the code generated by SEAM generate-entities.

                                  Thank you for any help you can give.

                                  • 14. Re: Getting Id of Presisted object.
                                    damianharvey

                                    Is your ID field in your Entity annotated with @GeneratedValue ?

                                    This caused me all sorts of grief with my Seam Gen'd project until I figured out it's significance.

                                    1 2 Previous Next