gpt4 book ai didi

java - JPA hibernate : Invalid persistence. xml

转载 作者:行者123 更新时间:2023-11-29 13:30:46 24 4
gpt4 key购买 nike

我在使用 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>

最佳答案

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

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

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

关于java - JPA hibernate : Invalid persistence. xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24272577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com