I can't retrieve relationships - spring-data-neo4j

I have a Person node and a call relationship.I want to find a specific person and those who called him.My code are as below
#NodeEntity
public class Person extends BaseEntity{
#Property(name = "id")
private String mobile;
private String name;
private int partition;
private int StronglyConnectedComponents;
private int ConnectedComponent;
private int LabelPropagation;
private double pagerank;
private int seed_label; //在线算法结果写回字段
#Relationship(type="Call",direction=Relationship.OUTGOING)
private List<Person> contact;
//setter and getter
}
#RelationshipEntity(type = "Call")
public class Call extends BaseEntity{
#StartNode
private Person caller;
#EndNode
private Person callee;
private String BS;
private String time;
//setter and getter
}
#Repository
public interface PersonRepository extends GraphRepository<Person>{
Person findById(String id, #Depth int depth);
}
public String test() {
Person person = community.personRepository.findById("18565124452",2);
return person.toString();
}
Use test method I can retrieve the properties of the person node but the relationship property contact is null. How can I fix this?

Looks like an older Spring Data Neo4j version. But this shouldn't be a problem here. In your person class you are defining
#Relationship(type="Call",direction=Relationship.OUTGOING)
private List<Person> contact;
where it should be
#Relationship(type="Call",direction=Relationship.OUTGOING)
private List<Call> contact;

I had the same problem with null relationships. I think the only way to fetch relationships is by using cypher query language. Correct the mistake meistermeir point out. Try adding this method to your repository class.
#Query("MATCH (p:Person) WHERE ID(p)=18565124452 MATCH (p)-[call:Call*]->(p2) return p, call, p2")
Person getPersonById(long personId);
Also, I think you need to change :
#Property(name = "id")
to
#Id
#GeneratedValue
private Long id;
in order to generate unique id's every time you create new Person nodes.

Related

Mockito test when(repository.findById()).thenReturn(object with custom attributes)

So I was writing a couple of unit tests, some of them were made for testing missing attributes of Document. When using of when(repository.findOne(id)).thenReturn(DOCUMENT_WITHOUT_SOMETHING), those DOCUMENT_WITHOUT_SOMETHING were final objects with predefined attributes, depending on test cases.
Let's say, there could be a document without name, so when loading document by id the method was returning null with some log message. So I created a class of test data DocumentServiceTestData, where those necessary objects and attributes were declared. Is it a good approach or you suggest something more clear, without need of making new class?
I will try to write a simplified code of what I did.
Document
public class Document {
private String name;
private String applicant;
private String sign;
private boolean signed;
public boolean isSigned() {return signed;}
public String getApplicant() {return applicant;}
public String getName() {return name;}
public String getSign() {return sign;}
public void setSigned(boolean signed) {this.signed = signed;}
public void setApplicant(String applicant) {this.applicant = applicant;}
public void setName(String name) {this.name = name;}
public void setSign(String sign) {this.sign = sign;}
}
DocumentService
#Service
public class DocumentService {
private static final Logger LOG = LoggerFactory.getLogger(DocumentService.class);
private DocumentRepository documentRepository;
public Document loadDocument(Long docId) {
Document document = documentRepository.findByIdOrFail(docId);
if(document.getName() == null) {
LOG.info("There is no name for document, returning null");
return null;
}
if(document.getSign() == null) {
LOG.info("The document is not signed, returning null");
return null;
}
return document;
}
}
DocumentServiceTestData
public class DocumentServiceTestData {
public static final Long DOC_ID = 1L;
public static final String APPLICANT = "Applicant";
public static final String DOC_NAME = "Document";
public static final String SIGN = "Sign";
public static final Document DOCUMENT_WITHOUT_NAME;
public static final Document DOCUMENT_WITHOUT_SIGN;
static {
DOCUMENT_WITHOUT_NAME = makeDocument(null, APPLICANT, SIGN, true);
DOCUMENT_WITHOUT_SIGN = makeDocument(DOC_NAME, APPLICANT, null, false);
}
private static Document makeDocument(String name, String applicant, String sign, boolean signed) {
Document document = new Document();
document.setName(name);
document.setApplicant(applicant);
document.setSign(sign);
document.setSigned(signed);
return document;
}
}
DocumentServiceTest
#RunWith(MockitoJUnitRunner.class)
#Category(MockTest.class)
public class DocumentServiceUnitTest {
#InjectMocks
private DocumentService service;
#Mock
private DocumentRepository documentRepository;
#Test
public void testLoadDocument_notSigned(){
when(documentRepository.findByIdOrFail(DOC_ID)).thenReturn(DOCUMENT_WITHOUT_SIGN);
Document document = service.loadDocument(DOC_ID);
Assert.assertNull(document);
}
#Test
public void testLoadDocument_notNamed(){
when(documentRepository.findByIdOrFail(DOC_ID)).thenReturn(DOCUMENT_WITHOUT_NAME);
Document document = service.loadDocument(DOC_ID);
Assert.assertNull(document);
}
}
To sum up, I would like to know - if there is another way to test those cases, when I need to return specified objects while calling repository.findById(id).
PS: keep in mind that this is very simplified, those tests I've made were much more complex. If there is any information missing please let me know, I will try to clarify things. Thanks

How can i access attributes from another class with pointer

How can i access the lastname with the pointer in Class 2
class Person
{
public:
Person(string firstname, string lastname);
private:
string firstname;
string lastname;
};
My Class 2
class Account
{
public: Person getPersonName();
private: const Person* person_;
}
.cpp file where lastname should be returned
Person Account::getPersonName()
{
return person_?;
}
You can't access private member of a class outside the class that contain it.
So in this case you can't access them directly.
However you can create a public getter in Person Class for the member lastname and then call it in the Person Account::getPersonName().
In alternative you can define lastname public ( this approch is not recommended ).
Another way is to use friend class. I leave you a useful link:
Friend Class

How to parcel cascading classes with Parceler?

I can parcel some models using Parceler like this:
#Parcel(Serialization.BEAN)
public class PasswordSetModel {
private String mPassword;
private String mRepetition;
/* Getter & Setter */
...
But if this class is part of another class, the mechanism does not work. I am getting an NPE for mPasswordSetModel. Creating an instance in an constructor did not work, because the members mPassword and mRepetition were null after unparcelling.
#Parcel
public class RegistrationModel {
private PasswordSetModel mPasswordSetModel;
/* Getter & Setter */
...
So how can I parcel this using Parceler?
okay, the problem was that I uses "wrong" setter methods. In order to use fluent interface style I made it this way:
public String getPassword() {
return mPassword;
}
public PasswordSetModel setPassword(String password) {
mPassword = password;
return this;
}
public String getRepetition() {
return mRepetition;
}
public PasswordSetModel setRepetition(String repetition) {
mRepetition = repetition;
return this;
}
It seems that the setters were now found and therefor the model was NULL

Java EE / Objectify get a list filtered by another using ofy()

My classes:
#Entity
#Cache
#Index
public class Log {
#Id Long id;
Ref<Site> site;
Ref<User> user;
Ref<Computer> computer;
Ref<License> license;
private String date_in;
private String date_out;
private boolean active;
}
#Entity
#Cache
#Index
public class License {
#Id Long id;
private String name;
private String startDate;
private String expDate;
private int timeStamp;
private int status;
private int offline;
private String identification;
Ref<Daemon> daemon;
private boolean active;
public enum Type {Country, Site, User, Computer, Lic};
}
I don't get why this is not working:
ofy().load().type(Log.class)
.filter("license in",
ofy().load().type(License.class).filter("name",licName))
.list();
or this too:
ofy().load().type(Log.class)
.filter("license in",
ofy().load().type(License.class).filter("name",licName).list())
.list();
My License is a reference in my Log. Can I do it using ofy() ?
Can anyone explain me?

jersey - using an object with a list of super Classes containing Objects of several sub classes - sub classes are not unmarshalled correctly

I have a QuestionsData class that has a List of QuestionData.
The QuestionData is an abstract class and has two implementations: TextQuestionData and SelectionQuestionData.
The problem is that after doing clientResponse.getEntity, I get the object with a list of SelectionQuestionData only, while I know that some of the questions are of type TextQuestionData.
I tried to add #XmlSeeAlso, but it did not help.
I also tried to change the order of elements in the #XmlElementRefs but that caused all the questions to be of type TextQuestionData.
I don't know if this is relevant or not, but the object I use in jersey is another Jaxb Object that has QuestionsData as a member
Here is the code:
#XmlRootElement(name = "questions")
#XmlAccessorType(XmlAccessType.FIELD)
#XmlSeeAlso({SelectionQuestionData.class, TextQuestionData.class })
public class QuestionsData {
#XmlElementRefs({#XmlElementRef(type = TextQuestionData.class), #XmlElementRef(type = SelectionQuestionData.class)})
private List<QuestionData> questions;
private QuestionsData() {}
public QuestionsData(List<QuestionData> questions) {
this.questions = questions;
}
}
#XmlRootElement(name = "question")
#XmlAccessorType(XmlAccessType.FIELD)
public class TextQuestionData extends QuestionData {
#XmlElement
private String someString;
public TextQuestionData() {}
}
#XmlRootElement(name = "question")
#XmlAccessorType(XmlAccessType.FIELD)
public class SelectionQuestionData extends QuestionData {
#XmlElements({#XmlElement(name = "option")})
private List<String> options;
public SelectionQuestionData() {}
}
In this use case the element name is used to determine which subclass (mapped with #XmlRootElement) should be instantiated during unmarshalling. Since you mapped both subclasses to question the JAXB (JSR-222) implementation can not determine the correct one to unmarshal. You will need to map them to different root elements.
TextQuestionData
#XmlRootElement(name = "textQuestion")
#XmlAccessorType(XmlAccessType.FIELD)
public class TextQuestionData extends QuestionData {
#XmlElement
private String someString;
public TextQuestionData() {}
}
SelectionQuestionData
#XmlRootElement(name = "selectionQuestion")
#XmlAccessorType(XmlAccessType.FIELD)
public class SelectionQuestionData extends QuestionData {
#XmlElements({#XmlElement(name = "option")})
private List<String> options;
public SelectionQuestionData() {}
}
For More Information
http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html