简体   繁体   English

JPA Hibernate:无效的persistence.xml

[英]JPA Hibernate: Invalid persistence.xml

I am having some trouble with Hibernate and JPA. 我在使用Hibernate和JPA时遇到了一些麻烦。 I have used Hibernate before but not with JPA so writing my first persistence.xml currently. 我以前使用过Hibernate,但没有使用过JPA,因此目前正在编写我的第一个persistence.xml。 I get error message " Invalid persistence.xml." 我收到错误消息“ Invalid persistence.xml。” or that the entity manager could not be created, depending on how I write my persistence.xml. 或无法创建实体管理器,具体取决于我如何编写persistence.xml。 The code line that fails is 失败的代码行是

entityManagerFactory = Persistence.createEntityManagerFactory("name");

And the persisted class looks something like 持久化的类看起来像

@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;
}

And my pom.xml looks like this 我的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>

And this i my Persistence.xml placed in "src/main/java/resources/META-INF/ 这是我将我的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>

It looks like this is Java SE application. 看起来这是Java SE应用程序。 You might need to change persistent-unit tag like this: 您可能需要像这样更改persistent-unit标记:

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

in Java SE you don't have JTA transactions, which are default. 在Java SE中,您没有默认的JTA事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM