hibernate自动生成表遇到的问题总结。

我用的是Hibernate4.2.20+Mysql5.5.28

遇到的问题:运行测试代码时,未能自动创建数据表,此时问题可能有以下原因:

1.hibernate.cfg.xml中未配置数据表自动生成策略;

<!-- 指定数据表生成方式 -->
<property name="hbm2ddl.auto">update</property>

配置上边的以后才可以自动生成数据表(注:参数值可为 validate,update,create和create-drop ,update的指定若数据库无数据表时自动新建数据表,若存在不进行新建)

2.数据库方言使用不当:

<!-- 配置数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

我的这个需要用到org.hibernate.dialect.MySQLDialect,根据数据库版本不同用的数据库方言也不同,Hibernate提供几种数据库方言,可在hibernate-release-4.2.20.Final\project\etc中的hibernate.properties查看。


根据需求选择数据库方言。

3.映射文件*.hbm.xml中的主键生成方式

<!-- 指定主键生成方式 -->
        <generator class="native" />

一般指定native,它是依据底层数据库的能力在identity,sequence,hilo 3中生成器中选择一种,适合跨数据库平台的开发。

还有其他几种生成策略有需要的可以了解一下。

4.自动生成的表与系统中的表重名,也会不能生成

今天就遇见了这样的问题,要生成Customer表和Order表,结果Customer生成了,Order没能生成,把Order改成Order_1就生成成功了。生成数据表的时候需要注意一下系统的本身存在的表名。


下面是完整的代码:

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
	<!-- 基本数据库连接配置 -->
		<property name="connection.username">root</property>
		<property name="connection.password">123456</property>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/se</property>

		<!-- 配置Hibernate基本信息 -->
		<!-- 配置数据库方言 -->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- 是否在控制台打印SQL语句 -->
		<property name="show_sql">true</property>
		<!-- 是否格式化SQL语句 -->
		<property name="format_sql">true</property>
		<!-- 指定数据表生成方式 -->
		<property name="hbm2ddl.auto">update</property>
		
		<mapping resource="com/Hibernate/zzh/Customer.hbm.xml"/>
		<mapping resource="com/Hibernate/zzh/Order_1.hbm.xml"/>
	</session-factory>
</hibernate-configuration>


Customer.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2015-10-6 11:47:34 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
	<class name="com.Hibernate.zzh.Customer" table="CUSTOMER">
		<id name="customerID" type="int">
			<column name="CUSTOMER_ID" />
			<generator class="native" />
		</id>
		<property name="customerName" type="java.lang.String">
			<column name="CUSTOMER_NAME" />
		</property>
	</class>
</hibernate-mapping>

Order_1 .hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2015-10-6 11:47:34 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.Hibernate.zzh.Order_1" table="ORDER_1">
        <id name="orderID" type="int">
            <column name="ORDER_ID" />
            <generator class="native" />
        </id>
        <property name="orderName" type="java.lang.String">
            <column name="ORDER_NAME" />
        </property>
        <many-to-one name="customer" class="com.Hibernate.zzh.Customer" fetch="join">
            <column name="CUSTOMER_ID" />
        </many-to-one>
    </class>
</hibernate-mapping>

实体类:

Customer.java

public class Customer {

	private int customerID;
	private String customerName;

	public int getCustomerID() {
		return customerID;
	}

	public void setCustomerID(int customerID) {
		this.customerID = customerID;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

}

Order_1.java

public class Order_1 {

	private int orderID;
	private String orderName;
	private Customer customer;

	public int getOrderID() {
		return orderID;
	}

	public void setOrderID(int orderID) {
		this.orderID = orderID;
	}

	public String getOrderName() {
		return orderName;
	}

	public void setOrderName(String orderName) {
		this.orderName = orderName;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

}

测试类:

Test.java

import static org.junit.Assert.*;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;

public class Test {

	// 1.创建SessionFactory对象
		SessionFactory sessionFactory;
		Session session;
		Transaction transaction;
		@Before
		public void init() {
			// 1).创建Configuration对象,对应hibernate的配置信息
			Configuration configuration = new Configuration().configure();

			// 2).创建ServiceRegistry对象:hibernate 4.x新添加对象
			// hibernate的任何配置和服务都需要在其中注册后才有效
			ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
					.buildServiceRegistry();

			sessionFactory = configuration.buildSessionFactory(serviceRegistry);

			// 2.创建Session对象
			session = sessionFactory.openSession();

			// 3.开启事务
			transaction = session.beginTransaction();
		}
		@After
		public void destroy() {
			// 5.提交事务
			transaction.commit();
			// 6.关闭Session
			session.close();
			// 7.关闭SessionFactory
			sessionFactory.close();

		}

	@org.junit.Test
	public void test() {
		
	}

}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值