日期:2014-05-17  浏览次数:20855 次

JPA 无法自动建表
本帖最后由 zhanglt 于 2011-06-03 17:03:50 编辑
最近在学JPA,遇到一个问题,发现代码无法自动建表。代码如下:


package com.babasport.bean.product;
import javax.persistence.Entity;
import javax.persistence.GenerationType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class ProductType {
private Integer typeid;

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getTypeid() {
return typeid;
}

public void setTypeid(Integer typeid) {
this.typeid = typeid;
}



}


package junit.test;


import javax.persistence.Persistence;
import javax.persistence.*;
import com.babasport.bean.product.*;
import org.junit.BeforeClass;
import org.junit.Test;

public class Product {

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@Test
public void runTest(){
           Persistence.createEntityManagerFactory("babasport");

}


}



配置文件如下:

<?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="babasport" transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <properties>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"></property>
        <property name="hibernate.connection.password" value="12345"></property>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/hibernate"></property>
        <property name="hibernate.connection.username" value="root"></property>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"></property>
        <!-- Use the C3P0 connection pool provider -->
 <!--<property name="hibernate.c3p0.min_size" value="5"></property>
<property name="hibernate.c3p0.max_size" value="20"></property>
<property name="hibernate.c3p0.timeout" value="300"></property>
<property name="hibernate.c3p0.max_statements" value="50"></property>
<property name="hibernate.c3p0.idle_test_period" value="3000"></property>-->
<!-- Show and print nice SQL on stdout -->
<property name="show_sql" value="true"