在我看来,Tomcat忽略了presistence.xml,但是我把它放在了正确的目录中,当我部署我的.war项目时,表格不会在我的数据库中创建。
我的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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>myproject.new</groupId>
<artifactId>myproject.new.jsfProject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>JSsjProject</name>
<properties>
<jdk.version>1.7</jdk.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.8.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.2.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1.13</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.13</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
<build>
<finalName>JsfProject</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<url>http://localhost:8081/manager/text</url>
<server>my-tomcat</server>
<path>/JsfProject</path>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
我的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
<persistence-unit name="MyPersistenceUnit" transaction-type="RESOURCE_LOCAL" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/testbd"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.hbm2ddl.import_files" value= "import.sql"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>
我的项目结构:
当我甚至从我的项目中删除持久性xml并进行部署时,TomCat仍然没有反应,没有错误,没有警告。你能帮帮我们吗?
UPDATE1 我改变了我的persistence.xml:
<property name="hibernate.hbm2ddl.auto" value="update"/>
那也就是我的UsersDao服务:
public class UserDao {
private EntityManager entityManager;
public UserDao(){
entityManager = Persistence.createEntityManagerFactory("MyPersistenceUnit").createEntityManager();
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
public EntityManager getEntityManager() {
return entityManager;
}
public Users findUserById(Long id){
Users user = null;
user = entityManager.find(Users.class,id);
System.out.println("Find user: " + user.getUser() + " " + user.getPass());
return user;
}
public Users findByName(String username) {
System.out.println("IN FIND BY NAME");
Users user = null;
List result = (List) entityManager.createQuery(
"select u from Users u where u.user = :userName").setParameter("userName", username).getResultList();
if(!result.isEmpty()){
user = (Users) result.get(0);
}
System.out.println("FIND USER " + user);
return user;
}
public boolean addUser(Users user){
try {
entityManager.merge(user);
return true;
}catch (Exception e){
System.out.println("Error in addUser() -->" + e.getMessage());
return false;
}
}
public boolean checkAvailable(String userName) {
Query query = entityManager
.createQuery("select count(*) from Users"
+ " u where u.user = :userName").setParameter("userName", userName);
Long count = (Long) query.getSingleResult();
return count < 1;
}
}
我的用户实体:
@Entity
@Table(name ="userinfo")
public class Users implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
private String user;
@Column(nullable = false)
private String role;
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
private String pass;
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
但是当我部署项目时,userinfo表仍然没有创建!为什么呢?
UPDATE2 persistence.xml路径
答案 0 :(得分:1)
您的配置是:
在tomcat关闭后,该值将丢弃表。
将其更改为&#34;更新&#34;。
其他细节是:<persistence-unit name="MyPersistenceUnit" transaction-type="RESOURCE_LOCAL" >
。使用此配置,您需要确保Persistence.createEntityManagerFactory("YOUR_PERSISTENCE_UNIT");
。
如果未调用上述代码,则不会创建表。您可以创建一个在服务器启动后调用的侦听器,或者在项目的第一页访问中触发一些方法。