8 Replies Latest reply on May 19, 2011 6:51 AM by Thomas Andraschko

    Problems with Faces + Persistence... Example available?

    Thomas Andraschko Newbie

      Hi,


      i tried to get persistence and faces module running on jetty but i had some problems.
      The faces module runs without problems but if i add the persistence module and annotate my bean method with @Transactional, seam already says no transaction open or similar.
      I using Hibernate 3.6.x with hsql and without JTA.
      I added the interceptor, t:SeSynchronizations and t:EntityTransaction to my beans.xml.


      Can anyone post/create a example with the persistence and faces module without EJB's and with Tomcat/Jetty?


      Thanks and best regards,
      Thomas

        • 1. Re: Problems with Faces + Persistence... Example available?
          elfuhrer Novice

          Can you post your code? I have managed already to run a JPA 2 persistence example within tomcat using Seam 3

          • 3. Re: Problems with Faces + Persistence... Example available?
            Thomas Andraschko Newbie

            I uploaded the example here:


            http://www.megaupload.com/?d=8173QR4O




            You can simple call mvn package jetty:run und open localhost:8080/my-webapp/index.xhtml


            Thanks for your help

            • 4. Re: Problems with Faces + Persistence... Example available?
              MIklos Piklos Newbie

              First of all you got this in beans.xml




              <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      xmlns:s="urn:java:ee" xmlns:t="urn:java:org.jboss.seam.transaction"
                      xsi:schemaLocation="
                    http://java.sun.com/xml/ns/javaee
                    http://docs.jboss.org/cdi/beans_1_0.xsd">
                      <interceptors>
                              <class>org.jboss.seam.transaction.TransactionInterceptor</class>
                      </interceptors>
                      <t:SeSynchronizations>
                              <s:modifies />
                      </t:SeSynchronizations>
                      <t:EntityTransaction>
                              <s:modifies />
                      </t:EntityTransaction>
              </beans>



              That is wrong, you should be having:




              <?xml version="1.0" encoding="UTF-8"?>
              <beans xmlns="http://java.sun.com/xml/ns/javaee"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="
                    http://java.sun.com/xml/ns/javaee 
                    http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
                    
                    <interceptors>
              
                    <class>org.jboss.seam.transaction.TransactionInterceptor</class>
              
                 </interceptors>
              </beans>


              In your beans.xml and you should have a file called seam-beans.xml that has this:



              <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      xmlns:s="urn:java:ee" xmlns:t="urn:java:org.jboss.seam.transaction"
                      xsi:schemaLocation="
              http://java.sun.com/xml/ns/javaee 
              http://docs.jboss.org/cdi/beans_1_0.xsd">
                      <t:SeSynchronizations>
                              <s:modifies />
                      </t:SeSynchronizations>
                      <t:EntityTransaction>
                              <s:modifies />
                      </t:EntityTransaction>
              </beans>



              Then in the class UserDaoImpl you have this:




              @Singleton
              @SuppressWarnings("serial")
              public class UserDaoImpl implements UserDao, Serializable {
              
                      @PersistenceContext
                      @Inject
                      private EntityManager entityManager;
              




              You dont need @PersistenceContext it won't even work on tomcat. So remove it and use only @Inject. Also you shouldn't use @Singleton for the bean scope, use @ApplicationScoped. After those couple of things i think you should be fine, if not writte here again.


              Good luck.

              • 5. Re: Problems with Faces + Persistence... Example available?
                MIklos Piklos Newbie

                And yeah you need this in your pom.xml



                              <dependency>
                               <groupId>org.jboss.seam.persistence</groupId>
                               <artifactId>seam-persistence</artifactId>
                               <version>3.0.0.Final</version>
                          </dependency>
                          <dependency>
                               <groupId>org.jboss.seam.config</groupId>
                               <artifactId>seam-config-xml</artifactId>
                               <version>3.0.0.Final</version>
                               <scope>runtime</scope>
                          </dependency>
                                <dependency>
                               <groupId>org.jboss.seam.solder</groupId>
                               <artifactId>seam-solder</artifactId>
                               <version>3.0.0.Final</version>
                          </dependency>



                • 6. Re: Problems with Faces + Persistence... Example available?
                  Thomas Andraschko Newbie

                  Great, it works :D Thanks!
                  I also tried it with seam-beans.xml but apparently i forgot to add the seam-config-xml module :/


                  But there is another problem:
                  If i add a user the first time, it works without problems but if i add another user it does not work anymore.
                  Is this a problem of the persistence module or is it really hibernate? (I also tried Hibernate 3.5.1)


                  Caused by: java.sql.SQLException: Violation of unique constraint $$: duplicate value(s) for column(s) $$: 
                  SYS_CT_46 in statement [insert into Users (id, email, loginName, password, status) values (null, ?, ?, ?, ?)]
                          at org.hsqldb.jdbc.Util.throwError(Unknown Source)
                          at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
                          at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:57)
                          ... 91 more


                  @Id
                  @GeneratedValue(strategy = GenerationType.AUTO)
                  @Column(name = "id", nullable = false)
                  private Long id;



                  p:commandButton actionListener="#{testController.newUser}"



                  @Transactional
                  public void newUser() {
                          User user = new User();
                          user.setPassword("blublublubl");
                          user.setEmail("asdas@asdas.com");
                          user.setLoginName("testLogin");
                          user.setStatus(1);
                                  
                          userDao.persist(user);
                  }



                  Any ideas?

                  • 7. Re: Problems with Faces + Persistence... Example available?
                    MIklos Piklos Newbie

                                  


                    <dependency>
                                   <groupId>org.hibernate</groupId>
                                   <artifactId>hibernate-entitymanager</artifactId>
                                   <version>3.6.0.Final</version>
                                   <scope>compile</scope>
                              </dependency>
                                    <dependency>
                                   <groupId>org.hibernate.javax.persistence</groupId>
                                   <artifactId>hibernate-jpa-2.0-api</artifactId>
                                   <version>1.0.0.Final</version>
                                   <scope>compile</scope>
                              </dependency>
                                    <dependency>
                                   <groupId>org.hibernate</groupId>
                                   <artifactId>hibernate-validator</artifactId>
                                   <version>4.0.0.GA</version>
                              </dependency>


                    I use this combination and it works.
                    Try it.

                    • 8. Re: Problems with Faces + Persistence... Example available?
                      Thomas Andraschko Newbie

                      Argh!
                      it was my mistake - loginname is declared as unique!


                      Sorry and thanks for your help :)