View Javadoc

1   package se.citerus.dddsample.infrastructure.persistence.hibernate;
2   
3   import org.springframework.stereotype.Repository;
4   import se.citerus.dddsample.domain.model.cargo.Cargo;
5   import se.citerus.dddsample.domain.model.cargo.CargoRepository;
6   import se.citerus.dddsample.domain.model.cargo.TrackingId;
7   
8   import java.util.List;
9   import java.util.UUID;
10  
11  /**
12   * Hibernate implementation of CargoRepository.
13   */
14  @Repository
15  public class CargoRepositoryHibernate extends HibernateRepository implements CargoRepository {
16  
17    public Cargo find(TrackingId tid) {
18      return (Cargo) getSession().
19        createQuery("from Cargo where trackingId = :tid").
20        setParameter("tid", tid).
21        uniqueResult();
22    }
23  
24    public void store(Cargo cargo) {
25      getSession().saveOrUpdate(cargo);
26      // Delete-orphan does not seem to work correctly when the parent is a component
27      getSession().createSQLQuery("delete from Leg where cargo_id = null").executeUpdate();
28    }
29  
30    public TrackingId nextTrackingId() {
31      // TODO use an actual DB sequence here, UUID is for in-mem
32      final String random = UUID.randomUUID().toString().toUpperCase();
33      return new TrackingId(
34        random.substring(0, random.indexOf("-"))
35      );
36    }
37  
38    public List<Cargo> findAll() {
39      return getSession().createQuery("from Cargo").list();
40    }
41  
42  }