0

I have a JSF application (Java 8, Wildfly 10, MySQL DB, CDI beans, use of JPA/Hibernate). Most of similar questions are related to Spring framework or doesn't work in my case. Have no clue, which statements and configurations belonging to Spring, so I have problems to seperate other possible solutions.

In general my JPA entities work fine, because as long as I could use em.getTransaction.begin() and em.getTransaction.commit(), everything work fine. Here is my persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    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_2_0.xsd">
    <persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
        <jta-data-source>java:jboss/datasources/Hanse</jta-data-source>
        <class>model.Commodity</class>
        <class>model....</class>
        <class>model....</class>
        <properties>
            <!-- <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/> -->
            <!-- <property name="hibernate.hbm2ddl.auto" value="update" /> -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
            <!-- <property name="hibernate.show_sql" value="true" /> -->
            <property name="hibernate.enable_lazy_load_no_trans" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

But my entities are not persisted, I get no error or stacktrace. For example:

@Named
@ViewScoped
public class TestController implements Serializable {
    private static final long serialVersionUID = -4602041684807072422L;

    private String buildDate;
    private Ship ship;

    @PersistenceContext(unitName = "PU", type = PersistenceContextType.EXTENDED)
    private EntityManager em;
    @Resource
    private UserTransaction utx;


    public TestController() {

    }

    @Transactional
    public String doPersistTest() {
        this.ship.setRum(this.ship.getRum()+10);


//      this.em.getTransaction().begin();
        this.em.merge(this.ship);
//      this.em.getTransaction().commit();

        return "";
    }

    @PostConstruct
    public void init() {
        this.ship = (Ship) em.createNamedQuery("Ship.findByID").setParameter("id", 3).getSingleResult();
    }

}

The db entry does not change. I also tried it with

public String doPersistTest() {
    this.ship.setRum(this.ship.getRum()+10);


    this.em.getTransaction().begin();
    this.em.merge(this.ship);
    this.em.getTransaction().commit();

    return "";
}

and got the error WFLYJPA0060: Transaction is required to perform this operation (either use a transaction or extended persistence context) (what I don't unterstand, there IS an transaction and I us the extended persistence context).

If I try

public String doPersistTest() {
    this.ship.setRum(this.ship.getRum()+10);

    try {
        this.utx.begin();
        this.em.merge(this.ship);
        this.utx.commit();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    return "";
}

also nothing happens. The Entity is not persisted to the database. I don't get it, what is my mistake? Is my JPA "building" ok. If not, can someone push me to the right direction, please?

I also read

Community
  • 1
  • 1
xentity
  • 129
  • 1
  • 2
  • 10
  • 1
    Don't set your transaction-type to RESOURCE_LOCAL. You want your app server to manage the transactions, and JPA to use the app server transactions, so it should be set to JTA. – JB Nizet Aug 16 '16 at 07:21
  • @JBNizet OK, this seems to work... but how can I create an EntityManager in cases, where access via PersistenceContext is not possible, for example in "stupid" (no EJB or CDI) java classes? Get _Unable to access TransactionManager or UserTransaction to make physical transaction delegate_ from there. That's why I used resource_local... – xentity Aug 16 '16 at 10:20
  • In that case, you would use RESOURCE_LOCAL, and manage transactions by yourself, using the JPA API. – JB Nizet Aug 16 '16 at 11:58
  • @JBNizet Thanks for your reply. I have no problem to manage it by myself, but just to be clear: to you mean JPA API or JTA API? Because I thought I use the JPA API already with the EntityManager. If you mean JTA, does http://kishantha.blogspot.de/2009/06/mixing-transactions-manual-and.html point to the right direction? – xentity Aug 16 '16 at 20:26
  • If you're in an EJB container, the container comes with JTA, and you can handle JTA transactions declaratively (with session beans, or `@Transactional`). The transaction-type must be set to JTA. If you're in a simple Java SE application, you don't have JTA, so you must set the transaction-type to RESOURCE_LOCAL, and use the JPA API (EntityManager.getTransaction(), EntityTransaction.commit(), EntityTransaction.rollback()) to handle the transactions programmatically, by yourself. – JB Nizet Aug 16 '16 at 20:50

0 Answers0