快崩溃了,JPA简单例子,junit测试通过,但总是生成不了数据库!!请高人帮忙看看!!!!不知道是跟ECLIPES有关系还是跟导入的jar有关系还是跟例子代码有关系。
下面贴上例子代码。1、建立web项目.
2、Hibernate-3.2.6.GA目录下hibernate3.jar以及lib目录下所有的jar.
hibernate-entitymanager-3.3.2.CR1目录下hibernate-entitymanager.jar以及lib目录下的三个jar.拷贝到WebRoot的WEB-INF的lib的目录下。把mysql数据库驱动mysql-connector-java-5.0.5-bin.jar拷贝WebRoot的WEB-INF的lib的目录下.3、在web项目的src目录下建立META-INF目录,并建立persistence.xml文件。
内容如下<?xml version="1.0"?>
<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_1_0.xsd" version="1.0">
  <persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
         <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
         <property name="hibernate.connection.username" value="root"/>
         <property name="hibernate.connection.password" value="123456"/>
         <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
     <property name="hibernate.show_sql" value="false"/>
     <property name="hibernate.format_sql" value="false"/> 
      </properties>
  </persistence-unit>
</persistence>4、建立ProductType.javapackage com.itcast.bean.product;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class ProductType {
private Integer typeid;


@Id
    @GeneratedValue(strategy=GenerationType.AUTO)
public Integer getTypeid() {
return typeid;
} public void setTypeid(Integer typeid) {
this.typeid = typeid;
}

}
5、建立测试,junit.test.ProductTest.java内容如下:package junit.test;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;import org.junit.BeforeClass;
import org.junit.Test;public class ProductTest { @BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@Test
public void runtest(){

EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast"); factory.close();
}}测试绿条通过。但数据库内死活生成不了数据库和表!!!反反复复重新了很多次检查,就是只能测试通过,但实际生成不了数据库表。

解决方案 »

  1.   

    Eclipse SDK 版本 3.2myeclipse FullStack_MyEclipse5.1.0GA_E3.2.1.exe
      

  2.   

    一般是先有数据库,再有 Entity 吧?
      

  3.   

    @Entity
    你这里没有指定生成的数据表名啊
      

  4.   

    靠。成功。我还真疏忽呢!搞了半天,不知道什么原因,差点就要重新配置eclipse3.5+myeclipse6了。
      

  5.   

    @Entity不需要指定表名,可以根据类名生成表。