【问题标题】:JPA Hibernate: Invalid persistence.xmlJPA Hibernate:无效的persistence.xml
【发布时间】:2014-06-17 20:17:04
【问题描述】:

我在使用 Hibernate 和 JPA 时遇到了一些问题。我以前使用过 Hibernate,但没有使用 JPA,所以目前正在编写我的第一个 persistence.xml。我收到错误消息“Invalid persistence.xml”。或者无法创建实体管理器,这取决于我编写persistence.xml 的方式。失败的代码行是

entityManagerFactory = Persistence.createEntityManagerFactory("name");

持久化类看起来像

@Entity
@Table( name = "test_tbl" )
public class Test {

private Long _id;
private String _type;

public Position() {
    // this form used by Hibernate
}

@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
@Column(name = "n_id")
public Long getId() {
    return _id;
}

public void setId(Long id) {
    this._id = id;
}


@Column(name = "str_type")
public String getType() {
    return _type;
}

public void setType(String type) {
    this._type = type;
}

我的 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycoolproject</groupId>
<artifactId>Proj</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>Proj</name>
<url>http://maven.apache.org</url>

<properties>
<!-- Skip artifact deployment -->
<maven.deploy.skip>true</maven.deploy.skip>
</properties>


<dependencies>

  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
  </dependency>

  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.2.13.Final</version>
  </dependency>

  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.2.13.Final</version>
  </dependency>

  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-envers</artifactId>
    <version>4.2.13.Final</version>
  </dependency>

  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.6.1</version>
  </dependency>

  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
  </dependency>

</dependencies>


<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>

  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.1</version>
      <configuration>
        <source>1.7</source>
        <target>1.7</target>
      </configuration>
    </plugin>
  </plugins>

</build>
</project>

这是我的 Persistence.xml 放在“src/main/java/resources/META-INF/

<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="name">
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 

    <class>com.mycoolproject.Test</class>

    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> 
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5555/trial"/>
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
        <property name="javax.persistence.jdbc.user" value="lalala"/>
        <property name="javax.persistence.jdbc.password" value="testing"/>
        <property name="hibernate.max_fetch_depth" value="3"/>
        <property name="hibernate.show_sql" value="true" />
    </properties>

</persistence-unit>

</persistence>

【问题讨论】:

    标签: java hibernate postgresql jpa


    【解决方案1】:

    看起来这是 Java SE 应用程序。您可能需要像这样更改persistent-unit 标签:

    <persistence-unit name="name" transaction-type="RESOURCE_LOCAL">
    

    在 Java SE 中,您没有默认的 JTA 事务。

    【讨论】:

    • 谢谢!这是问题以及 maven 文件中缺少 postgresql 依赖项。
    猜你喜欢
    • 2014-03-17
    • 1970-01-01
    • 2015-08-08
    • 2017-11-20
    • 2011-02-09
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    相关资源
    最近更新 更多