Created
November 1, 2013 22:13
-
-
Save RichardHightower/7272830 to your computer and use it in GitHub Desktop.
Composite Example for Mr. B
This file contains hidden or 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
import com.examples.model.test.Email; | |
import com.examples.model.test.UserEmail; | |
... | |
import java.util.List; | |
import java.util.Objects; | |
import static org.boon.criteria.CriteriaFactory.eqNested; | |
... | |
putl ("EXAMPLE: Simple Composite Object query example", | |
"See com.examples.model.test", | |
"See Email and UserEmail classes", | |
"__________________________________________" | |
); | |
boolean ok = true; | |
RepoBuilder repoBuilder = Repos.builder (); | |
repoBuilder.usePropertyForAccess ( true ); | |
putl ("The primary key is set to email"); | |
repoBuilder.primaryKey ( "email" ); | |
putl ("For ease of use you can setup nested properties ", | |
"UserEmail.email property is a Email object not a string", | |
"Email.email is a string."); | |
//You can index component objects if you want | |
repoBuilder.nestedIndex ( "email", "email" ); | |
/** Create a repo of type String.class and User.class */ | |
final Repo<Email, UserEmail> userRepo = repoBuilder.build ( Email.class, UserEmail.class ); | |
puts("Adding three test objects for bob, sam and joe "); | |
userRepo.add ( new UserEmail ( "[email protected]" ) ); | |
userRepo.add ( new UserEmail ( "[email protected]" ) ); | |
userRepo.add ( new UserEmail ( "[email protected]" ) ); | |
putl("Query using nested query Repo.eqNested()"); | |
UserEmail bob = (UserEmail) userRepo.results ( eqNested ( "[email protected]", "email", "email" ) ) | |
.expectOne ().firstItem (); | |
ok |= bob.getEmail ().getEmail ().equals ( "[email protected]" ) || die(); | |
putl("Avoid the cast with using nested query Repo.eqNested(UserEmail.class)"); | |
//NOT IN JDK7 Branch yet , but there is a generic version coming | |
bob = userRepo.results ( eqNested ( "[email protected]", "email", "email" ) ) | |
.expectOne (UserEmail.class).firstItem (); | |
ok |= bob.getEmail ().getEmail ().equals ( "[email protected]" ) || die(); | |
Email email = new Email ( "[email protected]" ); | |
bob = (UserEmail) userRepo.results ( eq ( EMAIL, email ) ) | |
.expectOne ().firstItem (); | |
ok |= bob.getEmail ().getEmail ().equals ( "[email protected]" ) || die(); | |
puts("success=", ok); | |
putl("__________________________________________", | |
"__________________________________________", | |
"__________________________________________"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment