我一直试图用hibernate自动创建一个表。尝试了为同一个问题提供的所有解决方案。请看看并指出错误。我在MySQL 5.6版本中使用它。
public class Tester
{
public static void main(String[] args)
{
SessionFactory factory = new Configuration().configure("com/hibernate.hbm.xml").buildSessionFactory();
System.out.println(factory.toString() + " - DONE");
}
}
产品类是:
public class Product
{
private String product;
private String productName;
public String getProduct()
{
return product;
}
public void setProduct(String product)
{
this.product = product;
}
public String getProductName()
{
return productName;
}
public void setProductName(String productName)
{
this.productName = productName;
}
}
我的hbm文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com">
<class name="Product" table="product_table">
<id name="product" column="product_id" type="java.lang.String" length="20" >
<generator class="assigned" />
</id>
<property name="productName" column="product_name" type="java.lang.String" length="50" not-null="true" />
</class>
</hibernate-mapping>
下面给出了hibernate.cfg.xml -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/caching_hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="cache.providerclass">org.hibernate.NoCacheProvider</property>
<mapping resource="com/product.hbm.xml" />
</session-factory>
</hibernate-configuration>
程序显示没有异常并且正常运行..提前谢谢