8 Replies Latest reply on Jan 21, 2013 9:23 AM by nathandennis

    Seam3 persistence example/forge

    tomlux

      Hy,

      I'm currently trying to get Seam3 persistence running.

      I'm always getting "object is detached".

      Does anybody have a small working example which he could share?

       

      I just want to build a simple CRUD application which can edit 2 EntityBean where the second bean is in a One2Many relation to the first one.

      The application should run on a JBoss7.1.1 application server.

       

      I'm also seeing that the  Seam3 perssistence is removed from the Forge documentation page.

      What's the direction of Seam3 persistence? Is it already deprecated?

       

      Thanks,

      Tom

        • 1. Re: Seam3 persistence example/forge
          nathandennis

          easiest way to get going is to build your db and use jboss forge on it. generate the crud structure and you are almost back to the same place you were with seam gen back in the good ol days.

           

          the direction of seam 3 persistence and all seam in general is deltaspike. all of the current development is being done there. however it isnt quite ready for prime time yet.

          • 2. Re: Seam3 persistence example/forge
            nathandennis

            i guess that wasnt really a complete answer was it... use forge. build the project... then check and make sure you maven dependencies are there

             

             

             

             {code:xml}
            <properties>
                    <seam.persistence.version>RELEASE</seam.persistence.version>
              </properties>
            <dependency>
                    <groupId>org.jboss.seam</groupId>
                    <artifactId>seam-bom</artifactId>
                    <version>3.1.0.Final</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency> 
              <dependency>
                    <groupId>org.jboss.seam.persistence</groupId>
                    <artifactId>seam-persistence</artifactId>
              </dependency>
            
            {code}
            
            

             

            
            
            
            
            
            create a annotation injection point
            
            package org;
            
            
            
            import java.lang.annotation.ElementType; 
            
            import java.lang.annotation.Retention; 
            
            import java.lang.annotation.RetentionPolicy; 
            
            import java.lang.annotation.Target; 
            
            
            
            import javax.inject.Qualifier; 
            
            
            
            @Qualifier 
            
            @Target({ ElementType.METHOD,ElementType.FIELD, ElementType.PARAMETER,ElementType.TYPE  }) 
            
            @Retention(RetentionPolicy.RUNTIME) 
            
            public @interface SomeIdentifier { 
            
                /* class body intentionally left blank */ 
            
            }
            
            
            
            create an entityManager Producer
            
            package org;
            
            
            
            import javax.enterprise.context.ConversationScoped;
            
            import javax.enterprise.inject.Produces;
            
            import javax.persistence.EntityManager;
            
            import javax.persistence.EntityManagerFactory;
            
            import javax.persistence.PersistenceContext;
            
            import javax.persistence.PersistenceUnit;
            
            
            
            import org.jboss.solder.core.ExtensionManaged;
            
            
            
            
            
            /**
            
             * 
            
             * @author Nathan Dennis
            
             * 
            
             */
            
            public class EntityManagerProducer {
            
            
            
            
            
                @ExtensionManaged
            
                @Produces
            
                @PersistenceUnit(unitName="somePU")
            
                EntityManagerFactory hemf;
            
            
            
                @Produces
            
                @SomeIdentifier
            
                @PersistenceContext(unitName = "somePU")
            
                private EntityManager hem;
            
            
            
            }
            
            
            
            //Now inject your enitityManager where ever you want with 
            @Inject private EntityManager hem;
            

             

             

             

             


             



            • 3. Re: Seam3 persistence example/forge
              tomlux

              Hy,

              thanks for your code.

              I followed your instrucations and have a working example:

               

              When I'm working with the entitymanager in 2 different bean, I'm always getting a "detached object" exception. What am I making wrong?

              It's configured to use the Jboss7 ExampleDS datasource.

              I created to 2 beans: CityBean and PersonBean

               

              I attached the example app:

                 http://localhost:8080/example/home.jsf

               

               

              When I'm invoking the methods on citybean, everything is running fine.

              When I'm calling the same method in the personbean, I'm getting a "detached object" exception.

               

              Shoud the entitymanager be 'conversationscoped'?

               

              Thanks,

              Tom

              • 4. Re: Seam3 persistence example/forge
                nathandennis

                youre passing the entity between beans. the original object was within the list of objects in cityBean.

                 

                simple solution is reattach it if you want to use it within personBean. entityManager.find(Person.class, id)

                 

                the other thing i saw you were going to run into is an optimistic lock because the object is being held by another trasnaction. it will throw a stale state exception if left the way it is.

                 

                detached entities are just part of the whole beast you have to deal with. if you arent carrying edits in the detached just reattach and go on.. else you will have to move the data into a reattached object to persist it.

                • 5. Re: Seam3 persistence example/forge
                  tomlux

                  Hy,

                  thanks for your replies.

                   

                  To attach the objects, I modified the PersonBean to this:

                      public void createNewPerson(City city) {

                          city = hem.find(City.class,city.getId());

                          Person p = new Person();

                          p.setFirstName("newPerson-" + new Date());

                          p.setCity(city);

                          hem.persist(p);

                          city.getPersons().add(p);

                      }

                   

                   

                      public void editPerson(Person person) {

                          person = hem.find(Person.class,person.getId());

                          person.setFirstName(person.getFirstName() + "-X");

                          person = hem.merge(person);

                      }

                   

                   

                  I'm now not getting a "detached object" extension, but after calling the "personBean.createPerson", my list from #{cityBean.cities} isn't updated. Calling the "cityBean.createPerson" the list is updated correctly

                  Did you have an explication/solution?

                   

                  Should I always reattach the objects as above in any methods where I'm not sure if objects are attached or not?

                  Is this operation costy or are there no problems to call it everytime?

                  Could I optimize it like this:

                          if (!hem.contains(city))

                              city = hem.find(City.class, city.getId());

                   

                   

                  Thanks,

                    Tom

                  • 6. Re: Seam3 persistence example/forge
                    nathandennis

                    without seeing the line that broke it .... im going to say by definition, a detached entity has already been persisted and is no longer controlled by the EntityManager. So if you have detached the entity and you want to do a db call like

                            city.getPersons(); //.add(p);

                    you should reattach it first.

                     

                    you only need to reattach entities if they are detached. of course reattaching would work every time (and i am guilty of such) but it does cost you in performance.

                    so you if youre concerned with performance you should probably check if the entity is in the session context before reattaching it.

                     

                    i think you can get to that under the entitymanager... entityManager.contains(city) or entityManager.contains(person)..

                    • 7. Re: Seam3 persistence example/forge
                      ffatheranderson

                      +1 i also want to see the pure example with configuration that allow to use EntityManager injection in Java SE. If some one can provide maven light weight example of it it will be superb.

                      • 8. Re: Seam3 persistence example/forge
                        nathandennis