gpt4 book ai didi

java - 更改代码时 Maven 出现问题?

转载 作者:行者123 更新时间:2023-12-01 13:31:32 27 4
gpt4 key购买 nike

我正在尝试使用 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

有人知道问题出在哪里吗?

最佳答案

Maven没有问题。它只是报告错误。它说它找不到名为“GlassesPU”的持久性提供程序。

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

尝试将其更改为“GlassesPU”:

<persistence-unit name="GlassesPU" ...>

如果 Maven 再次提示,可能是由于其他原因。

关于java - 更改代码时 Maven 出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21537369/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com