Przeglądaj źródła

Fixed bug when trying to find a nonexisting cargo.

Patrik Fredriksson 18 lat temu
rodzic
commit
d8847bd9b2

+ 13
- 7
dddsample/src/main/java/se/citerus/dddsample/application/persistence/CargoRepositoryHibernate.java Wyświetl plik

22
     // Query for id and then perform a standard load()
22
     // Query for id and then perform a standard load()
23
     // to use metadata-defined query and lazy proxy access
23
     // to use metadata-defined query and lazy proxy access
24
     Long id = (Long) getSession().
24
     Long id = (Long) getSession().
25
-            createQuery("select id from Cargo where trackingId = :tid").
26
-            setParameter("tid", tid).
27
-            uniqueResult();
25
+       createQuery("select id from Cargo where trackingId = :tid").
26
+       setParameter("tid", tid).
27
+       uniqueResult();
28
+
29
+    if (id != null) {
30
+      return (Cargo) getSession().get(Cargo.class, id);
31
+    } else {
32
+      return null;
33
+    }
34
+
28
 
35
 
29
-    return (Cargo) getSession().load(Cargo.class, id);
30
   }
36
   }
31
 
37
 
32
   public void save(Cargo cargo) {
38
   public void save(Cargo cargo) {
35
     // Delete orphaned itineraries - conceptually the responsibility
41
     // Delete orphaned itineraries - conceptually the responsibility
36
     // of the Cargo aggregate
42
     // of the Cargo aggregate
37
     final List<Itinerary> orphans = getSession().
43
     final List<Itinerary> orphans = getSession().
38
-      createQuery("from Itinerary where cargo = null").
39
-      list();
44
+       createQuery("from Itinerary where cargo = null").
45
+       list();
40
     for (Itinerary orphan : orphans) {
46
     for (Itinerary orphan : orphans) {
41
       getSession().delete(orphan);
47
       getSession().delete(orphan);
42
     }
48
     }
45
   public TrackingId nextTrackingId() {
51
   public TrackingId nextTrackingId() {
46
     final String random = UUID.randomUUID().toString().toUpperCase();
52
     final String random = UUID.randomUUID().toString().toUpperCase();
47
     return new TrackingId(
53
     return new TrackingId(
48
-      random.substring(0, random.indexOf("-"))
54
+       random.substring(0, random.indexOf("-"))
49
     );
55
     );
50
   }
56
   }
51
 
57
 

+ 1
- 1
dddsample/src/main/java/se/citerus/dddsample/domain/model/cargo/CargoRepository.java Wyświetl plik

8
    * Finds a cargo using given id.
8
    * Finds a cargo using given id.
9
    *
9
    *
10
    * @param trackingId Id
10
    * @param trackingId Id
11
-   * @return Cargo
11
+   * @return Cargo if found, else {@code null}
12
    */
12
    */
13
   Cargo find(TrackingId trackingId);
13
   Cargo find(TrackingId trackingId);
14
 
14
 

+ 4
- 0
dddsample/src/test/java/se/citerus/dddsample/application/persistence/CargoRepositoryTest.java Wyświetl plik

85
     assertEquals(cargo, event.cargo());
85
     assertEquals(cargo, event.cargo());
86
   }
86
   }
87
 
87
 
88
+  public void testFindByCargoIdUnknownId() {
89
+    assertNull(cargoRepository.find(new TrackingId("UNKNOWN")));
90
+  }
91
+
88
   private void assertLeg(Leg firstLeg, String cmId, Location expectedFrom, Location expectedTo) {
92
   private void assertLeg(Leg firstLeg, String cmId, Location expectedFrom, Location expectedTo) {
89
     assertEquals(new CarrierMovementId(cmId), firstLeg.carrierMovement().carrierMovementId());
93
     assertEquals(new CarrierMovementId(cmId), firstLeg.carrierMovement().carrierMovementId());
90
     assertEquals(expectedFrom, firstLeg.from());
94
     assertEquals(expectedFrom, firstLeg.from());