歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Hibernate+JUnit測試實體類生成數據庫表

今天在使用hibernate注解方式標明的實體類時產生數據庫表示遇到了一些問題,經過搜集一些資料,最後總結了一下,如下所示:

先把我的整體的幾個文件的代碼貼出來吧

這是hibernate.cfg.xml文件

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

 <session-factory>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/phn_dsjava</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">123456</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.show_sql">true</property>
  <property name="format_sql">true</property>
  <property name="hibernate.hbm2ddl.auto">update</property>

  <mapping class="com.phn.bean.Announces"/>

 </session-factory>

</hibernate-configuration>

這是實體類Announces.java

package com.phn.bean;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * @author phn
 *
 */
@Entity
@Table(name = "t_announce")
public class Announces {

 private Integer id;
 private String announcement;
 private String title;
 private Date thetime;

 @Id
 @GeneratedValue
 @Column(nullable = false)
 public Integer getId() {
  return this.id;
 }

 public void setId(Integer id) {
  this.id = id;
 }

 @Column(length = 20000)
 public String getAnnouncement() {
  return this.announcement;
 }

 public void setAnnouncement(String announcement) {
  this.announcement = announcement;
 }

 @Column(length = 100)
 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public Date getThetime() {
  return thetime;
 }

 public void setThetime(Date thetime) {
  this.thetime = thetime;
 }

}

這是測試類HibernateAnnotationTest.java

package com.phn.junitTest;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.Test;

import junit.framework.TestCase;

public class HibernateAnnotationTest extends TestCase {
 @Test
 public void testSQL() {
  AnnotationConfiguration configuration = new AnnotationConfiguration();
  configuration.configure();
  SessionFactory sessionFactory = configuration.buildSessionFactory();
 }
}

---------------------------下面是大致問題解釋---------------------------

Hibernate的配置文件hibernate.cfg.xml位於src目錄下。在單元測試時,執行下面代碼時,會產生異常。

Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();

異常:
org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="***"/>

Hibernate配置文件中,若帶有<mapping class="com.phn.Users"/>,則說明映射類時,采用了Annotation方式。在初始化Configuation時,應使用AnnoationConfiguration,代碼如下:

AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration.configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();

注意:這裡使用的hibernate版本是3.3
版本低一點可能會出現下面圖片這個錯誤,這是因為低版本的hibernate的jar包org.hibernate.engine.query.sql.NativeSQLQueryReturn類缺少,因此將其版本更新一下就行了

Hibernate整體理解 http://www.linuxidc.com/Linux/2014-07/104405.htm

Hibernate的映射機制  http://www.linuxidc.com/Linux/2014-12/110265.htm

Hibernate 的詳細介紹:請點這裡
Hibernate 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved