Question

I'm new to jpa and jsf. I'm trying to develop simple web app using jpa and jsf. I have installed mysql and have configured Glassfish to work with it. I have created small project, that consists of one Managed Bean and one JPA Entity. While deployment the table is successfully created in database, so connection to database is ok. But I have one issue, I cant't persist any entity in Managed Bean method:

@ManagedBean(name = "data")
@SessionScoped
public class Data implements Serializable {

    @PersistenceUnit(unitName = "JChatPU")
    EntityManagerFactory emf;

    @Resource
    UserTransaction utx;

    public Data() {
    }

    public void add() {
        EntityManager em = emf.createEntityManager();
        try {
            utx.begin();
            JChatUser u = new JChatUser();
            em.persist(u);
            utx.commit();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}

My persistence unit configuration in persistence.xml:

  <persistence-unit name="JChatPU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/jchatdb</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    </properties>
  </persistence-unit>

But I can successfully persist from EJB. So, how to persist from managed bean's method?

Thank's for the future answers.

Was it helpful?

Solution

I have solved the issue!

The problem was in the way I have called bean method:

<h:commandButton id="add" value="add" action="#{data.add()}" />

It doesn't work, because we need to plase command button in the form:

<h:form>
    <h:commandButton id="add" action="#{data.meth()}" value="add"/>
</h:form>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top