【问题标题】:generateSchema in JPAJPA 中的生成模式
【发布时间】:2018-12-06 22:27:06
【问题描述】:

我对 JPA 很陌生,这个项目不是我的。 我看到./test 目录中有一些测试类。当我尝试运行 mvn clean package 时,测试的构建确实失败了:我没有测试类使用的 mysql db。 我在测试包中找到了一个类CreateSchema,但如果我运行它,则不会创建任何数据库。我什至尝试手动创建数据库,在这种情况下,构建会打印有关缺少 mysql 表的错误。

这是课

package com.myproject.test;
import javax.persistence.Persistence;
import org.apache.log4j.Logger;
public class CreateSchema {
    public static void main(String[] args) {
        Persistence.generateSchema("MySchema", null);
    }
}

当我运行它时,它绝对不会打印任何东西。

我错过了什么?

这是我的pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>mygroup</groupId>
    <artifactId>myartid</artifactId>
    <version>2.1.28</version>
    <description>Jaxb libs dependencies managed and cleaned</description>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <testSourceDirectory>test</testSourceDirectory>
        <resources>
            <resource>
                <directory>src</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/META-INF</directory>
                <excludes>
                    <exclude>**/persistence.xml</exclude>
                </excludes>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>test</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </testResource>
        </testResources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>jar-with-dependencies</descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>build-jar-with-dependencies</id>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <phase>package</phase>
                    </execution>
                </executions>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <excludes>
                        <exclude>**/META-INF/persistence.xml</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <!-- explicitly define maven-deploy-plugin after other to force exec 
                    order -->
                <artifactId>maven-deploy-plugin</artifactId>
                <executions>
                    <execution>
                        <id>deploy</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>deploy</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>
        <!-- <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.2.0</version>
        </dependency> -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.mod4j.org.apache.commons</groupId>
            <artifactId>logging</artifactId>
            <version>1.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jettison</groupId>
            <artifactId>jettison</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.6.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.4.2.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.el</artifactId>
            <version>3.0.1-b10</version>
            <scope>test</scope>
        </dependency> 

    </dependencies>
</project>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    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_1.xsd">
    <persistence-unit name="MySchema" transaction-type="RESOURCE_LOCAL">

        <class>com.myproj.jpa.class1</class>
        <class>com.myproj.jpa.class1</class>
        <class>...</class>
        <!-- other classes-->

        <shared-cache-mode>NONE</shared-cache-mode>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpaexample" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="eclipselink.logging.level.sql" value="FINE" />
            <property name="eclipselink.logging.parameters" value="true" />
            <property name="eclipselink.jdbc.batch-writing" value="JDBC" />
            <property name="eclipselink.jdbc.batch-writing.size" value="10000" />
            <property name="eclipselink.connection-pool.default.min"
                value="5" />
            <property name="eclipselink.connection-pool.default.max"
                value="200" />
            <property name="eclipselink.canonicalmodel.subpackage"
                value="dev" />
            <property name="eclipselink.cache.shared.default" value="false" />
        </properties>

    </persistence-unit>

</persistence>

【问题讨论】:

  • 失踪?缺少发布persistence.xml,缺少发布您在persistence.xml 中指定的实体,缺少发布哪个JPA 提供者,缺少发布JPA 提供者日志中的内容(当您执行该操作时)
  • 我用 pom 和 persistence.xml 编辑了问题
  • 是的,但你不说会发生什么。如果有错误,则发布错误+堆栈跟踪。如果没有,请查看您选择的 JPA 提供者的日志

标签: java maven jpa


【解决方案1】:

我没有使用 maven 完成此操作,尽管我在 persistence.xml 上使用了几个属性来获取我的实体的 .sql 文件。

 <property name="eclipselink.ddl-generation" value="create-tables"/>
 <property name="eclipselink.ddl-generation.output-mode" value="sql-script"/>
 <property name="eclipselink.application-location" value="${TARET_DIR}"/>
 <property name="eclipselink.create-ddl-jdbc-file-name" value="schema.sql"/>
 <property name="eclipselink.drop-ddl-jdbc-file-name" value="drop-schema.sql"/>

我希望这会有所帮助。

【讨论】:

  • 问题是关于如何使main 方法起作用。作为解决方法,我确实在 Eclipse 中生成了 DDL ..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-14
  • 2017-11-27
  • 2016-03-14
  • 2018-12-25
  • 2016-01-21
  • 2019-05-19
  • 2010-09-22
相关资源
最近更新 更多