我正在尝试使用NetBeans运行一个小的Maven测试程序。现在,NetBeans提供的代码与Maven完美配合。编译,运行,一切都很好。但是当我改变任何东西时,比如添加FXML文件或添加JPA持久性,我在启动程序时会遇到错误。编译似乎有效,但我无法启动该程序。我希望错误在Maven的配置中,但我找不到它。代码本身应该可以正常工作。
好的,向你们展示我的意思,这里是代码。我只是将一个JPA持久性添加到MySQL数据库中。添加程序后不再运行。
App.java :
package com.myproject.mavenproject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello new World!" );
EntityManagerFactory emf = Persistence.createEntityManagerFactory( "GlassesPU" );
try {
EntityManager em = emf.createEntityManager();
EntityTransaction tx = null;
try {
tx = em.getTransaction();
tx.begin();
Hund hund = new Hund();
hund.setFormerName("Albert");
hund.setNewName("Einstein");
em.persist(hund);
//em.persist( entity1 );
//em.merge( entity2 );
//em.find( MyEntity.class, id );
tx.commit();
} catch( RuntimeException ex ) {
if( tx != null && tx.isActive() ) tx.rollback();
throw ex;
} finally {
em.close();
}
} finally {
emf.close();
}
}
}
Hund.java :
package com.myproject.mavenproject;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Hund implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String formerName;
private String newName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFormerName() {
return formerName;
}
public void setFormerName(String formerName) {
this.formerName = formerName;
}
public String getNewName() {
return newName;
}
public void setNewName(String newName) {
this.newName = newName;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Hund)) {
return false;
}
Hund other = (Hund) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.myproject.mavenproject.Hund[ id=" + id + " ]";
}
}
的persistence.xml :
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="com.myproject_MavenProject_jar_1.0-SNAPSHOTPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.myproject.mavenproject.Hund</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/glasses?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.password" value="mypassword"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
的pom.xml :
<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.myproject</groupId>
<artifactId>MavenProject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MavenProject</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.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.5.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
错误消息:
cd /home/myuser/NetBeansProjects/MavenProject; JAVA_HOME=/usr/lib/jvm/jdk1.7.0_51 /home/myuser/netbeans-7.4/java/maven/bin/mvn "-Dexec.args=-classpath %classpath com.myproject.mavenproject.App" -Dexec.executable=/usr/lib/jvm/jdk1.7.0_51/bin/java org.codehaus.mojo:exec-maven-plugin:1.2.1:exec
Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.
Scanning for projects...
------------------------------------------------------------------------
Building MavenProject 1.0-SNAPSHOT
------------------------------------------------------------------------
--- exec-maven-plugin:1.2.1:exec (default-cli) @ MavenProject ---
Hello new World!
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named GlassesPU
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at com.myproject.mavenproject.App.main(App.java:18)
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 0.891s
Finished at: Mon Feb 03 21:35:25 CET 2014
Final Memory: 5M/111M
------------------------------------------------------------------------
Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project MavenProject: Command execution failed. Process exited with an error: 1 (Exit value: 1) -> [Help 1]
To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.
For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
任何人都知道问题可能是什么?
答案 0 :(得分:1)
Maven没有问题。它只报告错误。它说它找不到一个名为&#34; GlassesPU&#34;的持久性提供者。
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named GlassesPU
你的persistence.xml有另一个名字:
<persistence-unit name="com.myproject_MavenProject_jar_1.0-SNAPSHOTPU" ... >
尝试将其更改为&#34; GlassesPU&#34;:
<persistence-unit name="GlassesPU" ...>
如果Maven再次抱怨,那可能是由于其他原因。