-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example for #208 - Multiple unidirectional inverse relations
- Loading branch information
Showing
5 changed files
with
189 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
sculptor-examples/DDDSample/src/main/resources/model-relations.btdesign
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
ApplicationPart RelationPart { | ||
|
||
Module relation { | ||
"Testing entity for different type of relations" | ||
Entity Person { | ||
scaffold | ||
|
||
String first; | ||
String secondName; | ||
|
||
// 1:N with backref | ||
- List<@House> owning <-> owner inverse; | ||
- Set<@House> related <-> relation inverse; | ||
- Bag<@House> other <-> something inverse; | ||
|
||
// 1:N without backref | ||
- List<@House> owningNoRel inverse; | ||
- Set<@House> relatedNoRel inverse; | ||
- Bag<@House> otherNoRel inverse; | ||
|
||
// N:M with backref | ||
- List<@House> owningN <-> ownerN; | ||
- Set<@House> relatedN <-> relationN; | ||
- Bag<@House> otherN <-> somethingN; | ||
|
||
// N:M without backref | ||
- List<@House> owningNoRelN; | ||
- Set<@House> relatedNoRelN; | ||
- Bag<@House> otherNoRelN; | ||
} | ||
|
||
Entity House { | ||
String street; | ||
String number; | ||
String town; | ||
String state; | ||
String zipCode; | ||
Integer landFieldSize; | ||
Integer houseFootprint; | ||
Integer numberOfFloors; | ||
- @Person owner <-> owning; | ||
- @Person relation <-> related; | ||
- @Person something <-> other; | ||
|
||
- Set<@Person> ownerN <-> owningN; | ||
- List<@Person> relationN <-> relatedN; | ||
- Bag<@Person> somethingN <-> otherN; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...DDDSample/src/test/java/org/sculptor/dddsample/relation/serviceapi/PersonServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package org.sculptor.dddsample.relation.serviceapi; | ||
|
||
import org.junit.Test; | ||
import org.sculptor.dddsample.relation.domain.House; | ||
import org.sculptor.dddsample.relation.domain.Person; | ||
import org.sculptor.dddsample.relation.domain.PersonRepository; | ||
import org.sculptor.dddsample.relation.serviceapi.PersonService; | ||
import org.sculptor.framework.test.AbstractDbUnitJpaTests; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
|
||
/** | ||
* Spring based transactional test with DbUnit support. | ||
*/ | ||
public class PersonServiceTest extends AbstractDbUnitJpaTests implements PersonServiceTestBase { | ||
|
||
@Autowired | ||
private PersonService personService; | ||
|
||
@PersistenceContext(unitName = "DDDSampleEntityManagerFactory") | ||
private EntityManager entityManager; | ||
|
||
@Override | ||
protected String getDataSetFile() { | ||
return "dbunit/TestData.xml"; | ||
} | ||
|
||
@Test | ||
public void testFindById() throws Exception { | ||
Person feromon = personService.findById(getServiceContext(), 103l); | ||
assertEquals("Feromon", feromon.getFirst()); | ||
assertEquals("Smradoch", feromon.getSecondName()); | ||
} | ||
|
||
@Test | ||
public void testFindAll() throws Exception { | ||
List<Person> all = personService.findAll(getServiceContext()); | ||
assertEquals(4, all.size()); | ||
boolean was101 = false; | ||
for (Person p : all) { | ||
if (p.getId().equals(101l)) { | ||
Collection<House> owningNoRel = p.getOwningNoRel(); | ||
assertEquals(3, owningNoRel.size()); | ||
Collection<House> relatedNoRel = p.getRelatedNoRel(); | ||
assertEquals(3, relatedNoRel.size()); | ||
Collection<House> otherNoRel = p.getOtherNoRel(); | ||
assertEquals(3, otherNoRel.size()); | ||
was101 = true; | ||
} | ||
} | ||
assertTrue(was101); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void testSave() throws Exception { | ||
Person p = new Person(); | ||
p.setFirst("Newman"); | ||
p.setSecondName("Elemental"); | ||
House firstHouse = new House(); | ||
firstHouse.setStreet("Atomic"); | ||
firstHouse.setNumber("1123"); | ||
firstHouse.setNumberOfFloors(2); | ||
firstHouse.setOwner(p); | ||
firstHouse.setRelation(p); | ||
firstHouse.setSomething(p); | ||
firstHouse.setTown("Plymouth"); | ||
firstHouse.setZipCode("A-923754"); | ||
firstHouse.setState("Small Britain"); | ||
firstHouse.setHouseFootprint(123); | ||
firstHouse.setLandFieldSize(432); | ||
House secondHouse = new House(); | ||
secondHouse.setStreet("Quark"); | ||
secondHouse.setNumber("893"); | ||
secondHouse.setNumberOfFloors(1); | ||
secondHouse.setOwner(p); | ||
secondHouse.setRelation(p); | ||
secondHouse.setSomething(p); | ||
secondHouse.setTown("Cardiff"); | ||
secondHouse.setZipCode("B-3478"); | ||
secondHouse.setState("Small Britain"); | ||
secondHouse.setHouseFootprint(947); | ||
secondHouse.setLandFieldSize(1837); | ||
|
||
p.addOwningNoRel(firstHouse); | ||
p.addOwningNoRel(secondHouse); | ||
Person stored = personService.save(getServiceContext(), p); | ||
entityManager.clear(); | ||
|
||
// All will have 2 items not only OwningNoRel | ||
Person fetched = personService.findById(getServiceContext(), stored.getId()); | ||
|
||
Collection<House> owningNoRel = fetched.getOwningNoRel(); | ||
assertEquals(2, owningNoRel.size()); | ||
|
||
// This is WRONG - should be 0 | ||
Collection<House> relatedNoRel = fetched.getRelatedNoRel(); | ||
assertEquals(2, relatedNoRel.size()); | ||
|
||
// This is WRONG - should be 0 | ||
Collection<House> otherNoRel = fetched.getOtherNoRel(); | ||
assertEquals(2, otherNoRel.size()); | ||
} | ||
|
||
@Override | ||
public void testDelete() throws Exception { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters