By Philip Japikse MVP, MCSD.NET, MCDBA, CSM, CSP Principal Consultant Pinnacle Solutions Group

Similar documents
indx.qxd 11/3/04 3:34 PM Page 339 Index

foreword to the first edition preface xxi acknowledgments xxiii about this book xxv about the cover illustration

find() method, 178 forclass() method, 162 getcurrentsession(), 16 getexecutablecriteria() method, 162 get() method, 17, 177 getreference() method, 178

--Microsoft-- --Windows Phone--

Spring & Hibernate. Knowledge of database. And basic Knowledge of web application development. Module 1: Spring Basics

Call: JSP Spring Hibernate Webservice Course Content:35-40hours Course Outline

TEST DRIVEN DEVELOPMENT FOR T-SQL IT S NOT JUST TESTING FIRST!

Lightweight J2EE Framework

NHibernate Reference Documentation. Version: 1.0.2

Course Outline. Developing Data Access Solutions with Microsoft Visual Studio 2010 Course 10265A: 5 days Instructor Led

Developing Data Access Solutions with Microsoft Visual Studio 2010

By Philip Japikse MVP, MCSD.Net, MCDBA, CSM Principal Consultant Pinnacle Solutions Group

object/relational persistence What is persistence? 5

Fast Track to EJB 3.0 and the JPA Using JBoss

Hibernate Interview Questions

DESIGN PATTERNS FOR MERE MORTALS

"Charting the Course... MOC A Developing Data Access Solutions with Microsoft Visual Studio Course Summary

Chapter 3. Harnessing Hibernate

NHibernate Reference Documentation. Version: 5.0

Unit 6 Hibernate. List the advantages of hibernate over JDBC

NHibernate Reference Documentation. Version: 5.2

LESSONS LEARNED: BEING AGILE IN THE WATERFALL SANDBOX

Java Object/Relational Persistence with Hibernate. David Lucek 11 Jan 2005

Architecture overview

The C# Programming Language. Overview

ADVANCED JAVA TRAINING IN BANGALORE

Advanced WCF 4.0 .NET. Web Services. Contents for.net Professionals. Learn new and stay updated. Design Patterns, OOPS Principles, WCF, WPF, MVC &LINQ

10265: Developing Data Access Solutions with Microsoft Visual Studio 2010 Duration: 5 Days Method: Instructor-Led

CONTROLLING ASP.NET MVC 4

Lightweight J2EE Framework

SOLID DESIGN PATTERNS FOR MERE MORTALS

DOT NET Syllabus (6 Months)

Introduction to Web Application Development Using JEE, Frameworks, Web Services and AJAX

MCSA Universal Windows Platform. A Success Guide to Prepare- Programming in C# edusum.com

JAVASCRIPT FOR THE C# DEVELOPER

Installing MySQL. Hibernate: Setup, Use, and Mapping file. Setup Hibernate in IDE. Starting WAMP server. phpmyadmin web console

Setting Schema Name For Native Queries In. Hibernate >>>CLICK HERE<<<

Object Persistence Design Guidelines

VERSION ASP.NET CORE APIS

Pro JPA 2. Mastering the Java Persistence API. Apress* Mike Keith and Merrick Schnicariol

The Object-Oriented Paradigm. Employee Application Object. The Reality of DBMS. Employee Database Table. From Database to Application.

GitHub code samples. Appendix B

JAVA COURSES. Empowering Innovation. DN InfoTech Pvt. Ltd. H-151, Sector 63, Noida, UP

Learn Well Technocraft

Web Application Development Using Spring, Hibernate and JPA

Index. setmaxresults() method, 169 sorting, 170 SQL DISTINCT query, 171 uniqueresult() method, 169

Page 1

Generating A Hibernate Mapping File And Java Classes From The Sql Schema

Build Testable Client and Service Applications

IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS

Enterprise Features & Requirements Analysis For EJB3 JPA & POJO Persistence. CocoBase Pure POJO

Web Application Development Using Spring, Hibernate and JPA

THE RESURGENCE OF WEBFORMS I M NOT DEAD YET! Philip Japikse MVP, MCSD.Net, MCDBA, CSM, CSP

.NET Database Technologies. Entity Framework: Queries and Transactions

POJOs in Action DEVELOPING ENTERPRISE APPLICATIONS WITH LIGHTWEIGHT FRAMEWORKS CHRIS RICHARDSON MANNING. Greenwich (74 w. long.)

International Journal of Advance Research in Engineering, Science & Technology HIBERNATE FRAMEWORK FOR ENTERPRISE APPLICATION

Enterprise JavaBeans 3.1

Table of Contents. I. Pre-Requisites A. Audience B. Pre-Requisites. II. Introduction A. The Problem B. Overview C. History

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Web Application Development Using Spring, Hibernate and JPA

Course Hours

Saikat Banerjee Page 1

"Charting the Course... Mastering EJB 3.0 Applications. Course Summary

EF6 - Version: 1. Entity Framework 6

Understanding Impact of J2EE Applications On Relational Databases. Dennis Leung, VP Development Oracle9iAS TopLink Oracle Corporation

Microsoft.CerTkiller v by.SANDRA.65q

DOT NET SYLLABUS FOR 6 MONTHS

New Features in Java language

How to be a C# ninja in 10 easy steps. Benjamin Day

NHibernate in Action

"Charting the Course... Agile Database Design Techniques Course Summary

Agile Database Techniques Effective Strategies for the Agile Software Developer. Scott W. Ambler

Web Application Development Using JEE, Enterprise JavaBeans and JPA

Tapestry. Code less, deliver more. Rayland Jeans

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

6232B: Implementing a Microsoft SQL Server 2008 R2 Database

welcome to production Graeme Foster Spanish Software Developer

20483BC: Programming in C#

Microsoft. Microsoft Visual C# Step by Step. John Sharp

Introduction to JPA. Fabio Falcinelli

Migrate Your Skills to Microsoft.NET Framework 2.0 and 3.0 using Visual Studio 2005 (C#)

Advanced Web Systems 9- Hibernate annotations, Spring integration, Aspect Oriented Programming. A. Venturini

Programming C# 5.0. Ian Griffiths O'REILLY' Beijing Cambridge * Farnham Kbln Sebastopol Tokyo

POJOs to the rescue. Easier and faster development with POJOs and lightweight frameworks

Object Persistence and Object-Relational Mapping. James Brucker

Struts: Struts 1.x. Introduction. Enterprise Application

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

CORE JAVA. Saying Hello to Java: A primer on Java Programming language

Web Application Development Using JEE, Enterprise JavaBeans and JPA

Module 1. An Introduction to C# Module 2. Classes and Objects. Vasundhara Sector 14-A, Plot No , Near Vaishali Metro Station,Ghaziabad

DEVELOPING WEB AZURE AND WEB SERVICES MICROSOFT WINDOWS AZURE

CS313D: ADVANCED PROGRAMMING LANGUAGE

db4o: Part 2 Configuration and Tuning, Distribution and Replication Schema Evolution: Refactoring, Inheritance Evolution Callbacks and Translators

ΠΙΝΑΚΑΣ ΠΛΑΝΟΥ ΕΚΠΑΙΔΕΥΣΗΣ

Programming in Visual Basic with Microsoft Visual Studio 2010

Java SE 8 Programming

WCF RIA Services. About Brian 8/10/2011. Brian Noyes Chief Architect IDesign Inc. (

Perfect for the Enterprise

Modeling. Enterprise Scale

Spring Interview Questions

Transcription:

By Philip Japikse Phil.japikse@pinnsg.com MVP, MCSD.NET, MCDBA, CSM, CSP Principal Consultant Pinnacle Solutions Group

Principal Consultant, Pinnacle Solutions Group Microsoft MVP MCSD, MCDBA, CSM, CSP Enterprise Application Architect Trainer/Mentor/Speaker Lead Director, Cincinnati.NET User s Group Contributing Author www.nplus1.org Phone:513-619-6323 Fax:513-791-5202 2

CRUD SUCKS Phone:513-619-6323 Fax:513-791-5202 3

Architecture Data Objects Configuration Mapping Collections Sessions and Transactions Querying Manipulating Persistent Objects Real World Customizations Custom Connection Provider SQL Server Timestamp for Concurrency Additional Resources Phone:513-619-6323 Fax:513-791-5202 4

Architecture Phone:513-619-6323 Fax:513-791-5202 5

Goal to be DAL implementation agnostic Not always attainable POCO exist in one of three states: Transient Don t exist in the data store Persistent Exist in the data store Detached Persistent objects no longer associated with the persistence manager Phone:513-619-6323 Fax:513-791-5202 6

Four Main Rules: Declare getters/setters for all persistent properties Can use auto props if not needing INotifyPropertyChanged Do NOT need to be public DO need to match mapping Must have no-arg constructor for NHibernate s use of Activator.CreateInstance Does NOT have to be public Provide an Identifier field Maps to the <id> element in the mapping document Class s/b non-sealed and all public methods declared as virtual Enables NHibernate to wrap pocos with proxies Used in performance tuning among other magic Phone:513-619-6323 Fax:513-791-5202 7

NHibernate ILifeCycle deprecated Provides callbacks for vetoing Save/Update/Delete operations on an object.net (My Implementations) INotifyPropertyChanged Used in data binding scenarios IComparable Allows adding business rules around sorting IDataErrorInfo Mainly used in WPF applications Phone:513-619-6323 Fax:513-791-5202 8

XML Configuration is either done through: *.config (better choice) hibernate.cfg.xml Configuration section name= hibernate-configuration type= NHibernation.Cfg.ConfigurationSectionHandl er, NHibernate Loaded with NHibernate.Cfg.Configuration If no file specified, looks in *.config file, then looks for hibernate.cgf.xml Phone:513-619-6323 Fax:513-791-5202 9

Key Properties for <session-factory>: connection-provider handles connections Example shows custom provider dialect target database for SQL dialects Example shows custom dialect show_sql outputs sql calls to windows Useful for debugging purposes TURN OFF IN PRODUCTION mapping assembly that contains the mapping files Otherwise must add the files into the configuration through code Phone:513-619-6323 Fax:513-791-5202 10

Define the bridge between persistent POCO (Plain Old CLR Objects) and Database Three main paradigms: Table per Concrete Class Table per Class Hierarchy Table per Subclass Phone:513-619-6323 Fax:513-791-5202 11

Main mapping methods: XML Documents (ClassName.hbm.xml) I prefer code generation of separate XML mapping and POCO files Keeps POCO data access agnostic Attributes on POCOs Castle ActiveRecord and other tools build on this and offer additional methods Phone:513-619-6323 Fax:513-791-5202 12

When Using XML Documents: Root element must be <hibernate-mapping> Files must be set as embedded resource for Project Each persistent class/interface is defined by a <class> element More than one class per file is allowed, considered bad form Objects (and collections) can be defined for Lazy Load Overridable at query time Phone:513-619-6323 Fax:513-791-5202 13

Concurrency columns (incremented on each change): <version> - Int32 <timestamp> - DateTime (not SQL Server timestamp data type) Non-NHibernate changes will cause collisions Should use SQL Server Timestamp column (requires custom implementation) Phone:513-619-6323 Fax:513-791-5202 14

Three basic types of collections: Set = Unique items, no order Bag = no order List = ordered, each item has an index Inverse allows for bidirectional associations Lazy loading or Not? Lazy is better choice, but must initialize items prior to use Cascade All none save-update delete all-delete-orphan Order-by Batch-size Phone:513-619-6323 Fax:513-791-5202 15

When One-To-Many collections are lazy loaded, they must be initialized prior to being handled NHibernate.IsInitialized NHibernate.Initialize If Parent is in Detached state, must use session.lock When creating new objects with bidirectional collections, must code both sides: Child c = new Child(); Parent p = new Parent(); c.parent = p; p.children.add(c); Phone:513-619-6323 Fax:513-791-5202 16

Sessions are analogous to connections One ISessionFactory per database Created via ISessionFactory.OpenSession() Recommend to use transaction Eliminates need to call Session.Flush() Sample usage: using (ISession session = SessionFactory.OpenSession()) { try { using (ITransaction trans = session.begintransaction()) { try { session.update(entity); } catch (Exception ex) { transaction.rollback(); } } } finally { SessionFactory.CloseSession(); } } Phone:513-619-6323 Fax:513-791-5202 17

Always call ISession.Close() when app exits Implement IDisposable on SessionManager IInterceptor Much more robust than ILifeCycle, can be used for auditing and other tasks Phone:513-619-6323 Fax:513-791-5202 18

Single POCO query If you know the primary key value Get<T> - loads object from the database Returns null if not found Load<T> - loads the object from the cache or the database Returns proxy if not found, throws exception when accessed IList<T> queries IQuery Hibernate Query Language Similar to LINQ syntactically ISQLQuery Native SQL Based on Dialect ICriteria OO implementation Expressions can used to refine the search Stored Procedures Phone:513-619-6323 Fax:513-791-5202 19

Can use Projection to create instance of different object based on query Similar to LINQ to Objects See sample code or this: http://tinyurl.com/dmc8cy Phone:513-619-6323 Fax:513-791-5202 20

Commit Save persist a transient object Update update an object already in the data store SaveOrUpdate call appropriate method based on value in id property Only works if unsaved-value = null (mapping) Delete removes an object from the data store Updating Detached Objects Lock associates an object with the current session Ignores changes prior to Lock call Should only be used to Initialize proxies Phone:513-619-6323 Fax:513-791-5202 21

If DBA requires all Stored Procedure access to data store Add tags to the mapping file: <sql-insert>exec createperson?,?</sql-insert> <sql-delete>exec deleteperson?</sql-delete> <sql-update>exec updateperson?,?</sql-update> Current version has some issues with this Depends on several factors Should be fully functional in 2.5 Phone:513-619-6323 Fax:513-791-5202 22

NHibernate doesn t support encrypted connections Must extend DriverConnectionProvider Override GetNamedConnectionString Phone:513-619-6323 Fax:513-791-5202 23

NHibernate concurrency models: none version dirty all If anything but version, must be in the same session Version is either DateTime or a Sequence Must implement IUserVersionType And hack a little Code Sample Phone:513-619-6323 Fax:513-791-5202 24

NHibernate http://tinyurl.com/nhib-doc - Documentation http://www.manning.com/kuate/ - NHibernate in Action http://nhibernate.org - Project Home http://nhforge.org/default.aspx - Community http://groups.google.com/group/nhusers Phone:513-619-6323 Fax:513-791-5202 25

Phil.japikse@pinnsg.com www.skimedic.com/blog www.twitter.com/skimedic www.pinnsg.com (513) 619-6323 Phone:513-619-6323 Fax:513-791-5202 26