I was able to get it to work using your entities without a custom query. The benefit of SDN is that it will perform the translation from the database entities to your domain objects for you without implementing code.
As you can see from the test, the profile object was saved using just the repository's generated "save" method and retrieved using the repo's "findById" method. The relationship to the Email object and the Email object were created and retrieved.
Note: I did not know what an Email's PrivacyLevel type was, so I removed it.
Profile Entity:
package com.example.sdndemo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Version;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Relationship;
import org.springframework.data.neo4j.core.support.UUIDStringGenerator;
@Data
@Builder
@Node("Profile")
@NoArgsConstructor
@AllArgsConstructor(staticName = "of")
public class Profile {
@Id
@GeneratedValue(generatorClass = UUIDStringGenerator.class)
private String profileId;
private long userId;
private String bio;
private String firstName;
private String lastName;
private long profilePictureId;
@Version
private long version;
@Relationship(type = "HAS", direction = Relationship.Direction.OUTGOING)
private Email email;
}
Email entity:
package com.example.sdndemo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.support.UUIDStringGenerator;
@Data
@Builder
@Node("Email")
@AllArgsConstructor(staticName = "of")
@NoArgsConstructor
public class Email {
@Id
@GeneratedValue(generatorClass = UUIDStringGenerator.class)
private String mailId;
private String emailValue;
}
Repository:
package com.example.sdndemo;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ProfileRepository extends Neo4jRepository<Profile, String> {
}
Test:
package com.example.sdndemo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Optional;
@SpringBootTest
class SdnDemoApplicationTests {
@Autowired
ProfileRepository profileRepository;
@Test
void testSave() {
Email email = Email.of(null, "mickey@google.com");
Profile profile = Profile.of(
null,
200,
"bio",
"firstName",
"lastName",
1000,
0,
email
);
Profile savedProfile = profileRepository.save(profile);
System.out.println("saved profile: " + profile);
Optional<Profile> queriedProfile = profileRepository.findById(savedProfile.getProfileId());
System.out.println("retrieved profile: " + queriedProfile.get());
}
}
Profile objects printed in the log. Notice it has the Email object.
saved profile: Profile(profileId=122fa05f-d494-4fcf-91a2-bbb04dd2ad33, userId=200, bio=bio, firstName=firstName, lastName=lastName, profilePictureId=1000, version=1, email=Email(mailId=d3442d54-609e-4a66-8b10-3ca79e48ffb3, emailValue=mickey@google.com))
retrieved profile: Profile(profileId=122fa05f-d494-4fcf-91a2-bbb04dd2ad33, userId=200, bio=bio, firstName=firstName, lastName=lastName, profilePictureId=1000, version=1, email=Email(mailId=d3442d54-609e-4a66-8b10-3ca79e48ffb3, emailValue=mickey@google.com))