java - 为什么使用 Maven 和 Hibernate 设置一个简单的项目会失败?

标签 java mysql spring hibernate maven

这是一个简单的项目,包括进行 MySQL 查询并显示它。我使用 NetBeans。 hibernate.xml 和 client.xml 文件是由 NetBeans(Hibernate 配置向导和 Hibernate 映射向导)创建的。我真的不知道问题是什么。 1)POM文件:

<?xml version="1.0" encoding="UTF-8"?>
<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.mycompany</groupId>
    <artifactId>alsoMaven</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.10.Final</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

2)主类:

public static void main(String[] args) {
    StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
            .configure() // obtiene los valores de hibernate.cfg.xml
            .build();
    try {
        SessionFactory sessionFactory = new MetadataSources(registry)
                .buildMetadata().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        Cliente c = (Cliente) session.load(Cliente.class, 1);
        System.out.println(c);
        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

3)异常:

------------------------------------------------------------------------
Building alsoMaven 1.0-SNAPSHOT
------------------------------------------------------------------------
Downloading: http://repo.maven.apache.org/maven2/org/jboss/spec/javax/transaction/jboss-transaction-api_1.2_spec/1.1.1.Final/jboss-transaction-api_1.2_spec-1.1.1.Final.jar
Downloading: http://repo.maven.apache.org/maven2/org/javassist/javassist/3.24.0-GA/javassist-3.24.0-GA.jar
Downloading: http://repo.maven.apache.org/maven2/org/jboss/logging/jboss-logging/3.3.2.Final/jboss-logging-3.3.2.Final.jar
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/common/hibernate-commons-annotations/5.1.0.Final/hibernate-commons-annotations-5.1.0.Final.jar




------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 3.184s
Finished at: Sun Mar 08 21:10:54 GMT-03:00 2020
Final Memory: 6M/16M
------------------------------------------------------------------------
Failed to execute goal on project alsoMaven: Could not resolve dependencies for project com.mycompany:alsoMaven:jar:1.0-SNAPSHOT: The following artifacts could not be resolved: org.jboss.logging:jboss-logging:jar:3.3.2.Final, org.javassist:javassist:jar:3.24.0-GA, org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.1.1.Final, org.hibernate.common:hibernate-commons-annotations:jar:5.1.0.Final: Could not transfer artifact org.jboss.logging:jboss-logging:jar:3.3.2.Final from/to central (http://repo.maven.apache.org/maven2): Failed to transfer file: http://repo.maven.apache.org/maven2/org/jboss/logging/jboss-logging/3.3.2.Final/jboss-logging-3.3.2.Final.jar. Return code is: 501 , ReasonPhrase:HTTPS Required. -> [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.

谢谢!

最佳答案

Starting 15 January 2020 ,Maven 中央存储库不支持通过纯 HTTP 进行不安全的通信。您必须调整本地配置。

考虑到这一点,我们在错误消息中发现了一个重要的细节:

Return code is: 501, ReasonPhrase:HTTPS Required.

正如我们在输出中看到的,您仍在使用普通的、未加密的连接来从 Maven Central 查询/检索 Artifact :

Downloading: http://repo.maven.apache.org/maven2/org/jboss/spec/javax/transaction/jboss-transaction-api_1.2_spec/1.1.1.Final/jboss-transaction-api_1.2_spec-1.1.1.Final.jar

...

确保您的本地 settings.xml 仅包含 Maven 中央存储库的带有 https 的条目。因此,条目 http://repo.maven.apache.org/maven2/ 必须更改为 https://repo.maven.apache.org/maven2/ .

最小工作配置如下:

<profile>
    <id>maven-https</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <repositories>
        <repository>
            <id>central</id>
            <url>https://repo1.maven.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>central</id>
            <url>https://repo1.maven.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories> 
</profile>

希望有帮助。

关于java - 为什么使用 Maven 和 Hibernate 设置一个简单的项目会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60593326/

相关文章:

java - 按顺序比较字符串与其他字符串

php - 填充下拉选择列表

java - SimpleMessageListenerContainer 中的构造函数注入(inject)

java - Spring Boot 无法 Autowiring @ConfigurationProperties

java - appium - 选择父节点

java - 将文件从文件阵列复制到目录

php - 数组合并问题

php - Zend 项目 - mysql 未获取 UTF8

spring - Tomcat以管理员身份注销另一个用户

java - if 和 else 语句未按预期工作