Search in sources :

Example 31 with Album

use of org.neo4j.ogm.domain.music.Album in project neo4j-ogm by neo4j.

the class LoadCapabilityTest method shouldNotBeDirtyOnLoadRelationshipEntityThenSaveThenReload.

// GH-177
@Test
public void shouldNotBeDirtyOnLoadRelationshipEntityThenSaveThenReload() {
    MappingContext context = ((Neo4jSession) session).context();
    Artist pinkFloyd = new Artist("Pink Floyd");
    Album divisionBell = new Album("The Division Bell");
    divisionBell.setArtist(pinkFloyd);
    Studio studio = new Studio("Britannia Row Studios");
    Recording recording = new Recording(divisionBell, studio, 1994);
    divisionBell.setRecording(recording);
    pinkFloyd.addAlbum(divisionBell);
    // new object not saved is always dirty
    assertThat(context.isDirty(recording)).isTrue();
    session.save(recording);
    // object hash updated by save.
    assertThat(context.isDirty(recording)).isFalse();
    // forget everything we've done
    session.clear();
    recording = session.load(Recording.class, recording.getId(), 2);
    // a freshly loaded object is never dirty
    assertThat(context.isDirty(recording)).isFalse();
    recording.setYear(1995);
    // we changed the year so it is now dirty
    assertThat(context.isDirty(recording)).isTrue();
    session.save(recording);
    // object saved, no longer dirty
    assertThat(context.isDirty(recording)).isFalse();
    Recording recording1995 = session.load(Recording.class, recording.getId(), // load the same identity, but to a copy ref
    2);
    // nothing has changed, so it should not be dirty
    assertThat(context.isDirty(recording1995)).isFalse();
    // two refs pointing to the same object
    assertThat(recording == recording1995).isTrue();
}
Also used : MappingContext(org.neo4j.ogm.context.MappingContext) Artist(org.neo4j.ogm.domain.music.Artist) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) Album(org.neo4j.ogm.domain.music.Album) Recording(org.neo4j.ogm.domain.music.Recording) Studio(org.neo4j.ogm.domain.music.Studio) Test(org.junit.Test)

Example 32 with Album

use of org.neo4j.ogm.domain.music.Album in project neo4j-ogm by neo4j.

the class LoadCapabilityTest method shouldBeAbleToLoadEntityToDifferentDepthsInDifferentSessions.

// DATAGRAPH-642, GH-174
@Test
public void shouldBeAbleToLoadEntityToDifferentDepthsInDifferentSessions() {
    Artist led = new Artist("Led Zeppelin");
    Album album = new Album("Led Zeppelin IV");
    Studio studio = new Studio("Island Studios");
    Recording recording = new Recording(album, studio, 1970);
    led.addAlbum(album);
    album.setArtist(led);
    album.setRecording(recording);
    session.save(led);
    session.clear();
    // In the first session, load the artist to depth 2
    Artist ledZeppelin = session.load(Artist.class, led.getId(), 2);
    assertThat(ledZeppelin).isNotNull();
    assertThat(ledZeppelin.getName()).isEqualTo(led.getName());
    assertThat(ledZeppelin.getAlbums()).hasSize(1);
    Album ledZeppelinIV = ledZeppelin.getAlbums().iterator().next();
    assertThat(ledZeppelinIV.getName()).isEqualTo(album.getName());
    assertThat(ledZeppelinIV.getArtist()).isEqualTo(ledZeppelin);
    assertThat(ledZeppelinIV.getRecording()).isNotNull();
    assertThat(ledZeppelinIV.getRecording().getStudio().getName()).isEqualTo(studio.getName());
    // In the second session, load the artist to depth 0
    Session session2 = sessionFactory.openSession();
    Artist ledZeppelin0 = session2.load(Artist.class, led.getId(), 0);
    assertThat(ledZeppelin0).isNotNull();
    assertThat(ledZeppelin0.getName()).isEqualTo(led.getName());
    assertThat(ledZeppelin0.getAlbums()).isEmpty();
}
Also used : Artist(org.neo4j.ogm.domain.music.Artist) Album(org.neo4j.ogm.domain.music.Album) Recording(org.neo4j.ogm.domain.music.Recording) Studio(org.neo4j.ogm.domain.music.Studio) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) Session(org.neo4j.ogm.session.Session) Test(org.junit.Test)

Example 33 with Album

use of org.neo4j.ogm.domain.music.Album in project neo4j-ogm by neo4j.

the class TypeConversionIntegrationTest method convertibleReturnTypesShouldBeHandled.

// GH-71
@Test
public void convertibleReturnTypesShouldBeHandled() {
    final ZoneId utc = ZoneId.of("UTC");
    final Artist queen = new Artist("Queen");
    Album album = new Album("Queen");
    album.setArtist(queen);
    album.setRecordedAt(Date.from(LocalDate.of(1972, 11, 30).atStartOfDay(utc).toInstant()));
    album.setReleased(Date.from(LocalDate.of(1973, 7, 13).atStartOfDay(utc).toInstant()));
    album = new Album("Queen II");
    album.setArtist(queen);
    album.setRecordedAt(Date.from(LocalDate.of(1973, 8, 1).atStartOfDay(utc).toInstant()));
    final Date queen2ReleaseDate = Date.from(LocalDate.of(1974, 3, 8).atStartOfDay(utc).toInstant());
    album.setReleased(queen2ReleaseDate);
    session.save(album);
    session.clear();
    final Date latestReleases = session.queryForObject(Date.class, "MATCH (n:`l'album`) RETURN MAX(n.releasedAt)", new HashMap<>());
    assertThat(latestReleases).isEqualTo(queen2ReleaseDate);
}
Also used : Artist(org.neo4j.ogm.domain.music.Artist) ZoneId(java.time.ZoneId) Album(org.neo4j.ogm.domain.music.Album) Date(java.util.Date) LocalDate(java.time.LocalDate) Test(org.junit.Test)

Aggregations

Album (org.neo4j.ogm.domain.music.Album)33 Test (org.junit.Test)30 Artist (org.neo4j.ogm.domain.music.Artist)27 Recording (org.neo4j.ogm.domain.music.Recording)21 Studio (org.neo4j.ogm.domain.music.Studio)21 Neo4jSession (org.neo4j.ogm.session.Neo4jSession)9 Session (org.neo4j.ogm.session.Session)7 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Before (org.junit.Before)3 Filter (org.neo4j.ogm.cypher.Filter)3 Map (java.util.Map)2 MappingContext (org.neo4j.ogm.context.MappingContext)2 SessionFactory (org.neo4j.ogm.session.SessionFactory)2 Transaction (org.neo4j.ogm.transaction.Transaction)2 LocalDate (java.time.LocalDate)1 ZoneId (java.time.ZoneId)1 Date (java.util.Date)1 EntityGraphMapper (org.neo4j.ogm.context.EntityGraphMapper)1 Filters (org.neo4j.ogm.cypher.Filters)1