ItEr26S07CUAsignacionGrupoRecursosAPlanificacionItEr25S07: Adding hibernate UserType to store and retrieve ResourcesPerDay

This commit is contained in:
Óscar González Fernández 2009-09-14 16:49:45 +02:00
parent 68e661433e
commit d42b3d670b
4 changed files with 186 additions and 0 deletions

View file

@ -0,0 +1,95 @@
package org.navalplanner.business.planner.entities.hibernate;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
import org.navalplanner.business.planner.entities.ResourcesPerDay;
/**
* Persists a {@link ResourcesPerDay} through hibernate
* @author Óscar González Fernández <ogonzalez@igalia.com>
*/
public class ResourcesPerDayType implements UserType {
private static final int[] SQL_TYPES = { Types.NUMERIC };
@Override
public int[] sqlTypes() {
return SQL_TYPES;
}
@Override
public Object assemble(Serializable cached, Object owner)
throws HibernateException {
return ResourcesPerDay.amount((BigDecimal) cached);
}
@Override
public Serializable disassemble(Object value) throws HibernateException {
ResourcesPerDay resourcesPerDay = (ResourcesPerDay) value;
return resourcesPerDay.getAmount();
}
@Override
public Object deepCopy(Object value) throws HibernateException {
return value;
}
@Override
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y)
return true;
if (x == null || y == null)
return false;
return x.equals(y);
}
@Override
public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}
@Override
public boolean isMutable() {
return false;
}
@Override
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
BigDecimal bigDecimal = (BigDecimal) Hibernate.BIG_DECIMAL.nullSafeGet(
rs, names[0]);
if (bigDecimal == null)
return null;
return ResourcesPerDay.amount(bigDecimal);
}
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
BigDecimal amount = null;
if (value != null) {
amount = ((ResourcesPerDay) value).getAmount();
}
Hibernate.BIG_DECIMAL.nullSafeSet(st, amount, index);
}
@Override
public Object replace(Object original, Object target, Object owner)
throws HibernateException {
return original;
}
@Override
public Class returnedClass() {
return ResourcesPerDay.class;
}
}

View file

@ -0,0 +1,27 @@
package org.navalplanner.business.test.planner.entities.hibernate;
import org.navalplanner.business.planner.entities.ResourcesPerDay;
public class EntityContainingResourcePerDay {
private Long id;
private ResourcesPerDay resourcesPerDay;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public ResourcesPerDay getResourcesPerDay() {
return resourcesPerDay;
}
public void setResourcesPerDay(ResourcesPerDay resourcesPerDay) {
this.resourcesPerDay = resourcesPerDay;
}
}

View file

@ -0,0 +1,56 @@
package org.navalplanner.business.test.planner.entities.hibernate;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertThat;
import static org.navalplanner.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE;
import static org.navalplanner.business.test.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_TEST_FILE;
import java.math.BigDecimal;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.navalplanner.business.planner.entities.ResourcesPerDay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { BUSINESS_SPRING_CONFIG_FILE,
BUSINESS_SPRING_CONFIG_TEST_FILE })
@Transactional
public class ResourcesPerDayTypeTest {
@Autowired
private SessionFactory sessionFactory;
private EntityContainingResourcePerDay entity;
private Session getSession() {
return sessionFactory.getCurrentSession();
}
private void givenEntity(ResourcesPerDay resourcesPerDay) {
this.entity = new EntityContainingResourcePerDay();
this.entity.setResourcesPerDay(resourcesPerDay);
}
@Test
public void canBeSavedAndRetrieved() {
ResourcesPerDay resourcesPerDay = ResourcesPerDay
.amount(new BigDecimal(2.7));
givenEntity(resourcesPerDay);
getSession().save(entity);
getSession().flush();
getSession().evict(entity);
EntityContainingResourcePerDay reloaded = (EntityContainingResourcePerDay) getSession()
.get(EntityContainingResourcePerDay.class,
entity.getId());
assertNotSame(reloaded.getResourcesPerDay(), resourcesPerDay);
assertThat(reloaded.getResourcesPerDay(), equalTo(resourcesPerDay));
}
}

View file

@ -36,4 +36,12 @@
<version name="version" type="long"/>
<property name="timeQuantity" type="org.navalplanner.business.common.partialtime.hibernate.TimeQuantityType"></property>
</class>
<class name="org.navalplanner.business.test.planner.entities.hibernate.EntityContainingResourcePerDay">
<id name="id">
<generator class="hilo">
<param name="max_lo">100</param>
</generator>
</id>
<property name="resourcesPerDay" type="org.navalplanner.business.planner.entities.hibernate.ResourcesPerDayType"></property>
</class>
</hibernate-mapping>