3

私は Eclipse Helios、m2eclipse Maven プラグイン、Glassfish プラグインを使用しています。「pom.xml」ファイルを編集して、DerbyClient および JPA Persistence クラスを取得できるようにしました。

<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">
  <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</url>

  <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;nf=1&amp;file=/rt/eclipselink/maven.repo</url>
    </repository>    
  </repositories>

</project>

次に、「src/main/resrouce」ディレクトリを作成し、以下を含む「persistence.xml」ファイルを配置しました。

<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>

もちろん、ダービー サーバーは完全に動作します。

そして、次の Book クラスを作成しました。

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;
    }


}

主な機能は次のとおりです。

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();
        }
    }

}

問題は、「persistence.xml」が認識されないことです。Maven が「persistence.xml」ファイルを指すようにするにはどうすればよいですか?

Maven コンソールと「依存関係の更新」をアクティブにすると、次の (悪い) 情報が表示されました。

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

それで、彼はどこかで「リソース」ディレクトリを作成しようとしたと思いますが、ビルド後にそれらを取得できません。

どうもありがとう;)よろしく

4

3 に答える 3

4

ファイルを「指す」ようにmavenに指示するのではなく、ファイルをディレクトリに配置し、最終的にクラスパスに配置します。リソースについては、 を使用することになっていますsrc/main/resources

つまり、 を入れるだけpersistence.xmlですsrc/main/resources/META-INF

私は m2eclipse 0.10.0 を使用していますが、特別な回避策は使用していません。タイプ jar、ejb、および war のワークスペース内の少なくとも 3 つのプロジェクトで機能します。

于 2010-06-27T14:18:41.823 に答える
0

m2eclipse 0.10.0 を使用している場合、リソースが認識されないという既知の問題があります (m2eclipse ユーザー リストを参照)。ここに説明されている回避策があります: https://docs.sonatype.org/pages/viewpage.action?pageId=2949459

maven-resources-plugin バージョン 2.4.2 または 2.4.3 を使用すると、2.4 よりもうまく機能する場合があります。

于 2010-06-27T14:05:34.383 に答える
0

JPA 標準によると、persistence.xml は META-INF フォルダーのルートに存在する必要があります。したがって、に入れるとうまくいくsrc/main/resources/META-INFはずです

于 2010-06-27T14:19:30.767 に答える