aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Data
  • Engineering

NHibernate Dialect For Cloud Spanner Is Now Generally Available

  • aster.cloud
  • November 21, 2022
  • 3 minute read

The Cloud Spanner NHibernate Dialect is now Generally Available

Introduction

Do you have an application that you developed with NHibernate and now you want to migrate to a cloud database? Google Cloud Spanner now supports the NHibernate Object Relational Mapper. You can reuse existing NHibernate code with the Cloud Spanner NHibernate dialect, or write new features while leveraging existing knowledge.The Cloud Spanner NHibernate dialect implements all the classes and interfaces that are needed to use Cloud Spanner with NHibernate. Add the Spanner NHibernate nuget package to your project and connect to Cloud Spanner as follows:

Installing

Install the Spanner NHibernate dialect:


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

dotnet add package Google.Cloud.Spanner.NHibernate --version 1.0.0

 

Connecting

Connect to Cloud Spanner with NHibernate:

using NHibernate;
using NHibernate.Cfg;

Configuration = new Configuration().DataBaseIntegration(db =>
{
    db.Dialect<SpannerDialect>();
    db.ConnectionString =
        "Data Source=projects/MY-PROJECT/instances/MY-INSTANCE/databases/MY-DATABASE";
});
var sessionFactory = Configuration.BuildSessionFactory();
using var session = configuration.SessionFactory.OpenSession();

var transaction = session.BeginTransaction();
var singer = new Singer
{
    FirstName = "Jamie",
    LastName = "Allison"
};
await session.SaveAsync(singer);
await transaction.CommitAsync();

You can also use a custom connection provider or a user supplied connection with Spanner NHibernate. See this example for more information.

Usage

The Cloud Spanner NHibernate dialect supports standard NHibernate features, like querying data using both the NHibernate Criteria API and Linq, managing associations and executing transactions. The dialect also supports specific Spanner features such as interleaved tables, mutations and stale reads.

Query with Linq

NHibernate 3.0 introduced the Linq to NHibernate provider. The Spanner NHibernate dialect implements translations for the most commonly used functions and operators so these can be used with LInq.

using var session = configuration.SessionFactory.OpenSession();

var singersBornBefore2000 = await session
    .Query<Singer>()
    .Where(s => s.BirthDate < new SpannerDate(2000, 1, 1))
    .OrderBy(s => s.BirthDate)
    .ToListAsync();
Console.WriteLine("Singers born before 2000:");
foreach (var singer in singersBornBefore2000)
{
    Console.WriteLine($"\t{singer.FullName}, born at {singer.BirthDate}");
}

var singersStartingWithAl = await session
    .Query<Singer>()
    .Where(s => s.FullName.StartsWith("Al"))
    .OrderBy(s => s.LastName)
    .ToListAsync();
Console.WriteLine("Singers with a name starting with 'Al':");
foreach (var singer in singersStartingWithAl)
{
    Console.WriteLine($"\t{singer.FullName}");
}

Read More  Google Cloud Is A Leader In The Forrester Wave: Public Cloud Container Platforms, Q1 2022

Interleaved Tables

Using interleaved tables for parent-child relationships in your schema can improve performance. Interleaving a child table with a parent means that the child records will be stored physically together with the parent. These relationships can be modeled and used as any other association in NHibernate.

using var session = configuration.SessionFactory.OpenSession();
using var transaction = session.BeginTransaction();

// Create a Singer, Album and Track.
// Album references Singer with a normal foreign key relationship.
var singer = new Singer
{
    FirstName = "Farhan",
    LastName = "Edwards",
};
var album = new Album
{
    Title = "Thinking Jam",
    Singer = singer,
};
// Track is interleaved in Album. This means that Track must use a composite primary
// key. In this example, the primary key is the Album ID and a Track number.
var track = new Track
{
    // A TrackIdentifier consists of the parent Album and a track number.
    TrackIdentifier = new TrackIdentifier(album, 1L),
    Title = "Always Sweet",
};
await session.SaveAsync(singer);
await session.SaveAsync(album);
await session.SaveAsync(track);
await transaction.CommitAsync();

This directory contains the complete mapping of all the entities that are used in the sample above.

Mutations

NHibernate will use Data Manipulation Language (DML) by default. You can instruct NHibernate to use mutations for all transactions that are executed using a specific NHibernate session, or for a single transaction.

var sessionFactory = configuration.Configuration.BuildSessionFactory();

// Create a session that will always use mutations.
using var session = sessionFactory
                        .OpenSession()
                        .SetBatchMutationUsage(MutationUsage.Always);

// Create a new Singer and save it. The insert will use a Mutation.
var transaction = sessionUsingMutations.BeginTransaction();
var singer = new Singer
{
    FirstName = "Wanda",
    LastName = "Yates",
};
await session.SaveAsync(singer);
await transaction.CommitAsync();

// You can also instruct a single transaction to use mutations.
using var session2 = sessionFactory.OpenSession();
var transaction2 = session.BeginTransaction(MutationUsage.Always);

Further Samples

The GitHub repository contains a directory with multiple samples for common use cases for working with NHibernate and/or Cloud Spanner.

Read More  Announcing Private Marketplace, Now In Preview

Limitations

Cloud Spanner features that are not supported in NHibernate are listed here.

Getting Involved

We’d love to hear from you, especially if you’re a .NET developer considering Cloud Spanner or an existing Cloud Spanner customer who is considering using NHibernate for new projects. The project is open-source, and you can comment, report bugs, and open pull requests on Github.

 

By: Knut Olav Løite (Software Engineer)
Source: Google Cloud Blog


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

aster.cloud

Related Topics
  • Cloud Spanner
  • Coding
  • Google Cloud
  • Linq
  • NHibernate
  • Progamming
  • Tutorials
You May Also Like
View Post
  • Engineering

Just make it scale: An Aurora DSQL story

  • May 29, 2025
Getting things done makes her feel amazing
View Post
  • Computing
  • Data
  • Featured
  • Learning
  • Tech
  • Technology

Nurturing Minds in the Digital Revolution

  • April 25, 2025
View Post
  • Engineering
  • Technology

Guide: Our top four AI Hypercomputer use cases, reference architectures and tutorials

  • March 9, 2025
View Post
  • Computing
  • Engineering

Why a decades old architecture decision is impeding the power of AI computing

  • February 19, 2025
View Post
  • Engineering
  • Software Engineering

This Month in Julia World

  • January 17, 2025
View Post
  • Engineering
  • Software Engineering

Google Summer of Code 2025 is here!

  • January 17, 2025
View Post
  • Data
  • Engineering

Hiding in Plain Site: Attackers Sneaking Malware into Images on Websites

  • January 16, 2025
View Post
  • Computing
  • Design
  • Engineering
  • Technology

Here’s why it’s important to build long-term cryptographic resilience

  • December 24, 2024

Stay Connected!
LATEST
  • 1
    Just make it scale: An Aurora DSQL story
    • May 29, 2025
  • 2
    Reliance on US tech providers is making IT leaders skittish
    • May 28, 2025
  • Examine the 4 types of edge computing, with examples
    • May 28, 2025
  • AI and private cloud: 2 lessons from Dell Tech World 2025
    • May 28, 2025
  • 5
    TD Synnex named as UK distributor for Cohesity
    • May 28, 2025
  • Weigh these 6 enterprise advantages of storage as a service
    • May 28, 2025
  • 7
    Broadcom’s ‘harsh’ VMware contracts are costing customers up to 1,500% more
    • May 28, 2025
  • 8
    Pulsant targets partner diversity with new IaaS solution
    • May 23, 2025
  • 9
    Growing AI workloads are causing hybrid cloud headaches
    • May 23, 2025
  • Gemma 3n 10
    Announcing Gemma 3n preview: powerful, efficient, mobile-first AI
    • May 22, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • Understand how Windows Server 2025 PAYG licensing works
    • May 20, 2025
  • By the numbers: How upskilling fills the IT skills gap
    • May 21, 2025
  • 3
    Cloud adoption isn’t all it’s cut out to be as enterprises report growing dissatisfaction
    • May 15, 2025
  • 4
    Hybrid cloud is complicated – Red Hat’s new AI assistant wants to solve that
    • May 20, 2025
  • 5
    Google is getting serious on cloud sovereignty
    • May 22, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.