|
Hi ,
i am a hibernate and spring newbie and i have some questions regarding how to setup onetomany unidirectional mapping.
i setup a dao and its corresponding test class using spring, hibernate, testng using annotations etc with an app-sevlet.xml that sets up my beans and transactions. I have setup the servlet.xml as follows:
<beans ....>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/my_site_est"/>
<property name="username" value="root"/>
<property name="password" value="iamcool"/>
</bean>
<!-- Database Property -->
<bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.c3p0.minPoolSize">2</prop>
<prop key="hibernate.c3p0.maxPoolSize">5</prop>
<prop key="hibernate.c3p0.timeout">600</prop>
<prop key="hibernate.c3p0.max_statement">50</prop>
<prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
</props>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties" ref="hibernateProperties"/>
<property name="annotatedClasses">
<list>
<value>com.<com.code.sm.tools.dbtest.trafficrestriction.TrafficRestriction</value>
<value>com.code.sm.tools.dbtest.trafficrestriction.TrafficRestrictionResponse</value>
</list>
</property>
</bean>
<bean id="trafficRestrictionDaoTarget" class="<com.code.sm.tools.dbtest.trafficrestriction.TrafficRestrictionDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="trafficRestrictionDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="target" ref="trafficRestrictionDaoTarget"/>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
<property name="proxyInterfaces">
<list>
<value><com.code.sm.tools.dbtest.trafficrestriction.TrafficRestrictionDao</value>
</list>
</property>
</bean>
<beans>
I am using mysql 5.0 database. So let me show how the tables are created for traffic_restriction and traffic_restriction_responses
MySQL Tables:
mysql> show create table traffic_restrictions;
+----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| traffic_restrictions | CREATE TABLE `traffic_restrictions` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(30) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uniquetrafficRestrictionName` (`name`)
)
mysql> show create table traffic_restriction_responses;
| Table | Create Table |
+-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| traffic_restriction_responses | CREATE TABLE `traffic_restriction_responses` (
`id` int(10) unsigned NOT NULL auto_increment,
`traffic_restriction_id` int(10) unsigned default NULL,
`min_thread_count` int(10) unsigned NOT NULL,
`max_thread_count` int(10) unsigned NOT NULL,
`response_time` smallint(5) unsigned NOT NULL,
`response_code` smallint(6) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uniquetrafficRestrictionName` (`traffic_restriction_id`,`response_time`,`response_code`),
CONSTRAINT `traffic_restriction_responses_ibfk_1` FOREIGN KEY (`traffic_restriction_id`) REFERENCES `traffic_restrictions` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
My POJOs for the Entities in question:
@Entity
@org.hibernate.annotations.Entity
@Table(name = "traffic_restrictions")
public class TrafficRestriction implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name="name",unique=true,nullable=false,length=30)
private String name;
@OneToMany(cascade = CascadeType.ALL )
@JoinColumn(name = "traffic_restriction_id", nullable=false)
// Getters and Setters
}
@Entity
@org.hibernate.annotations.Entity
@Table(name = "traffic_restriction_responses")
public class TrafficRestrictionResponse implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "min_thread_count",nullable=false)
private int minThreadCount;
@Column(name = "max_thread_count",nullable=false)
private int maxThreadCount;
@Column(name = "response_code",nullable=false)
private int responseCode;
@Column(name = "response_time",nullable=false)
private int responseTime;
// Getters and Setters
}
My Dao Implementation:
public class TrafficRestrictionDaoImpl extends HibernateDaoSupport implements TrafficRestrictionDao {
public List<TrafficRestriction> getAllTrafficRestrictions() {
return getHibernateTemplate().find("from TrafficRestriction");
public List<TrafficRestrictionResponse> getAllTrafficRestrictionResponses() {
return getHibernateTemplate().find("from TrafficRestrictionResponse");
}
I have a DaoTest using TestNg setup as follows:
@ContextConfiguration(locations={"classpath:app-servlet.xml"})
public class TrafficRestrictionDaoTest extends AbstractTransactionalTestNGSpringContextTests {
@Autowired
@Qualifier("trafficRestrictionDao")
private TrafficRestrictionDao trafficRestrictionDao;
@Test(groups = {"dao"})
public void testGetAllTrafficRestrictions() {
// get All Environments
List<TrafficRestriction> trList = trafficRestrictionDao.getAllTrafficRestrictions();
Assert.assertNotNull(trList);
//Check number of rows
Assert.assertEquals(trList.size(), this.countRowsInTable("traffic_restrictions"),"Size does not match");
//Check if the values match
for (TrafficRestriction tr: trList){
TrafficRestriction trTest = trafficRestrictionDao.getTrafficRestriction(tr.getId());
Assert.assertEquals(trTest.getName(),tr.getName(),"Name mismatch");
Assert.assertEquals(trTest.getTrafficRestrictionResponses().size(), trafficRestrictionDao.getAllTrafficRestrictionResponses(tr.getId()).size(), "Number of Traffic Restriction Responses for a given traffic Restriction mismatch");
}
The area in bold is where my test fails. Basically the failure is that a traffic restriction cannot get list of responses associated with it,so there is something wrong with the way my onetomany mapping is setup.
unfortunately the log doesnt give me information of incorrect mapping etc.
Can anyone please offer suggestions how to setup the onetomany mapping unidirectionally with the primary key.
|