DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Databases
  4. A Method for Persisting Static Entities to Neo4j

A Method for Persisting Static Entities to Neo4j

Sony Arouje user avatar by
Sony Arouje
·
Mar. 14, 12 · Interview
Like (0)
Save
Tweet
Share
2.54K Views

Join the DZone community and get the full member experience.

Join For Free

At the core Neo4jD uses Node and Relationship to persist data to Neo4j graph database. If you want to see how the core is working then visit here. You might notice that, the Node and Relationship is not static typed objects. Most of us work with Static typed entities and want to persist the same. To deal with static typed entities I added a layer on top of Neo4jD core called NodeMapper.

Node Mapper
Below are the features of NodeMapper

  • Map the entity to Node object and persist in Neo4j.
  • Handles the relation between entities, if an entity has a sub entity then NodeMapper will create a relationship between them.
  • Persist the object graph using the Model created, will cover later in the page.
  • Inject interceptor for Lazy loading of related entities.
  • And more

How to Persist entities using NodeMapper
To persist the entities, first we need to draw the object graph of the Entity we need to persist. Let’s have a look at an eg.

public class Order
{
    public Order()
    {
        _orderItems = new List<OrderItem>();
    }

    [EntityId]
    public int Id { get; set; }
    public virtual string Name { get; set; }

    IList<OrderItem> _orderItems;
    public virtual IList<OrderItem> OrderItems
    {
        get { return _orderItems; }
        private set { _orderItems = value; }
    }
    public void AddOrderItem(OrderItem item)
    {
        this._orderItems.Add(item);
    }
}

public class OrderItem
{
    public OrderItem()
    {
    }
    public OrderItem(int id, Product product)
    {
        this.Id = id;
        this.Product = product;
    }
    [EntityId]
    public int Id { get; set; }

    public virtual Product Product { get; set; }
}

public class Product
{
    public Product()
    {

    }
    public Product(int id, string productName)
    {
        this.Id = id;
        this.ProductName = productName;
    }
    [EntityId]
    public int Id { get; set; }
    public string ProductName { get; set; }
}

ou will notice some rules in defining entities.

  • The unique Id field is decorated with EntityId attribute. It’s mandatory to identify the Id field of the entity.
  • Properties that needs Lazy loading should be virtual.
  • Mandatory to have a default parameter less constructor.

Now let’s define the object graph. This configuration helps NodeMapper to persist the entire Graph. It’s very similar to how we do in Entity Framwork Code first approach.

public class OrderConfiguration:EntityConfiguration<Order>
{
    public OrderConfiguration()
    {
        this.RelatedTo<OrderItem>(o => o.OrderItems);
    }
}

public class OrderItemConfiguration:EntityConfiguration<OrderItem>
{
    public OrderItemConfiguration()
    {
        this.RelatedTo<Product>(oi => oi.Product);
    }
}

public class ProductConfiguration:EntityConfiguration<Product>
{
    public ProductConfiguration()
    {
        this.Leaf();
    }
}

As you can see in the ProductConfiguration, there is no related entity so we marked it as Leaf.

Let’s see how to persist an order

[SetUp]
public void Initialize()
{
    GraphEnvironment.SetBaseUri("http://localhost:7474/");

    ModelBuilder.Add(new OrderConfiguration());
    ModelBuilder.Add(new OrderItemConfiguration());
    ModelBuilder.Add(new ProductConfiguration());
}

[TestCase]
public void SaveOrder()
{
    Order order = new Order();
    order.Id = 0;
    order.Name = "Sony";
    order.AddOrderItem(new OrderItem(0, new Product(0, "Rice")));
    order.AddOrderItem(new OrderItem(0, new Product(0, "Sugar")));

    NodeMapper nodeMapper = new NodeMapper();
    nodeMapper.Save<Order>(order);
    Console.WriteLine(order.Id.ToString());
    Assert.AreEqual(1, order.Id);
}

In the Initialize method of NUnit Test we added EntityConfigurations to ModelBuilder. NodeMapper uses the ModelBuilder to understand the Object graph and uses reflection to traverse through the object graph and persist it.

Lazy Loading

Neo4jD uses Lazy loading to load the related entities, it uses Castle DynamicProxy to intercept property calls and inject lazy loading functionality. To perform lazy loading the property should be virtual, you can see the OrderItems in Order entity.

Retrieve saved Entity

[TestCase]
public void GetOrder()
{
    NodeMapper nodeMapper = new NodeMapper();
    Order order = nodeMapper.Get<Order>(14);
    Assert.AreEqual(14, order.Id);
    foreach (OrderItem item in order.OrderItems)
    {
        Console.WriteLine(item.Id.ToString());
        Product prod = item.Product;
        if (prod != null)
            Console.WriteLine(prod.ProductName);
    }
    Assert.AreEqual(2, order.OrderItems.Count);
}

 

Database Lazy loading Neo4j

Published at DZone with permission of Sony Arouje, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Scaling Your Testing Efforts With Cloud-Based Testing Tools
  • How To Handle Secrets in Docker
  • A First Look at Neon
  • Java REST API Frameworks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: