views:

53

answers:

3

Hi everybody,

I working with Eclipse Helios, m2eclipse Maven plugin and Glassfish plugin. I edited the "pom.xml" file so that I can get the DerbyClient and the JPA Persistence classes.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.apress.javaee6</groupId>
  <artifactId>chapter02</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>chapter02</name>
  <url>http://maven.apache.org&lt;/url&gt;

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derbyclient</artifactId>
        <version>10.6.1.0</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.0.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.0.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.4.3</version>
        <type>maven-plugin</type>
    </dependency>
  </dependencies>


  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
  </build>
  <repositories>
    <repository>
      <id>EclipseLink Repo</id>
      <url>http://www.eclipse.org/downloads/download.php?r=1&amp;amp;nf=1&amp;amp;file=/rt/eclipselink/maven.repo&lt;/url&gt;
    </repository>    
  </repositories>

</project>

Then, I created the "src/main/resrouce" directory and put a "persistence.xml" file that contains:

<xml version="1.0" encoding="UTF-8">
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
        <persistence-unit name="chapter02PU" transaction-type="RESOURCE_LOCAL">
            <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
            <class>com.apress.javaee6.chapter02.Book</class>
            <properties>
                <property name="eclipselink.target-datababase" value="DERBY"/>
                <property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
                <property name="eclipselink.jdbc.url" value="jdbc:derby://localhost:1527/chapter02DB;create=true"/>
                <property name="eclipselink.jdbc.user" value="APP"/>
                <property name="eclipselink.jdbc.password" value="APP"/>
                <property name="eclipselink.jdbc.ddl-generation" value="create-tables"/>
                <property name="eclipselink.jdbc.logging-level" value="INFO"/>
            </properties>
        </persistence-unit> 
    </persistence>
</xml>

Of course, the derby server runs perfectly.

And I have create the following Book Class:

package com.apress.javaee6.chapter02; 

import javax.persistence.*;
@Entity
@NamedQuery(name="findAllBooks", query="SELECT b from Book b") 
public class Book {
    @Id @GeneratedValue
    private Long id;
    @Column(nullable = false)
    private String  title;
    private float   price;
    @Column(length = 1000)
    private String  description;
    private String  isbn;
    private Integer nbOfPage;
    private Boolean illustrations;

    public Book() {
        super();
    }
    public Book(Long id, String title, float price, String description,
            String isbn, Integer nbOfPage, Boolean illustrations) {
        super();
        this.id = id;
        this.title = title;
        this.price = price;
        this.description = description;
        this.isbn = isbn;
        this.nbOfPage = nbOfPage;
        this.illustrations = illustrations;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getIsbn() {
        return isbn;
    }
    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }
    public Integer getNbOfPage() {
        return nbOfPage;
    }
    public void setNbOfPage(Integer nbOfPage) {
        this.nbOfPage = nbOfPage;
    }
    public Boolean getIllustrations() {
        return illustrations;
    }
    public void setIllustrations(Boolean illustrations) {
        this.illustrations = illustrations;
    }


}

The main function is here:

package com.apress.javaee6.chapter02;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // Create an instance of book
        Book book = new Book();
        book.setTitle("The Hitchhiker's guide to the Galaxy");
        book.setPrice(12.5F);
        book.setDescription("Science fiction comedy book");
        book.setIsbn("1-84023-742-2");
        book.setIllustrations(false);

        // Gets an entity manager and a transaction
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("chapter02PU");
        EntityManager em = emf.createEntityManager();

        // Persists the book to the database
        EntityTransaction tx = em.getTransaction();

        try {
            tx.begin();
            em.persist(book);
            tx.commit();

            em.close();
            emf.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

The problem is that the "persistence.xml" is not recognized. How can I make Maven point to the "persistence.xml" file?

When I active the Maven console and "Update Dependencies", I got the following (bad) info:

28/06/10 15:20:36 CEST: [INFO] Using 'UTF-8' encoding to copy filtered resources.
28/06/10 15:20:36 CEST: [INFO] skip non existing resourceDirectory /home/zakaria/workspace/chapter02/src/main/resources
28/06/10 15:20:36 CEST: [INFO] Using 'UTF-8' encoding to copy filtered resources.
28/06/10 15:20:36 CEST: [INFO] skip non existing resourceDirectory /home/zakaria/workspace/chapter02/src/test/resources

So i think he tried somewhere to create the "resources" directories and cannot get them after the build.

Thank you so much ;) Regards

A: 

If you're using m2eclipse 0.10.0, not having the resources recognized is a known problem (see m2eclipse user list). There is a workaround described here: https://docs.sonatype.org/pages/viewpage.action?pageId=2949459

Using maven-resources-plugin version 2.4.2 or 2.4.3 may also work better than 2.4.

Bruno
+1  A: 

You don't tell maven to "point" on a file, you put files in a directory that end up on the class path. For resources, you're supposed to use src/main/resources.

In other words, just put the persistence.xml in src/main/resources/META-INF.

I'm using m2eclipse 0.10.0 and I don't use any special workaround, it just works for at least 3 projects in my workspace of type jar, ejb and war.

Pascal Thivent
Thanks Pascal for the third time in 24 hrs loolYou can see here: http://yfrog.com/f/j3projectgp/ that the "src/main/resources" is created and the "persistence.xml" is there.I don't know what's goind wrong? Thanks again.
Zakaria
@Zakaria: Indeed, I can see that (this wasn't obvious in the question). Can you confirm that the `persistence.xml` doesn't get copied to `target/classes/META-INF` when building in Eclipse? Is the behavior different when building on the command line? If yes, then maybe you're facing the problem described by @Bruno. I can't say much about it, I'm not experiencing this problem myself.
Pascal Thivent
@Pascal: Yes. I confirm that the "resources" folder and, by the way, the "resources/META-INFO" directory and the "resources/META-INF/persistence.xml" aren't created.I added in the post some pieces of "INFO" printed in the Maven Console.For the command line building method, I'll try it later this evening when I won't find a solution because my goal is to have a "compact" development environment. I've already tried with Netbeans and It works out-of-box.Thanks again;)
Zakaria
A: 

According to JPA standard persistence.xml should reside in the root of META-INF folder. So putting in into src/main/resources/META-INF should work

eugener