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  Connecting Apigee To GKE Using Headless Services And Cloud DNS

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  Extending Network Reachability Of Vertex AI Pipelines

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
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
IBM and Ferrari Premium Partner
View Post
  • Data
  • Engineering

IBM Selected as Official Fan Engagement and Data Analytics Partner for Scuderia Ferrari HP

  • November 7, 2024

Stay Connected!
LATEST
  • notta-ai-header 1
    Notta vs Otter: Which AI Transcription Tool Has the Edge in 2025? (HackerNoon)
    • May 16, 2025
  • college-of-cardinals-2025 2
    The Definitive Who’s Who of the 2025 Papal Conclave
    • May 7, 2025
  • conclave-poster-black-smoke 3
    The World Is Revalidating Itself
    • May 6, 2025
  • oracle-ibm 4
    IBM and Oracle Expand Partnership to Advance Agentic AI and Hybrid Cloud
    • May 6, 2025
  • 5
    Conclave: How A New Pope Is Chosen
    • April 25, 2025
  • Getting things done makes her feel amazing 6
    Nurturing Minds in the Digital Revolution
    • April 25, 2025
  • 7
    AI is automating our jobs – but values need to change if we are to be liberated by it
    • April 17, 2025
  • 8
    Canonical Releases Ubuntu 25.04 Plucky Puffin
    • April 17, 2025
  • 9
    United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services
    • April 15, 2025
  • 10
    Tokyo Electron and IBM Renew Collaboration for Advanced Semiconductor Technology
    • April 2, 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
  • 1
    IBM Accelerates Momentum in the as a Service Space with Growing Portfolio of Tools Simplifying Infrastructure Management
    • March 27, 2025
  • 2
    Tariffs, Trump, and Other Things That Start With T – They’re Not The Problem, It’s How We Use Them
    • March 25, 2025
  • 3
    IBM contributes key open-source projects to Linux Foundation to advance AI community participation
    • March 22, 2025
  • 4
    Co-op mode: New partners driving the future of gaming with AI
    • March 22, 2025
  • 5
    Mitsubishi Motors Canada Launches AI-Powered “Intelligent Companion” to Transform the 2025 Outlander Buying Experience
    • March 10, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.