4 Replies Latest reply on May 9, 2009 3:40 PM by Nicklas Karlsson

    entityManager.persist(user) do not work

    xiao tang Newbie
               @PersistenceContext
           private EntityManager entityManager;

      //     @In
      //     private FacesMessages facesMessages;

           @In
           StatusMessages statusMessages;

           private String verify;

           private boolean registered;

           public void register() {
                if (user.getPassword().equals(verify)) {
      //work
                     List existing = entityManager.createQuery("select u.username from User u where u.username=#{user.username}").getResultList();
                     if (existing.size() == 0) {
      do not work, and do not insert into database, but no errors return
                          entityManager.persist(user);
                          statusMessages.add("Successfully registered as #{user.username}");
                          registered = true;
                     } else {
                          statusMessages.addToControl("username", "Username #{user.username} already exists");
                     }
                } else {
                     statusMessages.addToControl("verify", "Re-enter your password");
                     verify = null;
                }
           }
        • 1. Re: entityManager.persist(user) do not work
          alex becker Newbie

          If you get no error, it seems your transactionManager is not correct configured and sends no commit to your database.
          Do you see the insert statement in your console-output?
          alex

          • 2. Re: entityManager.persist(user) do not work
            xiao tang Newbie
            first thanks my friend alex becker...
            1:this is the insert statement:insert into Customer(name, password, username) values(?, ?, ?)

            2:persistence.xml
            <persistence-unit name="bookingDatabase">
                  <provider>org.hibernate.ejb.HibernatePersistence</provider>
                  <jta-data-source>java:/DefaultDS</jta-data-source>
                  <properties>
                     <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
                     <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
                     <property name="hibernate.transaction.flush_before_completion" value="true"/>
                     <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
                     <property name="hibernate.show_sql" value="true"/>
                     <property name="hibernate.format_sql" value="true"/>        
                  </properties>
               </persistence-unit>

            3:components.xml
            <core:init debug="true" jndi-pattern="booking-ear/#{ejbName}/local" />

                    <core:manager concurrent-request-timeout="500" conversation-timeout="120000" conversation-id-parameter="cid" parent-conversation-id-parameter="pid" />

                    <security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true" />
                   
                    <web:hot-deploy-filter url-pattern="*.seam" />

                    <persistence:managed-persistence-context name="entityManager" auto-create="true" persistence-unit-jndi-name="java:/bookingEntityManagerFactory" />

                    <drools:rule-base name="securityRules">
                            <drools:rule-files>
                                    <value>/security.drl</value>
                            </drools:rule-files>
                    </drools:rule-base>

                    <security:rule-based-permission-resolver security-rules="#{securityRules}" />
                   
                    <event type="org.jboss.seam.security.notLoggedIn">
                            <action execute="#{redirect.captureCurrentView}" />
                    </event>
                    <event type="org.jboss.seam.security.loginSuccessful">
                            <action execute="#{redirect.returnToCapturedView}" />
                    </event>

                    <mail:mail-session host="localhost" port="2525" username="test" password="test" />

            4:RegisterAction.java
            @Stateful
            @Scope(EVENT)
            @Name("register")
            public class RegisterAction implements Register {
                   
                    @In
                    private User user;

                    @PersistenceContext
                    private EntityManager entityManager;

                    @In
                    StatusMessages statusMessages;

                    private String verify;

                    private boolean registered;

                    public void register() {
                            if (user.getPassword().equals(verify)) {
                                    List existing = entityManager.createQuery("select u.username from User u where u.username=#{user.username}").getResultList();
                                    if (existing.size() == 0) {
                                            entityManager.persist(user);
                                            statusMessages.add("Successfully registered as #{user.username}");
                                            registered = true;
                                    } else {
                                            statusMessages.addToControl("username", "Username #{user.username} already exists");
                                    }
                            } else {
                                    statusMessages.addToControl("verify", "Re-enter your password");
                                    verify = null;
                            }
                    }

                    public void invalid() {
                            statusMessages.add("Please try again");
                    }

                    public boolean isRegistered() {
                            return registered;
                    }

                    public String getVerify() {
                            return verify;
                    }

                    public void setVerify(String verify) {
                            this.verify = verify;
                    }

                    @Remove
                    public void destroy() {
                    }
            }


            please help me...
            I will be waitting for you. thanks
            • 3. Re: entityManager.persist(user) do not work
              xiao tang Newbie
              It is ok.
              1:persistence.xml
              <?xml version="1.0" encoding="UTF-8"?>
              <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
                   version="1.0">
                   <persistence-unit name="booking">
                        <provider>org.hibernate.ejb.HibernatePersistence</provider>
                        <jta-data-source>java:/bookingDatasource</jta-data-source>
                        <properties>
                             <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
                             <property name="hibernate.hbm2ddl.auto" value="none" />               
                             <property name="hibernate.show_sql" value="true" />
                             <property name="hibernate.format_sql" value="true" />
                             <property name="jboss.entity.manager.factory.jndi.name" value="java:/bookingEntityManagerFactory" />
                        </properties>
                   </persistence-unit>
              </persistence>

              2:components.xml
              <?xml version="1.0" encoding="UTF-8"?>
              <components xmlns="http://jboss.com/products/seam/components"
                          xmlns:core="http://jboss.com/products/seam/core"
                          xmlns:persistence="http://jboss.com/products/seam/persistence"
                          xmlns:drools="http://jboss.com/products/seam/drools"
                          xmlns:bpm="http://jboss.com/products/seam/bpm"
                          xmlns:security="http://jboss.com/products/seam/security"
                          xmlns:mail="http://jboss.com/products/seam/mail"
                          xmlns:web="http://jboss.com/products/seam/web"
                          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                          xsi:schemaLocation=
                              "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.1.xsd
                               http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.1.xsd
                               http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.1.xsd
                               http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.1.xsd
                               http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.1.xsd
                               http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.1.xsd
                               http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.1.xsd
                               http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.1.xsd">
                   <!--
                        note :)
                        @debug@ will be replace by "true" whick display in the file components.properties.
                   -->
                   <core:init debug="@debug@" jndi-pattern="@jndiPattern@" />

                   <core:manager concurrent-request-timeout="500" conversation-timeout="120000" conversation-id-parameter="cid" parent-conversation-id-parameter="pid" />

                   <web:hot-deploy-filter url-pattern="*.seam" />
                   <persistence:managed-persistence-context name="entityManager" auto-create="true" persistence-unit-jndi-name="java:/bookingEntityManagerFactory" />



                   <drools:rule-base name="securityRules">
                        <drools:rule-files>
                             <value>/security.drl</value>
                        </drools:rule-files>
                   </drools:rule-base>

                   <security:rule-based-permission-resolver security-rules="#{securityRules}" />
                   <security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true" />

                   <event type="org.jboss.seam.security.notLoggedIn">
                        <action execute="#{redirect.captureCurrentView}" />
                   </event>
                   
                   <event type="org.jboss.seam.security.loginSuccessful">
                        <action execute="#{redirect.returnToCapturedView}" />
                   </event>

                   <mail:mail-session host="localhost" port="2525" username="test" password="test" />
              </components>

              3:code in java
              @PersistenceContext
              private EntityManager entityManager;
              • 4. Re: entityManager.persist(user) do not work
                Nicklas Karlsson Master

                How do you verify the row has/hasn't been inserted? I seem you have create-drop so it is cleared between executions.