How to Create a Class That Can Have Parent and Child Relationship
Tag : chash , By : user119605
Date : March 29 2020, 07:55 AM
To fix the issue you can do Just for people in the future looking for this same solution, here is the full class: public class Branch {
public string Name { get; set; }
public string Link { get; set; }
public Branch Parent { get; set; }
public TreeBranches Children { get; private set; }
internal Branch(string Name, string Link) {
this.Name = Name;
this.Link = Link;
this.Children = new TreeBranches(this);
} // Branch - Constructor - Overload
internal Branch(string Name, string Link, TreeBranches Children) {
this.Name = Name;
this.Link = Link;
this.Children = Children;
this.Children.ToList().ForEach(delegate(Branch branch) {
branch.Parent = this;
});
} // Branch - Constructor - Overload
/// <summary>
/// Returns a boolean indicating if the given Branch has any child Branches.
/// </summary>
public bool HasChildren {
get { return this.Children.Count > 0; }
} // HasChildren - Property - ReadOnly
/// <summary>
/// Gets the path from the oldest ancestor to the current Branch.
/// </summary>
public string Path {
get {
string Result = "";
Branch parent = this;
while (parent != null) {
Result = string.Format("{0}/{1}", parent.Name, Result);
parent = parent.Parent;
} // while stepping up the tree
return string.IsNullOrWhiteSpace(Result) ? "" : Result.Substring(0, Result.Length - 1);
} // get
} // Path - Property - ReadOnly
} // Branch - Class
public class TreeBranches : IList<Branch> {
private List<Branch> branches = new List<Branch>();
private Branch owner;
public TreeBranches() {
this.owner = null;
}
public TreeBranches(Branch owner) {
this.owner = owner;
}
public void Add(Branch branch) {
branch.Parent = this.owner;
this.branches.Add(branch);
}
#region Standard IList Method Implementation
IEnumerator<Branch> IEnumerable<Branch>.GetEnumerator() { return this.branches.GetEnumerator(); }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.branches.GetEnumerator(); }
public int IndexOf(Branch item) { return this.branches.IndexOf(item); }
public void Insert(int index, Branch item) { this.branches.Insert(index, item); }
public void RemoveAt(int index) { this.branches.RemoveAt(index); }
public Branch this[int index] {
get { return this.branches[index]; }
set { this.branches[index] = value; }
}
public void Clear() { this.branches.Clear(); }
public bool Contains(Branch item) { return this.branches.Contains(item); }
public void CopyTo(Branch[] array, int arrayIndex) { this.branches.CopyTo(array, arrayIndex); }
public int Count { get { return this.branches.Count(); } }
public bool IsReadOnly { get { return this.IsReadOnly; } }
public bool Remove(Branch item) { return this.branches.Remove(item); }
#endregion Standard IList Method Implementation
} // TreeBranches - Class
|
JPA entities: How to establish relationship between Parent and multiple Child classes
Date : March 29 2020, 07:55 AM
With these it helps If i got that right, class parent is an entity that keeps a one-to-one relationship with ChildType entity. Also ChildType is an abstract entity with 2 implementations, ChildA and ChildB. So the JPA annotations configuration for each one of the entities, could be like that: Parent class as Entity @Entity
@Table(name = "PARENT")
public class Parent { // better name will do the job, because parent is often called
// the higher level class of the same hierarchy
@Id
@GeneratedValue
@Column(name = "PARENT_ID")
private long id;
@Column(name = "SOME_VALUE") //if you need to persist it
private int someValue1;
@OneToOne(optional = false, cascade = CascadeType.ALL)
@JoinColumn(name = "FK_PARENT_ID")
private ChildType child;
// getters and setters
}
ChildType class as Entity @Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class ChildType { // this one is actually called parent class
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "CHILDTYPE_ID")
protected Long id;
@Column(name = "SOME_VALUE_2")
private int someValue2; // or maybe protected. Depends if you need childs to access it
@Column(name = "A_STRING")
private String string; // or maybe protected. Depends if you need childs to access it
// getters and setters
}
ChildA as Entity @Entity
@Table(name = "CHILD_A")
public Class ChildA extends ChildType {
@Column(name = "A_STRING")
private String string;
// getters and setters
}
ChildB as Entity @Entity
@Table(name = "CHILD_B")
public Class ChildB extends ChildType {
@Column(name = "AN_Integer")
private Integer integer;
// getters and setters
}
|
How do I create a relationship with Parent and Child using cypher?
Date : March 29 2020, 07:55 AM
To fix this issue It might be helpful if you provided your load csv I am not sure what you provided in the way of labels on the nodes when you imported your data but this example should find children that have not already been matched to a parent category where the parent category exists in your data (i.e. greater than equal to 4). It should then match the parent based ont eh parent id on the node and create a :CHILD_OF relationship between the child and the parent. match (child:Category)
where not (child-[:CHILD_OF]->())
and child.parent_id >= 4
with child
match (parent:Category)
where parent.category_id = child.parent_id
create child-[:CHILD_OF]->parent
|
Create a parent-child relationship after the parent type already exists
Date : March 29 2020, 07:55 AM
it fixes the issue This is not allowed in ElasticSearch, so it mandates to create both parent and child in the same request or call.
|
JAVA Inheritance issues- this is about the relationship between parent and child classes
Date : March 29 2020, 07:55 AM
Any of those help So why is that I can cast a parent class as a child but not the other way around? S = J; //aliasing ?
//J 0 S 1
System.out.println (S.x); // should print 0 but prints 1
J1 = (junior)S1; //Senior cannot be cast to junior, why?
System.out.println (S1.x);
System.out.println (J1.x);// should print 1 but prints 0
|