Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';找不到persistence.xml_Java_Spring_Hibernate_Jpa_Spring Orm - Fatal编程技术网

Java Can';找不到persistence.xml

Java Can';找不到persistence.xml,java,spring,hibernate,jpa,spring-orm,Java,Spring,Hibernate,Jpa,Spring Orm,我尝试将jpa与spring orm和hibernate结合使用 我有mysql数据库和交易表。我尝试使用jpa处理这个表。 我尝试使用persistence.xml测试EntityManager的创建,但遇到一个异常:“EntityManager没有名为[persistence.xml中的persistence unit name]的持久性提供程序。” 正如我在另一个这样的问题中所看到的,对于我的情况来说,persistence.xml文件找不到是很重要的 当我尝试测试这个配置时,我得到了一个

我尝试将jpa与spring orm和hibernate结合使用

我有mysql数据库和交易表。我尝试使用jpa处理这个表。 我尝试使用persistence.xml测试EntityManager的创建,但遇到一个异常:“EntityManager没有名为[persistence.xml中的persistence unit name]的持久性提供程序。” 正如我在另一个这样的问题中所看到的,对于我的情况来说,persistence.xml文件找不到是很重要的

当我尝试测试这个配置时,我得到了一个异常,我认为persistence.xml找不到

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named trade-mysql-pu
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
    at com.madhusudhan.jsd.jpa.EntityManagerTest.init(EntityManagerTest.java:16)
    at com.madhusudhan.jsd.jpa.EntityManagerTest.main(EntityManagerTest.java:29)
我不明白为什么会这样。你能帮我吗? 谢谢

表:

create table TRADES (ID int NOT NULL, 
    ACCOUNT VARCHAR(20) NOT NULL, 
    SECURITY VARCHAR(10) NOT NULL, 
    QUANTITY INT NOT NULL, 
    STATUS VARCHAR(10), 
    DIRECTION VARCHAR(10)
);
此表的实体类:

package com.madhusudhan.jsd.domain.jpa;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="trades")
public class Trade {

    private int id;
    private String direction;
    private String account;
    private String security;
    private String status;
    private int quantity;

    @Column(nullable=false)
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    @Column
    public String getDirection() {
        return direction;
    }
    public void setDirection(String direction) {
        this.direction = direction;
    }

    @Column
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }

    @Column
    public String getSecurity() {
        return security;
    }
    public void setSecurity(String security) {
        this.security = security;
    }

    @Column
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }

    @Column
    public int getQuantity() {
        return quantity;
    }
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    @Override
    public String toString() {
        return "Trade [id=" + id + ", direction=" + direction + ", account="
                + account + ", security=" + security + ", status=" + status
                + "]";
    }

}
和src/main/resources/META-INF中的persistence.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-unit name="trade-mysql-pu" transaction-type="RESOURCE_LOCAL">
        <provider>org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter</provider>
        <class>com.madhusudhan.jsd.domain.jpa.Trade</class>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="validate" />
            <property name="show_sql" value="true" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/JSDATA"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="prospring4"/>
            <property name="hibernate.connection.password" value="prospring4"/>
            <property name="hibernate.connection.pool_size" value="1"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
        </properties>
    </persistence-unit>
</persistence>
build.gradle中的依赖项:

dependencies {
    compile 'org.springframework:spring-context:4.1.6.RELEASE'
    compile 'org.springframework:spring-jdbc:4.1.6.RELEASE'
    compile 'org.springframework:spring-orm:4.1.6.RELEASE'
    compile 'commons-dbcp:commons-dbcp:1.4'
    compile 'mysql:mysql-connector-java:5.1.18'
    compile 'org.hibernate:hibernate-core:3.6.0.Final'
    compile 'org.hibernate:hibernate-entitymanager:3.6.0.Final'
    compile 'junit:junit:4.7'
    compile 'log4j:log4j:1.2.14'
}

您可以尝试以下几件事:

  • 检查src/main/resources文件夹是否在类路径中。在Eclipse中,这意味着右键单击项目>属性>Java构建路径,并确保在Source选项卡下指定了src/main/resources。如果不存在,请单击“添加文件夹”以添加资源文件夹
  • 如果上述操作不起作用,请尝试将persistence.xml移动到src/main/java/META-INF文件夹
  • 将persistence.xml中的提供程序更改为

    org.hibernate.ejb.HibernatePersistence


  • 你的建筑布局是什么?persistence.xml在哪里?@LeosLiterak persistence.xml在src/main/resources/META-INF中,src/main/resources/在构建路径中,没有任何排除项你好,Tony。谢谢你的回复。但是我尝试将文件夹移动到src/main/java,结果是一样的。我还检查了Eclipse/java和/resources的构建路径中是否没有任何排除的文件。persistence.xml是否确实复制到了target WEB-INF/classes文件夹中?请尝试上面我的第三个建议。谢谢。这是一个独立的应用程序,不应该像我想的那样有WEB-INF。谢谢你的帮助!它是一个独立的应用程序,不应该像我想的那样有WEB-INF。该persistence.xml文件是在将HibernatePersistence类用作提供程序而不是HibernateJavaEndorapter时找到的。现在我得到了一条关于不一致性的消息“分离的实体传递到持久化”,我将尝试找出发生这种情况的原因。
    dependencies {
        compile 'org.springframework:spring-context:4.1.6.RELEASE'
        compile 'org.springframework:spring-jdbc:4.1.6.RELEASE'
        compile 'org.springframework:spring-orm:4.1.6.RELEASE'
        compile 'commons-dbcp:commons-dbcp:1.4'
        compile 'mysql:mysql-connector-java:5.1.18'
        compile 'org.hibernate:hibernate-core:3.6.0.Final'
        compile 'org.hibernate:hibernate-entitymanager:3.6.0.Final'
        compile 'junit:junit:4.7'
        compile 'log4j:log4j:1.2.14'
    }