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 / UpdateQueryExample.java
Created July 12, 2016 15:31
Create a criteria to filter by position and passingYards
final QueryCriteria positionCriteria = new QueryCriteria("position", QueryCriteriaOperator.EQUAL, "QB");
final QueryCriteria passingCriteria = new QueryCriteria("stats.passingYards", QueryCriteriaOperator.LESS_THAN, 1500);
final QueryCriteria compoundCriteria = positionCriteria.and(passingCriteria);
Query query = new Query(Player.class, compoundCriteria);
@cosbor11
cosbor11 / UpdateIndexDemo.java
Created July 12, 2016 02:16
Manually trigger the rebuilding of an index
final EntityDescriptor entityDescriptor = persistenceManager.getContext().getDescriptorForEntity(Payment.class, "");
final IndexDescriptor indexDescriptor = entityDescriptor.getIndexes().get("notes");
final IndexController indexController = persistenceManager.getContext().getIndexController(indexDescriptor);
indexController.rebuild();
@cosbor11
cosbor11 / UpdateIndexDemo.java
Created July 12, 2016 02:15
Query an indexed field
final Query indexedQuery = new Query(Invoice.class, new QueryCriteria("dueDate", QueryCriteriaOperator.EQUAL, parseDate("04-01-2016")));
final List<Invoice> invoices = persistenceManager.executeQuery(indexedQuery);
// First pass may not give you 2 results since the index could still be rebuilding. After it is done re-indexing you should have 2 results.
assert invoices.size() == 2;
@cosbor11
cosbor11 / UpdateIndexDemo.java
Last active July 12, 2016 02:14
Query a non indexed field amount
final Query nonIndexedQuery = new Query(Invoice.class, new QueryCriteria("amount", QueryCriteriaOperator.EQUAL, 44.32));
List<Payment> payments = persistenceManager.executeQuery(nonIndexedQuery);
// See that we still get results by doing the full table scan.
assert payments.size() == 2;
@cosbor11
cosbor11 / Payment.java
Created July 12, 2016 02:13
Modify Payment Indexes
@Entity
public class Payment extends ManagedEntity implements IManagedEntity
{
@Attribute
@Identifier
protected long paymentId;
@Relationship(type = RelationshipType.MANY_TO_ONE,
inverse = "payments",
inverseClass = Invoice.class)
@cosbor11
cosbor11 / UpdateRelationshipDemo.java
Created July 7, 2016 14:48
Verify you can associate multiple payments after updating the data model
final Invoice myLatestInvoice = (Invoice)persistenceManager.findById(Invoice.class, 1l);
// This used to be a single entity and now it is a list of payments since the relationship has changed from a One To One
// to a One To Many. This is also handled by the light weight migration.
List<Payment> paymentList = myLatestInvoice.getPayments();
// What previously was a one to one relationship should have the existing record in the set.
assert paymentList.size() == 1;
assert paymentList.get(0).getPaymentId() == 1;
@cosbor11
cosbor11 / Invoice.java
Created July 7, 2016 14:44
Modify the Invoice entity to have a relationship type of ONE_TO_MANY
@Relationship(type = RelationshipType.ONE_TO_MANY,
inverse = "invoice",
inverseClass = Payment.class,
cascadePolicy = CascadePolicy.SAVE,
fetchPolicy = FetchPolicy.EAGER)
protected List<Payment> payments;
@cosbor11
cosbor11 / Payment.java
Created July 7, 2016 14:43
Modify the Payment entity to have a relationship type of MANY_TO_ONE
@Relationship(type = RelationshipType.MANY_TO_ONE,
inverse = "payments",
inverseClass = Invoice.class)
protected Invoice invoice;
@cosbor11
cosbor11 / UpdateIdentifierDemo.java
Created July 7, 2016 14:15
Retrive an Account using a long value and verify the generator is no longer used.
// Fetch an account. Notice that the id is now a long rather than an integer.
Account account = (Account)persistenceManager.findById(Account.class, 1l);
assert account.getAccountId() == 1l;
// This example creates a new account and attempts to save it without an identifier.
Account account2 = new Account();
persistenceManager.saveEntity(account2);
// The account ID was not auto incremented.
// This is because we removed the generator from the @Identifier annotation.
List<Player> inactivePlayers = manager.executeQuery(query);
// There should only be one inactivePlayer: Calvin Johnson
for (final Player player : inactivePlayers)
{
System.out.println(player.getFirstName() + " " + player.getLastName() + " is not active.");
}
manager.executeDelete(query);