Hardcoding values in Java - java

Currently I'm trying to Hardcode some values for a family tree application in Java.
Just need some ideas on how to go about it, not sure how to proceed.
I have also included the constructor classes:
public class TreeImpl {
private FamilyMember root;
public FamilyMember getRoot() {
return root;
}
public void setRoot(FamilyMember root) {
this.root = root;
}
public List<FamilyMember> getAllMembers() {
return allMembers;
}
public void setAllMembers(List<FamilyMember> allMembers) {
this.allMembers = allMembers;
this.setRoot(this.allMembers.get(0));
}
private List<FamilyMember> allMembers = new ArrayList<>();
public TreeImpl(FamilyMember root)
{
this.root=root;
addMembers(root);
}
private void addMembers(FamilyMember node)
{
if(node==null) return;
allMembers.add(node);
addMembers(node.getFather());
addMembers(node.getMother());
addMembers(node.getSpouse());
for(FamilyMember child : node.getChildren())
addMembers(child);
}
public void addSpouse(int index, FamilyMember spouse)
{
for(FamilyMember member : allMembers)
{
if(member.getMemberID()==index)
{
member.setSpouse(spouse);
allMembers.add(spouse);
return;
}
}
}
public void addFather(int index, FamilyMember father)
{
for(FamilyMember member : allMembers)
{
if(member.getMemberID()==index)
{
member.setFather(father);
allMembers.add(father);
return;
}
}
}
public void addMother(int index, FamilyMember mother)
{
for(FamilyMember member : allMembers)
{
if(member.getMemberID()==index)
{
member.setMother(mother);
allMembers.add(mother);
return;
}
}
}
public void addChild(int index, FamilyMember child)
{
for(FamilyMember member : allMembers)
{
if(member.getMemberID()==index)
{
member.setSpouse(child);
allMembers.add(child);
return;
}
}
}
public FamilyMember getDetailsForMember(String member)
{
for(FamilyMember m : this.getAllMembers())
{
if(m.getFirstName().equals(member))
return m;
}
return null;
}
My constructor class
public class FamilyMember implements Serializable{
private static int id=1;
private String firstName, surName, surNameAfterMarriage, life;
private Gender gender;
private Address address;
private FamilyMember mother, father, spouse;
private int memberID;
public boolean hasSub()
{
return (this.getFather()!=null || this.getMother()!=null || this.getChildren().size()>0);
}
public int getMemberID() {
return memberID;
}
public void setMemberID(int memberID) {
this.memberID = memberID;
}
private List<FamilyMember> children, grandChildren;
public static int getId() {
return id;
}
public static void incrementId()
{
id++;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSurName() {
return surName;
}
public void setSurName(String surName) {
this.surName = surName;
}
public String getSurNameAfterMarriage() {
return surNameAfterMarriage;
}
public void setSurNameAfterMarriage(String surNameAfterMarriage) {
this.surNameAfterMarriage = surNameAfterMarriage;
}
public String getLife() {
return life;
}
public void setLife(String life) {
this.life = life;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public FamilyMember getMother() {
return mother;
}
public void setMother(FamilyMember mother) {
this.mother = mother;
}
public FamilyMember getFather() {
return father;
}
public void setFather(FamilyMember father) {
this.father = father;
}
public FamilyMember(String firstName, String surName, String surNameAfterMarriage, String life, Gender gender,
Address address) {
super();
this.firstName = firstName;
this.surName = surName;
this.surNameAfterMarriage = surNameAfterMarriage;
this.life = life;
this.gender = gender;
this.address = address;
this.memberID = this.getId();
this.children = new ArrayList<>();
this.incrementId();
}
for(FamilyMember child : this.getChildren())
{
text = text + " " + child.firstName;
}
return text;
}
public FamilyMember getSpouse() {
return spouse;
}
public void setSpouse(FamilyMember spouse) {
this.spouse = spouse;
}
public List<FamilyMember> getChildren() {
return children;
}
public void setChildren(List<FamilyMember> childer) {
this.children = childer;
}
public void addChild(FamilyMember child) {
this.children.add(child);
}
public List<FamilyMember> getGrandChildren() {
return grandChildren;
}
public void setGrandChildren(List<FamilyMember> grandChildren) {
this.grandChildren = grandChildren;
}
}
For examples, I want to add details for father, mothers, child etc so when I run the program, these values are displayed
Eg: Familymember father = new Familymember ("xyz". "xyx" )

You can set values using Constructor,
public FamilyMember (Gender gen,Address add,FamilyMember fmm,FamilyMember fmf,FamilyMember fms,int mId){
this.gender = gen;
this.address = add;
this.mother = fmm;
this.father = fmf;
this.spouse = fms;
this.memberID = mId;
}
it will set the FamilyMember object with hard coded values,
FamilyMember fm = new FamilyMember(Gender.MALE,add,fmm,fmf,fms,menId);
Let me know if any quires.

If you really want it through a constructor, you could do it this way:
// Secondary constructor
public FamilyMember() {
this("Paul", "Stevenson", ...); // calls the primary constructor
}
public FamilyMember(String firstName, String surName, String surNameAfterMarriage, String life, Gender gender,
Address address) {
// your primary constructor
}
Unfortunately this isn't very flexible. An alternative is to use a static method :
public static FamilyMember familyMemberSteve() {
return new FamilyMember("Steve", ...)
}
But to be honest, I wouldn't recommend this either. Like I said in the comment, it would be better to use XML or another format, because it would be more flexible.

Create a a static method in FamilyMember class that creates a family and then call this method before initialising a TreeImpl object.
public static FamilyMember createFirstFamilyNode() {
FamilyMember mother = new FamilyMember("Mother", ...);
FamilyMember father = new FamilyMember("Father", ...);
mother.setSpouce(father);
FamilyMember child1 = new FamilyMember("First Child", ...);
mother.addChild(child1);
// And so on for rest of family members
return mother;
}

Related

Stack overflow error while invoking toString method

this is my code but when I execute the program I get many errors and I don't know why. May anyone help me?
RegisteredUser.java
public class RegisteredUser {
private String nickname;
ArrayList<ReviewDAO> reviews;
public RegisteredUser(String nickname) {
this.nickname = nickname;
reviews = new ArrayList<>();
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public void addReview(ReviewDAO review) {
if (!this.reviews.contains(review)) {
this.reviews.add(review);
review.addRegisteredUser(this);
}
}
#Override
public String toString() {
return "RegisteredUser{" +
"nickname='" + nickname + '\'' +
", reviews=" + reviews +
'}';
}
}
ReviewDAO.java
public abstract class ReviewDAO {
RegisteredUser registeredUser;
public abstract boolean write(Review review);
public void addRegisteredUser(RegisteredUser registeredUser) {
this.registeredUser = registeredUser;
}
#Override
public String toString() {
return "ReviewDAO{" +
"registeredUser=" + registeredUser +
'}';
}
}
Review.java
public class Review {
private String title;
private String description;
private int rank;
private boolean isAnonymous;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public boolean isAnonymous() {
return isAnonymous;
}
public void setAnonymous(boolean anonymous) {
isAnonymous = anonymous;
}
}
ReviewDAO_MongoDB.java
public class ReviewDAO_MongoDB extends ReviewDAO {
#Override
public boolean write(Review review) {
return false;
// todo
}
}
ReviewDAO_Factory.java
public class ReviewDAO_Factory {
public ReviewDAO getReviewDAO(String technology) throws ExecutionControl.NotImplementedException {
if (technology.equals("mongodb"))
return new ReviewDAO_MongoDB();
else
throw new ExecutionControl.NotImplementedException("");
}
}
BusinessLogic.java
public class BusinessLogic {
public static void main(String[] args) throws ExecutionControl.NotImplementedException {
ReviewDAO_Factory reviewDAO_factory = new ReviewDAO_Factory();
RegisteredUser registeredUser = new RegisteredUser("Alessandro");
registeredUser.addReview(reviewDAO_factory.getReviewDAO("mongodb"));
System.out.println(registeredUser.toString());
}
}
I am getting
Exception in thread "main" java.lang.StackOverflowError
at RegisteredUser.toString(RegisteredUser.java:33)
at java.base/java.lang.String.valueOf(String.java:2951)
at ReviewDAO.toString(ReviewDAO.java:15)
...
Process finished with exit code 1
errors
First of all, the design looks faulty. You should never mix POJOs with DAOs. DAOs are "data access object" classes which deals with CRUD operations on the POJOs. Here you have 2 POJOs - Review and RegisteredUser. It's the responsibility of ReviewDAO to perform CRUD operations on managed/unmanaged instances (or entities) of Review.
And for RegisteredUser also you need another POJO probably (in your actual code).
Secondly, I see you are calling List contains method to check if the user already has given that review, yet you have not implemented "equals" and "hashCode" in the ReviewDAO.
public void addReview(ReviewDAO review) {
if (!this.reviews.contains(review)) {
this.reviews.add(review);
review.addRegisteredUser(this);
}
}
I have made few tweaks. Please check if it satisfies your need:
RegisteredUser class (Have used hashset cause "contains" search will be faster)
public class RegisteredUser {
private String nickname;
private Set<Review> reviews;
public RegisteredUser(String nickname) {
this.nickname = nickname;
reviews = new HashSet<>();
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public void addReview(Review review) {
if (!this.reviews.contains(review)) {
this.reviews.add(review);
//review.addRegisteredUser(this);
}
}
#Override
public String toString() {
return "RegisteredUser{" +
"nickname='" + nickname + '\'' +
", reviews=" + reviews +
'}';
}
}
ReviewDAO class:
public abstract class ReviewDAO {
private RegisteredUser registeredUser;
public abstract boolean write(Review review);
public void addRegisteredUser(RegisteredUser registeredUser) {
this.registeredUser = registeredUser;
}
}
Review class:
public class Review {
private String title;
private String description;
private int rank;
private boolean isAnonymous;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public boolean isAnonymous() {
return isAnonymous;
}
public void setAnonymous(boolean anonymous) {
isAnonymous = anonymous;
}
#Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Review review = (Review) o;
return Objects.equals(title, review.title);
}
#Override
public int hashCode() {
return Objects.hash(title);
}
}
BusinessLogic class
public class BusinessLogic {
public static void main(String[] args) throws ExecutionControl.NotImplementedException {
ReviewDAO_Factory reviewDAO_factory = new ReviewDAO_Factory();
RegisteredUser registeredUser = new RegisteredUser("Alessandro");
Review review = new Review();
review.setTitle("some review");
review.setDescription("some desc");
registeredUser.addReview(review);
ReviewDAO reviewDAO = reviewDAO_factory.getReviewDAO("mongodb");
reviewDAO.addRegisteredUser(registeredUser);
System.out.println(registeredUser.toString());
}
}

How to extract the methods of the a nested object using reflection

How do I access the getter methods of a nested custom object? I am able to extract the methods which return Strings but not able to extract methods of a nested object.
My implementation is as follows:
public class DataExtraction {
public void showTheData(Object student) throws IOException {
Class classofStudent = student.getClass();
Method[] methodsOfStudent = classofStudent.getDeclaredMethods();
for(Method method:methodsOfStudent)
{
if(isGetType(method))
{
if(method.getReturnType()==String.class)
{
try(InputStream is = new FileInputStream("ObjectFileReaderPrimitive.properties"))
{
//InputStream is = new FileInputStream("ObjectFileReaderPrimitive.properties");
Properties properties = new Properties();
properties.load(is);
System.out.println(properties.getProperty(method.getName()));
}
}
else
try(InputStream is = new FileInputStream("ObjectFileReaderNonPrimitive.properties"))
{
Class innerObjectClass = method.getReturnType().getClass();
Method[] methodsOfinnerObject = innerObjectClass.getDeclaredMethods();
for(Method methodofInnerClass : methodsOfinnerObject) {
if(isGetType(method))
{
Properties properties = new Properties();
properties.load(is);
System.out.println(properties.getProperty(methodofInnerClass.getName()));
}
}}
}
}}
private boolean isGetType(Method method) {
if(method.getName().startsWith("get"))
return true;
return false;
}
}
Where the student class is as follows-:
package com.sample;
public class Student {
private String id;
private String section;
private Address address;
public Student(String id, String section, Address address) {
super();
this.id = id;
this.section = section;
this.address = address;
}
public Student() {
super();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSection() {
return section;
}
public void setSection(String section) {
this.section = section;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
#Override
public String toString() {
return "Student [id=" + id + ", section=" + section + ", address=" + address + "]";
}
}
Address Object-:
package com.sample;
public class Address {
private String AddressLine1;
private String AddressLine2;
private String AddressLine3;
public Address(String addressLine1, String addressLine2, String addressLine3) {
super();
AddressLine1 = addressLine1;
AddressLine2 = addressLine2;
AddressLine3 = addressLine3;
}
public Address() {
super();
}
public String getAddressLine1() {
return AddressLine1;
}
public void setAddressLine1(String addressLine1) {
AddressLine1 = addressLine1;
}
public String getAddressLine2() {
return AddressLine2;
}
public void setAddressLine2(String addressLine2) {
AddressLine2 = addressLine2;
}
public String getAddressLine3() {
return AddressLine3;
}
public void setAddressLine3(String addressLine3) {
AddressLine3 = addressLine3;
}
#Override
public String toString() {
return "Address [AddressLine1=" + AddressLine1 + ", AddressLine2=" + AddressLine2 + ", AddressLine3="
+ AddressLine3 + "]";
}
}
Your problem is that you are not actually getting the correct class for your inner custom object.
Currently you are doing:
Class innerObjectClass = method.getReturnType().getClass();
This does not work because the method getReturnType is already returning the Class object of the return type. So what is happening is you are calling getClass() on a class object. This will return class java.lang.Class. You just need to remove the call to getClass:
Class innerObjectClass = method.getReturnType();
Here I have modified your code so that it prints all the getter objects in Student and Address
Class classofStudent = Student.class;
Method[] methodsOfStudent = classofStudent.getDeclaredMethods();
for (Method method : methodsOfStudent) {
if (isGetType(method)) {
if (method.getReturnType() == String.class) {
System.out.println(method.getName());
} else {
Class innerObjectClass = method.getReturnType();
Method[] methodsOfinnerObject = innerObjectClass.getDeclaredMethods();
for (Method methodofInnerClass : methodsOfinnerObject) {
if (isGetType(method)) {
System.out.println(methodofInnerClass.getName());
}
}
}
}
}

How to use jdbctemplate and row mapper to create object with a list of objects?

I was wondering how to use jdbctemplate and RowMapper to create object with a list of objects?
Below are the three objects that I need to get mapped based on data from the db.
public class UserDTO {
private String userID;
private String email;
private List<RooftopDTO> rooftops;
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<RooftopDTO> getRooftops() {
return rooftops;
}
public void setRooftops(List<RooftopDTO> rooftops) {
this.rooftops = rooftops;
}
}
public class RooftopDTO {
private String dealerID;
private String name;
private String address;
private List<VenueDTO> venues;
public String getDealerID() {
return dealerID;
}
public void setDealerID(String dealerID) {
this.dealerID = dealerID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public List<VenueDTO> getVenues() {
return venues;
}
public void setVenues(List<VenueDTO> venues) {
this.venues = venues;
}
}
public class VenueDTO {
private int integrationID;
private String rooftopID;
public int getIntegrationID() {
return integrationID;
}
public void setIntegrationID(int integrationID) {
this.integrationID = integrationID;
}
public String getRooftopID() {
return rooftopID;
}
public void setRooftopID(String rooftopID) {
this.rooftopID = rooftopID;
}
}
As you can see, I need to create lists of objects within each object. This is what I have so far in my MapperClass, but I can't figure out what else to do..
public class UserDTOMapper implements RowMapper<UserDTO> {
#Override
public UserDTO mapRow(ResultSet rs, int rowNum) throws SQLException {
UserDTO userDTO = new UserDTO();
RooftopDTO rooftops = new RooftopDTO();
VenueDTO venues = new VenueDTO();
ArrayList<VenueDTO> venueList = new ArrayList<>();
ArrayList<RooftopDTO> rooftopList = new ArrayList<>();
userDTO.setUserID(rs.getString("user_id"));
userDTO.setEmail(rs.getString("email"));
rooftops.setDealerID(rs.getString("dealer_id"));
rooftops.setAddress(rs.getString("addr_1"));
rooftops.setName(rs.getString("dealer_nm"));
venues.setIntegrationID(rs.getInt("integration_id"));
venues.setRooftopID("act_org_id");
}
}
Can someone help me finish this mapRow method?

Builder Pattern with only one class

I'm learning the Builder Pattern. I have a question, Why don't using a class (ex: MyCoffe)instead of 2 class (ex: MyCoffe, BuilderClass). Thanks you for reading.
package Creational.Builder;
public class MyCoffe {
private String name;
private int id;
public MyCoffe(){
}
public MyCoffe setName(String newName){
this.name = newName;
return this;
}
public MyCoffe setId(int newId){
this.id = newId;
return this;
}
public MyCoffe build(){
return this;
}
public String toString(){
return name + "/" + id;
}
public static void main(String[] args){
MyCoffe myCoffe = new MyCoffe().setName("HelloWorld").setId(9999);
System.out.println(myCoffe);
System.out.println("Thanks for help!");
}
}
You certainly can in some select situations, but one of the purposes of the builder pattern is to avoid having an invalid instance of a class. So unless it's valid for MyCoffe to not have a name or ID, you don't want an instance of it running around without a name or ID, like this:
MyCoffe invalid = new MyCoffe();
Also note that when the class is self-building like that, it's easy to forget to call the final build method (as, in fact, you did in your main), so the instance never gets validated.
Hence using a separate builder to hold the incomplete information before building the complete, valid instance.
In Java, it's not uncommon for the builder to be a static nested class:
public class MyCoffe {
public static class Builder {
private String name;
private int id;
public Builder() {
}
public Builder setName(String name) {
this.name = name;
return this;
}
public Builder setId(int id) {
this.id = id;
return this;
}
public MyCoffe build() {
if (this.name == null) {
throw new AppropriateException("'name' is required");
}
if (this.id == 0) { // Assumes 0 isn't a valid ID
throw new AppropriateException("'id' is required");
}
return new MyCoffe(name, id);
}
}
private String name;
private int id;
public MyCoffe(String name, int id) {
this.name = name;
this.id = id;
}
#Override
public String toString(){
return name + "/" + id;
}
public static void main(String[] args){
MyCoffe myCoffe = new MyCoffe.Builder().setName("HelloWorld").setId(9999).build();
System.out.println(myCoffe);
System.out.println("Thanks for help!");
}
}
Or if declaring the properties twice like that bothers you, MyCoffe can allow private incomplete instances, like this:
public class MyCoffe {
public static class Builder {
private MyCoffe instance;
public Builder() {
this.instance = new MyCoffe();
}
public Builder setName(String name) {
this.instance.name = name;
return this;
}
public Builder setId(int id) {
this.instance.id = id;
return this;
}
public MyCoffe build() {
if (this.instance.name == null) {
throw new AppropriateException("'name' is required");
}
if (this.instance.id == 0) { // Assumes 0 isn't a valid ID
throw new AppropriateException("'id' is required");
}
return this.instance;
}
}
private String name;
private int id;
private MyCoffe() {
}
public MyCoffe(String name, int id) {
this.name = name;
this.id = id;
}
#Override
public String toString(){
return name + "/" + id;
}
public static void main(String[] args){
MyCoffe myCoffe = new MyCoffe.Builder().setName("HelloWorld").setId(9999).build();
System.out.println(myCoffe);
System.out.println("Thanks for help!");
}
}

Does Neo4j OGM work well with interfaces?

I've developed two sets of classes - the first one are just classes, whereas in the second set, the classes derive from interfaces. Both the sets of classes mimic each other. The repositories for them are also similar. However, the repository works well for the first set of classes (nodes and relationships). For the second set of classes though, the repository is able to insert records, but the findAll method fails and doesn't return me any records.
Here's the first set of classes with the repository -
public abstract class Entity {
#GraphId
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || id == null || getClass() != o.getClass()) return false;
Entity entity = (Entity) o;
if (!id.equals(entity.id)) return false;
return true;
}
#Override
public int hashCode() {
return (id == null) ? -1 : id.hashCode();
}
}
public abstract class GenericRepository<T> implements IGenericRepository<T> {
private static final int DEPTH_LIST = 1;
private static final int DEPTH_ENTITY = 2;
private Session session;
public GenericRepository(String url, String username, String password) {
super();
session = Neo4jSessionFactory.getInstance().getNeo4jSession(url, username, password);
}
public Iterable<T> findAll() {
return session.loadAll(getEntityType(), DEPTH_LIST);
}
public T findOne(Long id) {
return session.load(getEntityType(), id, DEPTH_ENTITY);
}
public void delete(T entity) {
session.delete(session.load(getEntityType(), ((Entity) entity).getId()));
}
public T createOrUpdate(T entity) {
session.save(entity, DEPTH_ENTITY);
return findOne(((Entity) entity).getId());
}
public abstract Class<T> getEntityType();
}
#RelationshipEntity(type="MY_ROLE")
public class ARole extends Entity {
#Property
private String title;
#StartNode
private HomoSapiens start;
#EndNode
private HomoSapiens end;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public HomoSapiens getStart() {
return start;
}
public void setStart(HomoSapiens start) {
this.start = start;
}
public HomoSapiens getEnd() {
return end;
}
public void setEnd(HomoSapiens end) {
this.end = end;
}
public ARole() {}
public ARole(String title) {
this.title = title;
}
}
#NodeEntity
public class HomoSapiens extends Entity {
private String name;
#Relationship(type = "MY_ROLE", direction = Relationship.INCOMING)
private ARole aRole;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ARole getaRole() {
return aRole;
}
public void setaRole(ARole aRole) {
this.aRole = aRole;
}
public HomoSapiens() {}
public HomoSapiens(String name) {
this.name = name;
}
}
public class ARoleDao extends GenericRepository<ARole> implements IARoleDao {
public ARoleDao(String url, String username, String password) {
super(url, username, password);
}
#Override
public Class<ARole> getEntityType() {
return ARole.class;
}
}
Here's the second set of classes -
public interface IARole {
String getTitle();
void setTitle(String title);
IHomoSapiens getStart();
void setStart(IHomoSapiens start);
IHomoSapiens getEnd();
void setEnd(IHomoSapiens end);
}
public interface IHomoSapiens {
String getName();
void setName(String name);
IARole getARole();
void setARole(IARole aRole);
}
#RelationshipEntity(type="MY_DERIVED_ROLE")
public class DerivedARole extends Entity implements IARole {
#Property
private String title;
#StartNode
private IHomoSapiens start;
#EndNode
private IHomoSapiens end;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public IHomoSapiens getStart() {
return start;
}
public void setStart(IHomoSapiens start) {
this.start = start;
}
public IHomoSapiens getEnd() {
return end;
}
public void setEnd(IHomoSapiens end) {
this.end = end;
}
public DerivedARole() {}
public DerivedARole(String title) {
this.title = title;
}
}
#NodeEntity
public class DerivedHomoSapiens extends Entity implements IHomoSapiens {
private String name;
#Relationship(type = "MY_DERIVED_ROLE", direction = Relationship.INCOMING)
private IARole aRole;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public IARole getARole() {
return aRole;
}
public void setARole(IARole aRole) {
this.aRole = aRole;
}
public DerivedHomoSapiens() {}
public DerivedHomoSapiens(String name) {
this.name = name;
}
}
public class DerivedARoleDao extends GenericRepository<DerivedARole> {
public DerivedARoleDao(String url, String username, String password) {
super(url, username, password);
}
#Override
public Class<DerivedARole> getEntityType() {
return DerivedARole.class;
}
}
public class DerivedARoleDaoSpecs {
private DerivedARoleDao derivedARoleDao;
#Before
public void setUp() throws Exception {
derivedARoleDao = new DerivedARoleDao(DomainHelper.NEO_URL, DomainHelper.NEO_USERNAME,
DomainHelper.NEO_PASSWORD);
}
public void should_insert_data() {
IHomoSapiens start = new DerivedHomoSapiens("d-start");
IHomoSapiens end = new DerivedHomoSapiens("d-end");
IARole aRole = new DerivedARole("parent");
start.setARole(aRole);
end.setARole(aRole);
aRole.setStart(start);
aRole.setEnd(end);
IARole created = derivedARoleDao.createOrUpdate((DerivedARole)aRole);
assertThat(created, is(notNullValue()));
}
public void should_find_all() {
Iterable<DerivedARole> derivedARoles = derivedARoleDao.findAll();
assertThat(derivedARoles, is(notNullValue()));
assertThat(Iterables.isEmpty(derivedARoles), is(false));
assertTrue(Iterables.size(derivedARoles) > 0);
System.out.println(Iterables.size(derivedARoles));
}
#Test
public void should_do_crud() {
// should_insert_data();
should_find_all();
// should_find_by_id();
}
}
Am I missing something here? Or does the Neo4j OGM works well with classes (not implementing interfaces)?
The entire source code is at - https://github.com/mmwaikar/java-neo-ogm-ex (in case, it helps).
Thanks,
Manoj.
This should be fixed in 1.1.3
Till that is released, please try your code example with 1.1.3-SNAPSHOT.
You'll need to include
<repository>
<id>neo4j-snapshots</id>
<url>http://m2.neo4j.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>

Categories