Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

JpaContainer - addEntity -> Cannot use an EntityTransaction while using JTA

Peter Varsanyi
6 years ago Mar 25, 2016 12:30pm

Hello,
I'm new in vaadin. I use NetBeans 8.1 (with Vaadin add-on), Oracle XE 11g and Tomee (plume) 1.7.4. I have created a new vaadin web application project with some addons: lazycontainer, viritin, jpacontainer. Here's the deps from the pom.xml:

[size=2][font=courier new][...]
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-server</artifactId>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-push</artifactId>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-client</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-themes</artifactId>
        </dependency>
        <dependency>
            <groupId>com.vaadin.addon</groupId>
            <artifactId>jpacontainer</artifactId>
            <version>3.2.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.vaadin</groupId>
            <artifactId>viritin</artifactId>
            <version>1.47</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.vaadin.addons.lazyquerycontainer</groupId>
            <artifactId>vaadin-lazyquerycontainer</artifactId>
            <version>7.6.1.3</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.1.1</version>
        </dependency>
        <!--
        Oracle JDBC
        mvn install:install-file -Dfile=D:\Toolz\apache-tomee-plume-1.7.2\lib\ojdbc7.jar -DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=11.2.0 -Dpackaging=jar
        -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>11.2.0</version>
        </dependency>
    </dependencies>
[...][/font][/size]

I use only one entity In my example project. There is a MappedSuperclass for common fileds...

[...]
@MappedSuperclass
public class BaseModel implements Serializable {

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "DefaultSequence")
    private Long id;
    @Basic(optional = false)
    @NotNull
    @Column(name = "CREATED")
    @Temporal(TemporalType.TIMESTAMP)
    private Date created;
[...]

... and here's the entity:

[...]
@Entity
@Table(name = "CLASSTYPES")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Classtypes.findAll", query = "SELECT c FROM Classtypes c"),
    [...]
@SequenceGenerator(name="S_CLASSTYPES", sequenceName="S_CLASSTYPES", allocationSize=1)
public class Classtypes extends BaseModel implements Serializable {

    private static final long serialVersionUID = 1L;

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 250)
    @Column(name = "NAME")
    private String name;
    @Lob
    @Column(name = "DESCRIPTION")
    private String description;
    @OneToMany(mappedBy = "classtypeId")
    private List<Classes> classesList;

    public Classtypes() {
    }
[...]

I have added a persistence.xml to the project, then I have modified by hand. Here's the result:

<?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="SourcePU" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.jdbc.platform"
                      value="org.eclipse.persistence.platform.database.OraclePlatform" />
            <property name="eclipselink.jdbc.driver" value="oracle.jdbc.OracleDriver" />
            <property name="eclipselink.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:XE" />
            <property name="eclipselink.jdbc.user" value="sandbox" />
            <property name="eclipselink.jdbc.password" value="sandbox" />
            <property name="eclipselink.logging.level" value="FINE" />
            <property name="eclipselink.ddl-generation" value="none" />
            <property name="eclipselink.ddl-generation.output-mode"
                                  value="database" />
        </properties>
    </persistence-unit>
</persistence>

Ok. Let's try to select:

JPAContainer<Classtypes> container = JPAContainerFactory.make(Classtypes.class, "SourcePU");

It works perfectly. The result is all (three) "records". Good.
Then let's try to insert:

Classtypes cc = new Classtypes();
cc.setName("xxx"); //...and all mandatory fileds are filled.
container.addEntity(cc);  

The result is an exception:
java.lang.IllegalStateException:
Exception Description: Cannot use an EntityTransaction while using JTA.
    at org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper.getTransaction(JTATransactionWrapper.java:73)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.getTransaction(EntityManagerImpl.java:1322)
    at com.vaadin.addon.jpacontainer.provider.MutableLocalEntityProvider.runInTransaction(MutableLocalEntityProvider.java:111)
    at com.vaadin.addon.jpacontainer.provider.MutableLocalEntityProvider.addEntity(MutableLocalEntityProvider.java:137)
    at com.vaadin.addon.jpacontainer.provider.CachingMutableLocalEntityProvider.addEntity(CachingMutableLocalEntityProvider.java:164)
    at com.vaadin.addon.jpacontainer.JPAContainer.addEntity(JPAContainer.java:1113)
[...]

Ahhh...what did I miss?

 

Peter Varsanyi
6 years ago Mar 29, 2016 8:05am