}
@Test
public void canUseObjectIdAssignedUponInsertInManyToManyAssociation() {
OgmSession session = openSession();
Transaction tx = session.beginTransaction();
// given
Snack nachos = new Snack( "nachos" );
Snack frozenYogurt = new Snack( "frozen yogurt" );
Ingredient milk = new Ingredient( "milk" );
Ingredient salt = new Ingredient( "salt" );
nachos.getIngredients().add( salt );
salt.getContainedIn().add( nachos );
frozenYogurt.getIngredients().add( milk );
milk.getContainedIn().add( frozenYogurt );
frozenYogurt.getIngredients().add( salt );
salt.getContainedIn().add( frozenYogurt );
// when
session.persist( nachos );
session.persist( frozenYogurt );
session.persist( milk );
session.persist( salt );
tx.commit();
session.clear();
tx = session.beginTransaction();
// then
Snack frozenYogurtLoaded = (Snack) session.load( Snack.class, frozenYogurt.getId() );
assertThat( frozenYogurtLoaded.getName() ).isEqualTo( "frozen yogurt" );
assertThat( frozenYogurtLoaded.getIngredients() ).onProperty( "name" ).containsOnly( "salt", "milk" );
tx.commit();
session.clear();
tx = session.beginTransaction();
Ingredient milkLoaded = (Ingredient) session.load( Ingredient.class, milk.getId() );
assertThat( milkLoaded.getName() ).isEqualTo( "milk" );
assertThat( milkLoaded.getContainedIn() ).onProperty( "name" ).containsOnly( "frozen yogurt" );
Ingredient saltLoaded = (Ingredient) session.load( Ingredient.class, salt.getId() );
assertThat( saltLoaded.getName() ).isEqualTo( "salt" );
assertThat( saltLoaded.getContainedIn() ).onProperty( "name" ).containsOnly( "nachos", "frozen yogurt" );
tx.commit();
session.close();
}