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
| // 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); |
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
| // 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!"); | |
| } |
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
| // Series.java | |
| @Entity | |
| public class Series extends ManagedEntity implements IManagedEntity | |
| { | |
| @Attribute | |
| @Identifier | |
| public String seriesId; | |
| @Relationship(type = RelationshipType.ONE_TO_MANY, | |
| cascadePolicy = CascadePolicy.ALL, |
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
| // 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 |
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
| // 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)); |
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
| // 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); |
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
| @Entity | |
| public class Book extends ManagedEntity implements IManagedEntity | |
| { | |
| @Identifier(generator = IdentifierGenerator.SEQUENCE) | |
| @Attribute | |
| protected long bookId; | |
| @Attribute | |
| protected String title; |
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
| // 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"); |
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
| // 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); |
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
| @Entity | |
| public class CallLog extends ManagedEntity implements IManagedEntity | |
| { | |
| @Attribute | |
| @Identifier(generator = IdentifierGenerator.SEQUENCE) | |
| protected long callLogId; | |
| @Attribute | |
| protected String destinationNumber; | |