У меня проблемы с 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, которые по умолчанию.