测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/test-dispatcher-servlet.xml"})
public class CityServiceImplIntgTest {
@Autowired
private CityService cityService;
@Test
public void CityServiceTest(){
City city = new City();
city.setCityname("Quebec");
CityAgents cityagents= new CityAgents();
cityagents.setFirstname("max");
cityagents.setLastname("well");
CityAgents cityagentsTwo= new CityAgents();
cityagentsTwo.setFirstname("tipi");
cityagentsTwo.setLastname("well");
Set<CityAgents> agents = new HashSet<CityAgents>();
agents.add(cityagents);
agents.add(cityagentsTwo);
City savedAgents = cityService.cityAgentsOneToMany(city, agents);
Assert.assertEquals("Quebec", savedAgents.getCityname());
}
}
服务层
@Service
public class CityServiceImpl implements CityService{
@Autowired
private CityDAO cityDAO;
@Transactional
public City cityAgentsOneToMany(City city, Set<CityAgents> agent) {
city.setCityagents(agent);
City savedCity = cityDAO.saveCity(city);
return savedCity;
}
}
DAO图层
@Repository
public class CityDAOimpl implements CityDAO{
@Autowired
private SessionFactory sessionFactory;
@Override
public City saveCity(City city) {
sessionFactory.getCurrentSession().save(city);
return city;
}
}
这是我的实体
城市
@Entity
@Table(name="city")
public class City{
@Id
@Column(name="id")
@GeneratedValue
private Integer id;
@Column(name="cityname")
private String cityname;
@OneToMany(mappedBy="city", cascade=CascadeType.ALL)
private Set<CityAgents> cityagents;
public Integer getId() {
return id;
}
}
CityAgent
@Entity
@Table(name="cityagents")
public class CityAgents{
@Id
@GeneratedValue
private Integer id;
private String firstname;
private String lastname;
@ManyToOne
@JoinColumn(name="city_id")
private City city;
}
我的测试用例失败了。我是Hibernamte的新手。谁能帮我吗? 感谢
测试dispacher
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config />
<context:component-scan base-package="com.agilemaple.common" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- <bean id="RegisterBean" class="com.agilemaple.common.dao.impl.RegisterDAOimpl" />
<bean id="RegisterServiceBean" class="com.agilemaple.common.services.impl.RegisterImpl"/>
-->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="jdbc.properties" />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
STACK TRACE
Hibernate: insert into city (cityname) values (?)
Hibernate: insert into cityagents (city_id, firstname, lastname) values (?, ?, ?)
22:50:12,875 DEBUG TransactionInterceptor:406 - Completing transaction for [com.agilemaple.common.services.impl.CityServiceImpl.cityAgentsOneToMany] after exception: org.hibernate.exception.GenericJDBCException: could not insert: [com.agilemaple.common.entity.CityAgents]
22:50:12,875 DEBUG RuleBasedTransactionAttribute:130 - Applying rules to determine whether transaction should rollback on org.hibernate.exception.GenericJDBCException: could not insert: [com.agilemaple.common.entity.CityAgents]
22:50:12,875 DEBUG RuleBasedTransactionAttribute:147 - Winning rollback rule is: null
22:50:12,875 DEBUG RuleBasedTransactionAttribute:152 - No relevant rollback rule found: applying default rules
22:50:12,876 DEBUG HibernateTransactionManager:938 - Triggering beforeCompletion synchronization
22:50:12,876 DEBUG HibernateTransactionManager:843 - Initiating transaction rollback
22:50:12,876 DEBUG HibernateTransactionManager:672 - Rolling back Hibernate transaction on Session [org.hibernate.impl.SessionImpl@1f87a22f]
22:50:12,957 DEBUG HibernateTransactionManager:967 - Triggering afterCompletion synchronization
22:50:12,958 DEBUG TransactionSynchronizationManager:316 - Clearing transaction synchronization
22:50:12,959 DEBUG TransactionSynchronizationManager:229 - Removed value [org.springframework.orm.hibernate3.SessionHolder@53fb35af] for key [org.hibernate.impl.SessionFactoryImpl@6184c65d] from thread [main]
22:50:12,960 DEBUG TransactionSynchronizationManager:229 - Removed value [org.springframework.jdbc.datasource.ConnectionHolder@57a679c5] for key [org.apache.commons.dbcp.BasicDataSource@418794a5] from thread [main]
22:50:12,961 DEBUG HibernateTransactionManager:734 - Closing Hibernate Session [org.hibernate.impl.SessionImpl@1f87a22f] after transaction
22:50:12,961 DEBUG SessionFactoryUtils:789 - Closing Hibernate Session
22:50:12,967 DEBUG TestContextManager:396 - afterTestMethod(): instance [com.agilemaple.common.services.impl.CityServiceImplIntgTest@6d976a55], method [public void com.agilemaple.common.services.impl.CityServiceImplIntgTest.CityServiceTest()], exception [org.hibernate.exception.GenericJDBCException: could not insert: [com.agilemaple.common.entity.CityAgents]]
22:50:12,969 DEBUG DirtiesContextTestExecutionListener:86 - After test method: context [[TestContext@5c03bf22 testClass = CityServiceImplIntgTest, locations = array<String>['classpath:/test-dispatcher-servlet.xml'], testInstance = com.agilemaple.common.services.impl.CityServiceImplIntgTest@6d976a55, testMethod = CityServiceTest@CityServiceImplIntgTest, testException = org.hibernate.exception.GenericJDBCException: could not insert: [com.agilemaple.common.entity.CityAgents]]], class dirties context [false], class mode [null], method dirties context [false].
22:50:12,982 DEBUG TestContextManager:439 - afterTestClass(): class [class com.agilemaple.common.services.impl.CityServiceImplIntgTest]
22:50:12,983 DEBUG DirtiesContextTestExecutionListener:113 - After test class: context [[TestContext@5c03bf22 testClass = CityServiceImplIntgTest, locations = array<String>['classpath:/test-dispatcher-servlet.xml'], testInstance = [null], testMethod = [null], testException = [null]]], dirtiesContext [false].
22:50:12,988 INFO GenericApplicationContext:1002 - Closing org.springframework.context.support.GenericApplicationContext@6ff0ccc2: startup date [Wed May 14 22:50:10 CDT 2014]; root of context hierarchy