0
votes

hello i'm learning java ee and maven here is my code The main file:

public class Main {
    public static void main(String[] args) {
        Book livre = new Book();
        livre.setId(new BigDecimal("1"));
        livre.setDescription(" la chanson dans la foret  avec coldplay ");
        livre.setIsbn("12.5.8");
        livre.setNbofpage(new BigInteger("2354"));
        livre.setTitle("adventure of a lifetime");
        livre.setPrice(new BigDecimal("267"));
        //creation de l'objet
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("BookStorePU"); 
        EntityManager em = emf.createEntityManager();
        EntityTransaction utx = em.getTransaction();
        utx.begin();
        em.persist(livre);
        utx.commit();
        TypedQuery<Book> crna = em.createNamedQuery("Book.findAll", Book.class);
        List<Book> livres = crna.getResultList();
        System.out.println(livres.toString());
    }
}

the pom xml : http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.espoirmur</groupId>
<artifactId>BookStoreApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>BookStoreApp</name>

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.2</version>
        <exclusions>
            <exclusion>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>commonj.sdo</artifactId>
            </exclusion>
        </exclusions>
           <scope>provided</scope>

    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
        <version>2.5.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc7</artifactId>
        <version>12.1.0.1</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>7.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

here is the project structure: netbeans structure

project file directory

after clean and build and run main file i get :

Exception in thread "main" javax.persistence.PersistenceException: No resource files named META-INF/services/javax.persistence.spi.PersistenceProvider were found. Please make sure that the persistence provider jar file is in your classpathat javax.persistence.Persistence.findAllProviders(Persistence.java:167) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:103) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83) at com.espoirmur.Entity.Main.main(Main.java:30)

persistence .xml file :

<persistence-unit name="BookStorePU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
        <property name="javax.persistence.jdbc.driver value="oracle.jdbc.OracleDriver" />
        <property name="javax.persistence.jdbc.url" 
            value="jdbc:oracle:thin:@localhost:1521:XE" />
        <property name="javax.persistence.jdbc.user" value="espoir" />
        <property name="javax.persistence.jdbc.password" value="9874" />
    </properties>
</persistence-unit>

the persistene xml is in that directory :

C:\Users\Espoir M\Google Drive\BookStoreApp\src\main\resources\META-INF

please help me to solve that issue

2

2 Answers

1
votes

Dear Espoir JTA can not be used into main java class only when your .war or .ear project is running into JEE contenait as Glassfish. To run your code please create a simple java project and try to use RESOURCE_LOCAL as transaction type.

0
votes

according to Celestin answer i change the first line of my persistance xml like this:

 <persistence-unit name="BookStorePU" transaction-type="RESOURCE_LOCAL">