JPA Hibernate:无效的persistence.xml

时间:2014-06-17 20:17:05

标签: java hibernate postgresql jpa

我在使用Hibernate和JPA时遇到了一些麻烦。我以前使用过Hibernate而不是JPA,所以现在编写我的第一个persistence.xml。我收到错误消息“无效的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>

1 个答案:

答案 0 :(得分:1)

看起来这是Java SE应用程序。您可能需要更改persistent-unit标记,如下所示:

<persistence-unit name="name" transaction-type="RESOURCE_LOCAL">
在Java SE中,您没有JTA事务,这是默认的。