2011-12-28 62 views
1

我有簡單的數據庫(使用MySQL),一個汽車表:Spring + Hibernate的:使用JUnit初始化數據庫不起作用

create table vehicle (
    vehicle_no varchar(10) not null, 
    color varchar(10), 
    wheel int, 
    seat int, 
    primary key (vehicle_no) 
) engine = InnoDB; 

在Java中,我有DAO對象應該查詢所有車輛(其它DAO的方法是ommited)。這DAO應該加入現有的事務或如果需要創建新的:

@Transactional(propagation=Propagation.REQUIRED, readOnly=false) 
public class HibernateVehicleDao implements VehicleDao { 

    private SessionFactory sessionFactory; 
    @Autowired 
    public void setSessionFactory(SessionFactory sessionFactory) { 
     this.sessionFactory = sessionFactory; 
    } 

    @Override 
    public List<Vehicle> findAll() { 
     return currentSession().createQuery("from Vehicle").list(); 
    } 
} 

現在,我已經寫了JUnit測試(JUnit4)用於DAO。在運行測試方法之前,應將10輛車輛插入數據庫,運行後應刪除所有車輛。我已經分開測試這種行爲與Spring的JDBC和一切工作正常,所以應該沒有問題。

@ContextConfiguration(locations = "/sk/xorty/dataaccess/dataaccess-beans.xml") 
public class HibernateVehicleDaoTest extends AbstractTransactionalJUnit4SpringContextTests { 

    private static final int COUNT = 10; 

    @Autowired 
    @Qualifier("hibernateVehicleDao") 
    private VehicleDao dao; 

    @Before 
    public void setUp() { 
     String insert = 
       "INSERT INTO VEHICLE (VEHICLE_NO, COLOR, WHEEL, SEAT) VALUES (?, ?, ?, ?)"; 
     List<Object[]> argsList = new ArrayList<>(); 
     for (int i = 0; i < COUNT; i++) { 
      argsList.add(VehicleUtil.nextVehicleArgs()); 
     } 
     simpleJdbcTemplate.batchUpdate(insert, argsList); 
    } 

    @After 
    public void tearDown() { 
     simpleJdbcTemplate.update("DELETE FROM VEHICLE", (Object[]) null); 
    } 

    @Test 
    public void testFindAll() { 
     assertEquals (COUNT, dao.findAll().size()); 
    } 
} 

一切加載,所以我懷疑配置是正確的和依賴性得到適當的注入。

問題是,測試失敗,因爲數據庫是空的(沒有車輛)。當我手動插入它們時,它們不會從另一方面刪除。

請嘗試注意使用交易註釋,我對此很新,我認爲我可能在那裏做了錯誤。

這是我的bean配置文件,如果這可能是任何幫助:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <context:annotation-config /> 
    <tx:annotation-driven /> 

    <!-- shared data source --> 
    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" 
      value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" /> 
     <property name="url" value="jdbc:mysql://localhost/vehicles" /> 
     <property name="username" value="root" /> 
     <property name="password" value="" /> 
    </bean> 

    <!-- JDBC transaction manager --> 
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
     <property name="dataSource" ref="dataSource" /> 
    </bean> 

    <!-- hibernate session factory --> 
    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="annotatedClasses" > 
      <list> 
       <value>sk.xorty.dataaccess.Vehicle</value> 
      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.hbm2ddl.auto">update</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="hibernateVehicleDao" class="sk.xorty.dataaccess.HibernateVehicleDao" /> 

</beans> 

編輯:要求車輛的實體代碼:

@Entity 
@Table(name="vehicle") 
public class Vehicle implements Serializable { 

    @Id 
    @Column(name="VEHICLE_NO", nullable=false, length=10) 
    private String vehicleNo; 
    private String color; 
    private int wheel; 
    private int seat; 

    public Vehicle() {} 

    public Vehicle(String vehicleNo, String color, int wheel, int seat) { 
     this.vehicleNo = vehicleNo; 
     this.color = color; 
     this.wheel = wheel; 
     this.seat = seat; 
    } 

    public String getVehicleNo() { 
     return vehicleNo; 
    } 

    public void setVehicleNo(String vehicleNo) { 
     this.vehicleNo = vehicleNo; 
    } 

    public String getColor() { 
     return color; 
    } 

    public void setColor(String color) { 
     this.color = color; 
    } 

    public int getWheel() { 
     return wheel; 
    } 

    public void setWheel(int wheel) { 
     this.wheel = wheel; 
    } 

    public int getSeat() { 
     return seat; 
    } 

    public void setSeat(int seat) { 
     this.seat = seat; 
    } 

    @Override 
    public String toString() { 
     return "Vehicle [vehicleNo=" + vehicleNo + ", color=" + color 
       + ", wheel=" + wheel + ", seat=" + seat + "]"; 
    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((color == null) ? 0 : color.hashCode()); 
     result = prime * result + seat; 
     result = prime * result 
       + ((vehicleNo == null) ? 0 : vehicleNo.hashCode()); 
     result = prime * result + wheel; 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     Vehicle other = (Vehicle) obj; 
     if (color == null) { 
      if (other.color != null) 
       return false; 
     } else if (!color.equals(other.color)) 
      return false; 
     if (seat != other.seat) 
      return false; 
     if (vehicleNo == null) { 
      if (other.vehicleNo != null) 
       return false; 
     } else if (!vehicleNo.equals(other.vehicleNo)) 
      return false; 
     if (wheel != other.wheel) 
      return false; 
     return true; 
    } 

} 
+0

你可以發佈車輛實體嗎? – 2011-12-28 17:29:29

+0

@ kmb385當然,請參閱編輯 – Xorty 2011-12-28 17:31:08

+0

你在哪裏實例化simpleJdbcTemplate? – 2011-12-28 17:38:01

回答

-2

我不知道這事,但外殼在以下方法是不正確的:

@Override 
public List<Vehicle> findAll() { 
    return currentSession().createQuery("from Vehicle").list(); 
} 

表名以「vehicle」中的小寫字母v開頭,而此方法使用大寫字母「V」。

另一個有趣的事情我從文檔閱讀:

simpleJdbcTemplate:在查詢以確認狀態很有用。例如, 可能在測試應用程序代碼之前和之後查詢,該應用程序代碼創建對象並使用ORM工具持久保存該對象,以驗證數據庫中是否出現數據 。 (Spring將確保查詢在同一事務的範圍內運行 )。您將需要告訴ORM 工具「刷新」其更改以使其正常工作,例如 使用Hibernate的flush()方法會話界面。

嘗試在執行查詢之前刷新會話。

+0

HQL使用實體名稱而不是SQL表afaik。只是爲了清楚起見,我用小寫字母試了一下,結果是org.hibernate.hql.ast.QuerySyntaxException – Xorty 2011-12-28 17:48:06

+0

在hibernate dao中沖洗沒有幫助: – Xorty 2011-12-29 12:16:36