2011-12-28 66 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