Assignment 2

Assignment 2 is due September 30th, 23:59.

Linking Eclipse to mavens repository

Eclipse needs to get the M2_REPO classpath variable set. There are two ways of doing this:

  1. Run mvn -Declipse.workspace=<path-to-eclipse-workspace> eclipse:add-maven-repo
  2. In Eclipse Preferences, Java->Build Path->Classpath variables, add the M2_REPO pointing to <your home dir>/.m2/repository

Spring and Hibernate configuration

For this assignment it is necessary to use a database, hibernate and springs database support. In the first lecture on hibernate, dependencies and configuration was listed.

Dependencies for spring:

<dependency> 
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId> 
  <version>3.0.4.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>3.0.4.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>3.0.4.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-orm</artifactId>
  <version>3.0.4.RELEASE</version>
</dependency>

and for hibernate and the h2 in-memory db:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>3.5.1-Final</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>3.5.1-Final</version>
</dependency>
<dependency>
  <groupId>geronimo-spec</groupId>
  <artifactId>geronimo-spec-jta</artifactId>
  <version>1.0-M1</version>
</dependency>
<dependency>
  <groupId>c3p0</groupId>
  <artifactId>c3p0</artifactId>
  <version>0.9.1.2</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.5.8</version>
</dependency>
<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <version>1.2.136</version>
</dependency>

Note that it can sometimes be important to have matching versions of dependencies. The dependencies listed here and used in the provided source code doesn't necessarily match, and are not the newest versions of the different libraries, either. As long as you are consistent, things should work fine. 

Remember also that dependecies are transient with maven, so if a library you depend on different versions of libraries you use, you might get different versions of e.g. different spring libraries.

This is the spring config:

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
  <property name="sessionFactory" ref="sessionFactory"/> <property name="dataSource" ref="dataSource"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
  <property name="dataSource" ref="dataSource"/> 
  <property name="mappingResources">
    <list> 
      <value>hibernate/Event.hbm.xml</value> 
      <value>hibernate/Person.hbm.xml</value>
    </list> 
  </property> 
  <property name="hibernateProperties">
    <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop> 
      <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
    </props> 
  </property>
</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 
  <property name="driverClass" value="org.h2.Driver"/> 
  <property name="jdbcUrl" value="jdbc:h2:mem:eventmanager;DB_CLOSE_ON_EXIT=FALSE"/> 
  <property name="user" value="sa"/>
  <property name="password" value=""/> 
</bean>