public class ManyToManyMain extends Object
NOTE: We use asserts in a couple of places to verify the results but if this were actual production code, we would have proper error handling.
CLASSYTOOLS COMMENTS:
This example shows JPA in action. The application code exempifies use of a standard persistence interface. The OrmLite implemention is mostly hidden in library code, but does show up in named queries where, to keep things lightweight, OrmLite QueryBuilder is employed in place of a JQL implementation @see ManyToManyGenerator.
Also noteable is dependency injection using Dagger @see ManyToManyModule. If one studies the details, what the dependency inject allows is flexibility in 3 ways:
| Modifier and Type | Field and Description |
|---|---|
static String |
POSTS_BY_USER
Named query to find all posts staged by a user identified by ID
|
static String |
PU_NAME
Persistence Unit name to look up configuration details in persistence.xml
|
static String |
USERS_BY_POST
Named query to find all users associated with a post identified by ID
|
| Constructor and Description |
|---|
ManyToManyMain()
Create ManyToManyMain object
This creates and populates the database using JPA, provides verification logic and runs a test from main().
|
| Modifier and Type | Method and Description |
|---|---|
Post |
getPost1()
Returns "Wow is it cold outside!" Post
|
Post |
getPost2()
Returns "Now it's a bit warmer thank goodness." Post
|
User |
getUser1()
Returns Bilbo Baggins' User
|
User |
getUser2()
Returns Gandalf Gray's User
|
static void |
main(String[] args)
Test ManyToMany association
|
void |
setUp()
Populate entity tables.
|
void |
verifyPostsByUser(List<Post> posts)
Verify posts retrieved by "posts_by_user" named query
|
void |
verifyUsersByPost(List<User> usersByPost1,
List<User> usersByPost2)
Verify users retrieved by "users_by_post" named query
|
public static final String POSTS_BY_USER
public static final String USERS_BY_POST
public static final String PU_NAME
public ManyToManyMain()
public static void main(String[] args)
args - Not usedpublic void setUp()
throws InterruptedException
InterruptedExceptionpublic User getUser1()
public User getUser2()
public Post getPost1()
public Post getPost2()
public void verifyPostsByUser(List<Post> posts)
posts - List<Post> retrieved for user1This documentation is licensed by Andrew Bowley under the GPLv3 License.