mysql连接中的java-nosuchmethod

gmxoilav  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(270)

我在用java连接mysql数据库时遇到了一个问题。我尝试过在pom.xml中修改hibernate core/hibernate entitymanager/hibernate jpa的版本,但没有任何效果。这似乎是hibernate jpa复制的问题,或者它不能与mysql connecot一起工作。试了两个小时后,我决定寻求帮助。
pom.xml文件:

<?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>Pracownia</groupId>
    <artifactId>PracowniaMain</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <resources>
            <resource>
                <directory>src</directory>
                <includes>
                    <include>META-INF/persistence.xml</include>
                </includes>
            </resource>
        </resources>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <attach>false</attach>
                            <appendAssemblyId>false</appendAssemblyId>
                            <archive>
                                <manifest>
                                    <mainClass>
                                        hibernate.Manager
                                    </mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- Hibernate resources -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.common</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>4.0.2.Final</version>
            <classifier>tests</classifier>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>

        <!-- Java Bind -->
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.11</version>
        </dependency>

        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.2.11</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-joda</artifactId>
            <version>2.9.5</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.7</version>
        </dependency>
    </dependencies>

</project>

持久性.xml:

<!--<persistence 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_0.xsd"
             version="2.0">-->
<persistence 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"
             version="2.1">

    <persistence-unit name="hibernate-dynamic" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <!--<class>org.halyph.sessiondemo.Event</class>-->

        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/phpmyadmin?useSSL=false" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="" />

            <property name="hibernate.connection.driver_class" value ="com.mysql.cj.jdbc.Driver" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
            <property name="hibernate.format_sql" value="false"/>
        </properties>

    </persistence-unit>

</persistence>

运行此命令后:

package hibernate;

import hibernate.model.artysci;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.List;

class Manager {

    public static void main(String[] args) {

            System.out.println("Start");

            EntityManager entityManager = null;

            EntityManagerFactory entityManagerFactory = null;

            System.out.println("Start2");
        try {
            System.out.println("Start3");
            //taka nazwa jak w persistence.xml
            entityManagerFactory = Persistence.createEntityManagerFactory("hibernate-dynamic");
            //utworz entityManagera
            System.out.println("Start4");
            entityManager = entityManagerFactory.createEntityManager();

            //rozpocznij transakcje
            entityManager.getTransaction().begin();
            //System.out.println("dis");
            /*
            Employee emp = new Employee();
            emp.setFirstName("Jan");
            emp.setLastName("Polak");
            emp.setSalary(100);
            emp.setPesel(100);

            entityManager.persist(emp);

            Employee employee = entityManager.find(Employee.class, emp.getId());

            entityManager.remove(emp);

            System.out.println("Employee " + employee.getId() + " " + employee.getFirstName() +     employee.getLastName());
            */
            //zakoncz transakcje
            entityManager.getTransaction().commit();

            System.out.println("Done");

            entityManager.close();

        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
        } finally {
            entityManagerFactory.close();
        }

    }

}

慰问:

Start
Start2
Start3
2021-01-06 18:52:56 DEBUG logging:38 - Logging Provider: org.jboss.logging.Log4jLoggerProvider
2021-01-06 18:52:56 INFO  LogHelper:31 - HHH000204: Processing PersistenceUnitInfo [
    name: hibernate-dynamic
    ...]
2021-01-06 18:52:57 INFO  Version:37 - HHH000412: Hibernate Core {5.2.0.Final}
2021-01-06 18:52:57 INFO  Environment:213 - HHH000206: hibernate.properties not found
2021-01-06 18:52:57 INFO  Environment:318 - HHH000021: Bytecode provider name : javassist
2021-01-06 18:52:57 INFO  Version:66 - HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2021-01-06 18:52:57 WARN  connections:71 - HHH10001002: Using Hibernate built-in connection pool (not for production use!)
2021-01-06 18:52:57 INFO  connections:127 - HHH10001005: using driver [com.mysql.cj.jdbc.Driver] at URL [jdbc:mysql://localhost/phpmyadmin?useSSL=false]
2021-01-06 18:52:57 INFO  connections:136 - HHH10001001: Connection properties: {user=root}
2021-01-06 18:52:57 INFO  connections:141 - HHH10001003: Autocommit mode: false
2021-01-06 18:52:57 INFO  DriverManagerConnectionProviderImpl:39 - HHH000115: Hibernate connection pool size: 20 (min=1)
2021-01-06 18:52:57 INFO  Dialect:152 - HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Initial SessionFactory creation failed.java.lang.NoSuchMethodError: javax/persistence/Table.indexes()[Ljavax/persistence/Index; (loaded from file:/C:/Users/xxx/Desktop/PROJEKT%202/lib/javax.persistence.jar by sun.misc.Launcher$AppClassLoader@102dc1cf) called from class org.hibernate.cfg.annotations.EntityBinder (loaded from file:/C:/Users/xxx/.m2/repository/org/hibernate/hibernate-core/5.2.0.Final/hibernate-core-5.2.0.Final.jar by sun.misc.Launcher$AppClassLoader@102dc1cf).
Exception in thread "main" java.lang.NullPointerException
    at hibernate.Manager.main(Manager.java:59)

Process finished with exit code -1

谢谢你的帮助!

zlhcx6iw

zlhcx6iw1#

问题与您的依赖项有关,其中一些依赖项以可传递的方式加载不同版本的jpa,因此会产生错误。
在pom的情况下,您有3倍的相同依赖性:
hibernate实体管理器(以可传递的方式加载hibernate-jpa-2.0-api:1.0.0.final)
hibernate核心(以可传递的方式加载hibernate-jpa-2.0-api:1.0.0.final)
休眠-jpa-2.0-api
它们都导入“hibernate-jpa-2.0-api”,但版本不同。我建议删除hibernate-jpa-2.0-api

<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.0-api</artifactId>
    <version>1.0.1.Final</version>
</dependency>

另一个选项是排除其他依赖项的依赖项。
一个好的插件,检查所有可能的冲突,在您的是强制执行

相关问题