使用persistent.xml创建数据库时遇到了一个非常大的问题。好的,我从头开始。 我创建了一个maven(artifact - > webapp)项目。我将带有文件persistence.xml的META-INF目录添加到我的源目录中,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<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="myDatabase" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/Lab6" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
</properties>
</persistence-unit>
</persistence>
然后我将一些模型放入我的java源代码中。我添加了这样的apropiate注释:
package model;
import javax.persistence.*;
@Entity
@Table(name="author")
public class Author
{
private int Id;
private String firstName;
private String lastName;
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy= GenerationType.SEQUENCE, generator="seq")
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
@Column(name = "first_name", nullable = false)
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name = "last_name", nullable = false)
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
在此之后,我添加了一个servlet类来连接,让程序使用POST方法进行一些操作。没关系:)之后我运行了这个项目......没什么可恶的;程序运行成功,我可以进入servlet但数据库尚未创建。然后我开始在网络上搜索解决方案,没有任何问题可以解决我的问题。下面是我的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>Lab6</groupId>
<artifactId>Lab6</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Lab6 Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<finalName>Lab6</finalName>
<testResources>
<testResource>
<directory>src/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>net.java.dev.glassfish</groupId>
<artifactId>glassfish-persistence-api</artifactId>
<version>b32g</version>
</dependency>
</dependencies>
</project>
我真的卡住了。到目前为止,我在persistence.xml中检查了其他版本的提供程序(eclipse),并在该文件中进行了一些非常奇怪的更改,但没有成功。我正在检查战争档案。每个需要的文件都在里面(特别是persistence.xml)。 FI将我当前的glassfish版本的服务器更改为不同的版本,但它没有帮助:((( 真的,伙计们请帮助我。我的知识太低了,没有更好的想法ghow来解决这个问题
提前致谢
答案 0 :(得分:0)
您错过了persistence.xml
的一部分,告诉它如何找到您的课程。以下是几个选项:
<jar-file>MyLibrary.jar</jar-file>
<class>model.Author</class>
您选择的选项取决于项目的设置方式。