Skip to content

Instantly share code, notes, and snippets.

View cosbor11's full-sized avatar
🤓
always coding

Chris Osborn cosbor11

🤓
always coding
View GitHub Profile
@cosbor11
cosbor11 / CascadeDeferExample.java
Created March 27, 2016 05:39
Batch save relationship correlations using saveRelationshipsForEntity
// Associate the actors to the movie. First add the actor unique identifiers to a set //3
Set actorIds = new HashSet<>();
actorIds.add(1);
actorIds.add(2);
manager.saveRelationshipsForEntity(starWarsMovie, "actors", actorIds);
@cosbor11
cosbor11 / CascadeDeferExample.java
Created March 28, 2016 21:40
Verify relationship has been associated
// The Persistence Manager did not save the actors and associated it to the movie
Movie starWarsAfterSave1 = (Movie) manager.findById(Movie.class, starWarsMovie.movieId);
manager.initialize(starWarsAfterSave1, "actors");
if(starWarsAfterSave1.actors.size() == 2) {
System.out.println("Actors have been associated!");
}
@cosbor11
cosbor11 / Series.java
Created March 29, 2016 04:27
Entity definitions for fetch policy example
// Series.java
@Entity
public class Series extends ManagedEntity implements IManagedEntity
{
@Attribute
@Identifier
public String seriesId;
@Relationship(type = RelationshipType.ONE_TO_MANY,
cascadePolicy = CascadePolicy.ALL,
@cosbor11
cosbor11 / FetchPolicyExample.java
Created March 29, 2016 04:32
Populate test data and build out the object graph
// Populate some test data
Series theSopranos = new Series();
theSopranos.seriesId = "SOPRANOS";
Season firstSeason = new Season(1, 1999);
Season secondSeason = new Season(2, 2000);
Season thirdSeason = new Season(3, 2001);
Season fourthSeason = new Season(4, 2002);
// Add the seasons to the sopranos
@cosbor11
cosbor11 / FetchPolicyExample.java
Last active March 29, 2016 04:53
Fetch the season that you recently created
// Fetch a new copy of the entity so that we can illustrate how eager relationships are fetched
Series theSopranosCopy = (Series)manager.findById(Series.class, theSopranos.seriesId);
// Assert seasons which is an eagerly loaded
assertNotNull("Seasons should be populated because it is eagerly fetched", theSopranosCopy.seasons);
assertTrue("Seasons should be fully populated as an ArrayList", (theSopranosCopy.seasons instanceof ArrayList));
@cosbor11
cosbor11 / FetchPolicyExample.java
Last active March 29, 2016 04:52
The episodes should not be null and they are lazily loaded
// Assert the episodes are lazily loaded
firstSeason = theSopranosCopy.seasons.get(0);
assertNotNull("The first seasons' episodes should not be null since it is lazily loaded", firstSeason.episodes);
assertTrue("The first seasons' episodes should be LazyRelationshipCollection", (firstSeason.episodes instanceof LazyRelationshipCollection));
// Notice when I reference an episode it is hydrated
Episode episode = firstSeason.episodes.get(0);
@cosbor11
cosbor11 / Book.java
Created April 4, 2016 02:58
Define a Book entity with an indexed field genre
@Entity
public class Book extends ManagedEntity implements IManagedEntity
{
@Identifier(generator = IdentifierGenerator.SEQUENCE)
@Attribute
protected long bookId;
@Attribute
protected String title;
@cosbor11
cosbor11 / Main.java
Created April 4, 2016 03:00
Crete and save Book data for index example
// Create test data
Book harryPotter = new Book();
harryPotter.setTitle("Harry Potter, Deathly Hallows");
harryPotter.setDescription("Story about a kid that has abnormal creepy powers that seeks revenge on a poor innocent guy named Voldomort.");
harryPotter.setGenre("CHILDREN");
Book theGiver = new Book();
theGiver.setTitle("The Giver");
theGiver.setDescription("Something about a whole community of color blind people.");
theGiver.setGenre("CHILDREN");
@cosbor11
cosbor11 / Main.java
Created April 4, 2016 03:01
Create a Query that predicates on an indexed field, genre
// Create a query to find children's books
// Note: This query has been optimized since the Book#genre attribute is indexed.
QueryCriteria childrenBookCriteria = new QueryCriteria("genre", QueryCriteriaOperator.EQUAL, "CHILDREN");
Query findBooksByGenreQuery = new Query(Book.class, childrenBookCriteria);
List<Book> childrenBooks = manager.executeQuery(findBooksByGenreQuery);
assertEquals("There should be 3 children's books", childrenBooks.size(), 3);
@cosbor11
cosbor11 / CallLog.java
Created April 7, 2016 03:36
Define a CallLog entity and its partition areaCode
@Entity
public class CallLog extends ManagedEntity implements IManagedEntity
{
@Attribute
@Identifier(generator = IdentifierGenerator.SEQUENCE)
protected long callLogId;
@Attribute
protected String destinationNumber;