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
- Transaction is required to perform this operation (either use a transaction or extended persistence context)
- WFLYJPA0060: Transaction is required to perform this operation (either use a transaction or extended persistence context)
- Object is not getting persisted using JPA/JTA/JBOSS/CDI
- CDI Transaction Management: How does @Transactional work?