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

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?

Ok, here’s the solution… I mean after these changes my code worked:

1.) The persistence.xml (the two added rows are marked with “NEW LINE” comment):

<?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>
        <jta-data-source>SourcePU</jta-data-source> <!-- NEW LINE -->
        <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.target-database" value="Oracle" /> <!-- NEW LINE -->
        </properties>
    </persistence-unit>
</persistence>

2.) The “separated” generator in BaseModel and Classtypes entity doesn’t work. :frowning:
So Classtypes entity looks like this:

[...]

@Entity
@Table(name = "CLASSTYPES")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Classtypes.findAll", query = "SELECT c FROM Classtypes c"),
    [...]

    @NamedQuery(name = "Classtypes.findByVersion", query = "SELECT c FROM Classtypes c WHERE c.version = :version")})
public class Classtypes implements Serializable {

    private static final long serialVersionUID = 20160325444673L;

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "S_CLASSTYPES")
    @SequenceGenerator(name="S_CLASSTYPES", sequenceName="S_CLASSTYPES", allocationSize=1)
    private Long id;
    @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;
    @Basic(optional = false)
    @NotNull
    @Column(name = "CREATED")
    @Temporal(TemporalType.TIMESTAMP)
    private Date created;
    @Basic(optional = false)
    @NotNull
    @Column(name = "CREATOR")
    private long creator;
[...]

3.) The persist code works so:

Classtypes classtypeItem = new Classtypes();
classtypeItem.setName("New item 123");  //and all mandatory fileds are filled.
EntityManager em = JPAContainerFactory.createEntityManagerForPersistenceUnit("SourcePU");
em.getTransaction().begin();
em.persist(classtypeItem);
em.getTransaction().commit();
em.close();
refreshValues();  //data refresh. rebuild container.

If you have any notice, opinion or idea, please don’t hesitate, just post here! :slight_smile: Thx.