[PDF Download] Spring persistence with hibernate expert s voice in open source paul tepper fisher br

Page 1


Tepper Fisher Brian D Murphy

Visit to download the full and correct content document: https://textbookfull.com/product/spring-persistence-with-hibernate-expert-s-voice-in-o pen-source-paul-tepper-fisher-brian-d-murphy/

More products digital (pdf, epub, mobi) instant download maybe you interests ...

Spring Persistence with Hibernate Fisher Paul Murphy

Brian D

https://textbookfull.com/product/spring-persistence-withhibernate-fisher-paul-murphy-brian-d/

Hibernate Recipes A Problem Solution Approach Expert s Voice in Open Source Mak Gary Guruzu Srinivas

https://textbookfull.com/product/hibernate-recipes-a-problemsolution-approach-expert-s-voice-in-open-source-mak-gary-guruzusrinivas/

Pro Spring Dynamic Modules for OSGi Service Platforms

Expert s Voice in Open Source Rubio Daniel

https://textbookfull.com/product/pro-spring-dynamic-modules-forosgi-service-platforms-expert-s-voice-in-open-source-rubiodaniel/

Pro Hibernate and MongoDB The Expert s Voice Leonard

Anghel

https://textbookfull.com/product/pro-hibernate-and-mongodb-theexpert-s-voice-leonard-anghel/

Beginning Database Driven Application Development in Java EE Using GlassFish Expert s Voice in Open Source

https://textbookfull.com/product/beginning-database-drivenapplication-development-in-java-ee-using-glassfish-expert-svoice-in-open-source-vasiliev-yuli/

Beginning Java and Flex Migrating Java Spring Hibernate and Maven Developers to Adobe Flex Expert s Voice in Web Development Di Pisa Filippo

https://textbookfull.com/product/beginning-java-and-flexmigrating-java-spring-hibernate-and-maven-developers-to-adobeflex-expert-s-voice-in-web-development-di-pisa-filippo/

Beginning Java with WebSphere Expert s Voice in Java

Janson Robert W

https://textbookfull.com/product/beginning-java-with-websphereexpert-s-voice-in-java-janson-robert-w/

Spring Boot Persistence Best Practices: Optimize Java

Persistence Performance in Spring Boot Applications 1st Edition Anghel Leonard

https://textbookfull.com/product/spring-boot-persistence-bestpractices-optimize-java-persistence-performance-in-spring-bootapplications-1st-edition-anghel-leonard/

JavaFX 2 0 Introduction by Example Expert s Voice in Java Dea Carl

https://textbookfull.com/product/javafx-2-0-introduction-byexample-expert-s-voice-in-java-dea-carl/

Covers Spring 3, Hibernate 3.5, Grails, and Roo!

Spring Persistence with Hibernate

Guides you through the essential aspects and best practices of building a real application, using Spring Framework 3, Hibernate 3.5, Grails, and Roo

Spring Persistence with Hibernate

Spring Persistence with Hibernate

Copyright © 2010 by Paul Tepper Fisher and Brian D. Murphy

All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.

ISBN-13 (pbk): 978-1-4302-2632-1

ISBN-13 (electronic): 978-1-4302-2633-8

Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

President and Publisher: Paul Manning

Lead Editors: Steve Anglin, Tom Welsh

Technical Reviewer: Sia Cyrus

Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh

Coordinating Editor: Mary Tobin

Copy Editor: Marilyn Smith

Compositor: Kimberly Burton

Indexer: Julie Grady

Artist: April Milne

Cover Designer: Anna Ishchenko

Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springersbm.com, or visit www.springeronline.com.

For information on translations, please e-mail rights@apress.com, or visit www.apress.com

Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales

The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work.

The source code for this book is available to readers at www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code.

To Melanie, for making it all worthwhile

—Paul

I would like to dedicate this, my first print book, to my mom. I miss you always

—Brian

About the Authors

■ Paul Tepper Fisher first began working in technology at Johns Hopkins University, where he spent several years developing a distance-learning application for neuroscience, while completing graduate school there. He has founded two technology start-ups: SmartPants Media, Inc., a software development company specializing in interactive multimedia technology; and dialmercury.com, which develops telephony applications using VOIP and Java.

Paul was also Manager of Technology at Wired.com, where he led the software development team for the online publications of Wired.com, webmonkey.com, and howto.wired.com, using Spring, Grails, and Java technology.

Currently, Paul is Director of Engineering for a new Music Service at Lime Wire, where he manages several development teams using agile methodologies. Comprised of client-side and distributed server-side components, the Music Service is designed for horizontal scalability with a goal of supporting millions of end-users and terabytes of data.

You can read Paul’s blog at: http://paultepperfisher.com

■ Brian D. Murphy is the Chief Architect and Director of Engineering at Condé Nast. He was an early adopter of Spring and Hibernate and uses both technologies to power all of Condé’s brands online, including wired.com, newyorker.com, epicurious.com, and vanityfair.com, drawing tens of millions of unique visitors each month. Brian deals with the challenges of building scalable, distributed systems every single day. He has a B.S. in Computer Science from Rutgers University. You can follow Brian on Twitter at http://twitter.com/@brimurph or read his blog at http://turmoildrivendevelopment.com.

About the Technical Reviewer

Sia Cyrus ’s experience in computing spans many decades and areas of software development. During the 1980s, he specialized in database development in Europe. In the 1990s, he moved to the US where he focused on client-server applications. Since 2000, he has architected a number of middle-tier business processes incorporating Spring and Hibernate. Most recently, he has been specializing in Web 2.0, Ajax, GWT, and Android.

Sia is an independent software consultant who is an expert in Java and development of Java enterprise-class applications. He has been responsible for innovative and generic software, holding a US Patent in database-driven user interfaces. Sia created a very successful configuration-based framework for the telecommunications industry, which he later converted to Spring Framework. His passion could be entitled “Enterprise Architecture in Open Source”.

When not experimenting with new technologies, he enjoys playing ice hockey especially with his two boys, Jason and Brandon. He can be reached at sia.cyrus@comcast.net.

Acknowledgments

Writing a book always ends up being more difficult than you initially imagined. Although the absurdly late nights and lost weekends prove difficult to the authors, it is often the people around them that end up suffering the most. To that end, I’d like to thank Melanie Colton for her endless patience and perseverance. She deserves more than a medal for putting up with the many 4am nights and my noisy typing. This book would not have been possible without her support, understanding, and muse.

I would also like to acknowledge my colleagues at Lime Company, for their continued trust and support. It is a rare experience to work with such a talented and committed group of people, and I am grateful for the opportunity to be a part of such an important adventure.

I’d also like to thank Solomon Duskis for starting this journey, and for his unwavering enthusiasm for technology—especially Java and Spring.

I would be remiss if I didn’t offer my appreciation and gratitude to my parents, who have inspired me through their relentless trust, support, and faith in everything I set out to do.

Finally, my sincere appreciation goes to Brian Murphy for joining the project and keeping things rolling along. If it hadn’t been for Brian’s tenacity and motivation, this book would never have seen the light of day. It’s been an honor and privilege working with you again.

We’d like to thank Apress for the opportunity to write this book. Special thanks to Steve Anglin for believing in us and letting us stretch the schedule to cover advanced topics in depth. We owe Mary Tobin a special debt of gratitude for shepherding us through this process and ultimately dragging us across the finish line. Thanks to Tom Welsh, Marilyn Smith, and Sia Cyrus, who provided invaluable feedback, suggestions and encouragement along the way. This is a much better book as a result of their wisdom and patience. Any issues or errors in this text are ours alone.

I would like to thank my wife, Dania, without whom this book wouldn’t be possible. She graciously took on the role of super mom while I devoted nights and weekends to writing for far longer than bargained for. I’d like to thank my son Liam for being the most terrific little kid. You provide me with more joy and a new appreciation for the world than you’ll ever know. I’d also like to acknowledge our second son, who is due shortly after this book will be published. I can’t wait to meet you!

Lastly, I’d like to thank Paul Fisher for sharing this experience with me. This book was Paul’s brainchild and I’m glad he invited me along for the ride. Writing this book has been both rewarding and challenging. I learned a ton and it’s been great to work with you again.

Preface

Since its inception, the Spring Framework has gradually changed the rules of application development in the Java community. This book is the ideal guide and teaching companion for developers interested in learning about the Spring Framework and how it can be leveraged to build persistence-driven applications using Hibernate, one of the most popular Java persistence frameworks today. Spring Persistence with Hibernate gets you rolling with fundamental Spring concepts, as well as proven design patterns for integrating persistence into your applications.

Many of the lessons illustrated in this book were culled from years of practical experience building scalable, high-volume web applications using Spring and Hibernate. One of the details that stands out in our joint experience is the importance and benefit of learning through hands-on experience. To this end, we will build a real-world application that utilizes Spring 3, Hibernate 3.5, JPA 2.0, Hibernate-Search, Grails, Spring Roo, and Dozer. We firmly believe that learning about Spring and Hibernate implies far more than simply understanding the respective APIs of each framework. To be able to effectively develop with these two amazing technologies, it is necessary to understand the design patterns and best practices for getting the best from these frameworks, and building on them in a consistent, proven manner. We hope this book will teach you more than just how to use Spring and Hibernate together. Our goal is to channel the development experience, lessons, and best practices we’ve seen work successfully in our experience, so that you can apply these skills and tools in your own applications.

Throughout these pages, we will introduce core Hibernate fundamentals, demonstrating how the framework can be best utilized within a Spring context. We will start with foundational concepts, such as strategies for developing an effective domain model and DAO layer, and then move into querying techniques using HQL, JPQL, and the Criteria API. After fundamental concepts are introduced, we will move on to more advanced topics, such as fetching and caching strategies. We will also illustrate several approaches for architecting a transactional service facade. Both programmatic and declarative transactions will be examined, showcasing the benefits of using Spring for expressing transactional semantics.

Spring Persistence with Hibernate will also introduce JPA, covering its history and the ways in which Hibernate influenced its development. We will discuss the benefits of following the JPA standard, as well as when it makes sense to utilize Hibernate-specific features. The book will also introduce Grails and GORM, illustrating the differences between the DAO and Active Record patterns. We will port our sample application (which will be developed in the course of the book) into both Grails and Spring Roo, highlighting the benefits and differences of using a rapid-development, convention-over-configuration platform. In these sections, we will explore topics related to concurrency/optimistic locking, Hibernate Session state, caching approaches, and transaction management.

The last part of the book will introduce several advanced techniques, important for working with enterprise Spring/Hibernate applications. We will illustrate some of the pitfalls with integrating legacy databases, as well as best practices for developing REST web services, handling Hibernate proxies and lazy collections, as well as building search functionality using Hibernate-Search.

Here are some of the main topics we will discuss in this book:

• Basic Spring Framework features such as IoC and AOP

• Core concepts for architecting a well-layered persistence tier

• JPA concepts and steps for integrating JPA

• Foundational and advanced concepts for working with Hibernate

• Hibernate querying techniques

• DAO and Service Facade layer development

• Grails, along with the introduction of Active-Record Pattern

• Introduction of Spring Roo

• Building a REST web service

• Translating between a domain model and a DTO using Dozer

• Leveraging other frameworks and technologies, such as Hibernate-Search

• Advanced Caching and Integration strategies

Architecting Your Application with Spring, Hibernate, and Patterns

Persistence is typically the lifeblood of an application, providing the long-term memory that software requires in order to be useful across multiple invocations. Despite its importance, the architecture of a persistence tier is rarely granted adequate consideration during the design or implementation stages of an application. The consequences of this lack of planning can be far-reaching and devastating to an organization.

The primary goal of this book is to provide you with the best practices, tools, and strategies required to architect and implement a solid and effective persistence tier. Many of the concepts found on these pages were gleaned from real-world, practical experience designing and building web applications intended to scale to millions of daily users. Our objective is to illustrate the patterns and approaches that have worked for us, while examining the integration details for using Spring and Hibernate in your own applications.

One important lesson we’ve acquired over the years is that it’s often best to learn by example. To this end, we will be building a real-world application over the course of the book: an Image Gallery web application, which allows users to view slideshows and exhibitions curated by administrators. To emphasize proven, pragmatic solutions and architectural patterns for building scalable and maintainable applications, each chapter will focus on a different aspect of application development, in regards to persistence. Through illustrated code samples and discussion, we will trace the design, architecture, and implementation of a real working application. Starting with the foundation, each successive chapter will build upon the previous one, adding new layers, features, and tests. And of course, as with any real-world application, we will do significant refactoring as we discover new capabilities of Spring and Hibernate, as well as alternative strategies and frameworks. In fact, the last two chapters will re-architect our Image Gallery application entirely, as we examine two new frameworks founded on the concept of “convention over configuration.” Intended for a more rapid style of development, Grails and Roo offer a more holistic and consistent development environment with powerful features popularized by frameworks from dynamic languages, such as Ruby on Rails and Django.

The Benefit of a Consistent Approach

As you will learn throughout this book, the manner in which data is saved and queried is an integral part of every application. In fact, the persistence layer often serves as the foundation upon which an application is built. Building on top of this foundation are the three core components of a standard Spring-based persistence tier: the domain model, the Data Access Object layer, and the service facade.

Don’t worry if some of these terms are unfamiliar to you. In the following chapters, we will explain the purpose and function of each of these components, demonstrating the role each plays in an application. While we don’t suggest that there is only one correct approach to architecting an application, we do want to emphasize the benefit of using key design patterns and best practices. This is a theme that you will see cropping up over and over again.

The Significance of Dependency Injection

The Spring Framework has helped to take much of the guesswork out of designing and building an application. It has become the de facto standard for integrating disparate components and frameworks, and has evolved far beyond its dependency injection roots. The purpose of dependency injection is to decouple the work of resolving external software components from your application business logic. Without dependency injection, the details of how a component accesses required services can get muddled in with the component’s code. This not only increases the potential for errors, adds code bloat, and magnifies maintenance complexities; it couples components together more closely, making it difficult to modify dependencies when refactoring or testing.

By its very nature, Spring helps to enforce best coding practices and reduce dependency on external frameworks, or even classes within an application. At the simplest level, Spring is a lightweight IoC container, meaning that it will assume the responsibility of wiring your application dependencies. Exactly how this wiring responsibility is handled will be discussed in depth throughout this book. However, a theme you will see replayed throughout these pages is how Spring effortlessly ties components together in a loosely coupled manner. This has far-reaching effects for any application, as it allows code to be more easily refactored and maintained. And in the context of this book, it allows developers to build a persistence tier that is not directly tied to a particular implementation or framework.

Spring owes much of its success to the sheer number of integration points it provides, covering a wide range of frameworks and technologies. As developers realized the benefits gleaned from using Spring for integrating the various components within their own code, many new abstractions appeared that relied on Spring to integrate popular open source frameworks. Using Spring to integrate a particular framework not only simplifies the introduction of the framework, it allows the integration to be performed in a consistent manner—no different than the way collaborating components are wired within the context of an application. Additionally, using Spring’s dependency injection to wire in a key framework ensures the integration is done in a decoupled way.

One of the leading catalysts for Spring’s adoption was its support for the open source, objectrelational mapping (ORM) framework, Hibernate. As the Spring Framework began to grow in popularity, the Java development community was also buzzing about Hibernate. It was a pivotal time for open source frameworks, as both Spring and Hibernate offered revolutionary solutions that would change the way many new applications were architected and implemented. As you will see, Spring and Hibernate complement each other in numerous ways, and each is partially responsible for the other’s success and widespread adoption.

A Synergistic Partnership

In this book, we will focus on showing how Spring and Hibernate can be used together most effectively. Nevertheless, we will still emphasize strategies for decoupling Hibernate from your application. This is not because we have any concerns about using Hibernate, but because loose coupling provides a cleaner separation of concerns.

No matter how good a framework might be, it’s always better to keep dependencies decoupled. Not only does an agnostic persistence tier lead to better, cleaner, more maintainable code (as well as portability from one persistence technology to another), but it also ensures consistency across your application. Suddenly, your code is supported by a backbone that handles dependency wiring, provides aspect-oriented programming (AOP) capability, and generates cohesive configuration metadata that implicitly documents the way your application’s pieces fit together.

Spring encourages design practices that help to keep all of your application’s dependencies decoupled. Whether it be an external framework, an application component, or even Spring or Hibernate themselves, ensuring that collaborating components are not directly tied together helps prevent the concerns of one layer from leaking into another. By delegating all your wiring details to Spring, you not only simplify your code base by relieving the need to create infrastructural “access code,” you also ensure that components are kept distinct. In the next few chapters, you will learn how coding to interfaces and using Spring’s ORM abstractions and generic exception hierarchy can help to achieve these goals.

The Story of Spring’s and Hibernate’s Success

The rise in Spring’s popularity stems from more than just its ability to reduce code complexity by helping to wire dependencies together. Much of the early excitement around the Spring Framework was due to its support for other leading open source frameworks, including Hibernate. Hibernate was one of the first open source ORM frameworks that provided an enterprise-level solution for building a persistence tier. Spring’s ability to externalize integration details to an XML configuration file or express dependency injection through Java annotations provided a powerful abstraction that greatly simplified and standardized the integration efforts required to bootstrap Hibernate into an application.

ORM frameworks provide an abstraction layer over the actual persistence technology being used (usually a relational database), allowing developers to focus on the object-oriented details of their domain model, rather than lower-level database concerns. There is an inherent impedance mismatch between the relational-table world of databases and the object-oriented world of Java, making an effective ORM abstraction difficult to implement. This impedance mismatch is due to the fundamental differences between relational databases and object-oriented languages, such as Java. For example, relational databases don’t implement core object-oriented principles such as polymorphism, encapsulation, and accessibility. Furthermore, the notion of equality is vastly different between Java and SQL. We will discuss some of these differences throughout this book, examining approaches to bridging the gap between a SQL database and a Java domain model.

Hibernate represented a significant step in bridging this gap by offering a powerful open source framework for expressing an object-oriented domain model, and defining the ways in which the tables and columns of a database synchronized with the object instances and properties in JavaBeans.

A Better Approach for Integration

Despite the improvements and efficiency with which a persistence tier could now be developed, integrating Hibernate into an application could still be a painstaking endeavor. With no standardized integration approach, developers were left to continuously reinvent the wheel, spending significant resources on the development and maintenance of the infrastructure code required to wedge Hibernate into their applications.

As Hibernate grew in popularity, the Spring Framework started to gain momentum as well. When Spring first came on the scene, its mission was to make the development of server-side Java applications simpler. First and foremost, it offered a better solution to wiring application dependencies together. For this reason, Spring is often referred to as a container, meaning that it offers a centralized abstraction for

integrating collaborating dependencies via configuration, rather than writing (often repetitive) code to handle this task

Part of Spring’s momentum stems from the way it enables applications to deliver enterprise-level features, such as declarative transactions and security, without requiring the overhead and complexity of an Enterprise JavaBean (EJB) container or forcing developers to grapple with the details of specific technologies or standards. Time has proven EJB, although powerful in theory, to be a victim of overengineering. Spring and Hibernate owe much of their success to the fact that they provide a more reasonable and effective solution than the EJB standard While Spring offers a simpler approach to declarative transaction management, Hibernate provides a more robust and intuitive ORM abstraction. Both frameworks were built and popularized by the growing need for a solution that was less complex than previous offerings With the success of Spring and Hibernate came a stronger emphasis on building applications that were simpler and lighter weight, significantly increasing both ease of maintenance and scalability.

Although dependency injection was Spring’s core purpose, the framework has evolved far beyond its original IoC foundation. The Spring Framework has expanded into other areas that naturally blend with its IoC roots. Spring now provides a pluggable transactional management layer, AOP support, integration points with persistence frameworks (such as Hibernate), and a flexible web framework, called Spring MVC. The addition of these features was a gradual process, spurred by demand and necessity.

As Hibernate’s popularity surged, developers began to rely on Spring’s persistence abstractions to simplify the often daunting task of integrating Hibernate into an application. Thanks to Spring, the process of getting up and running with Hibernate became a great deal easier Developers could start with a Spring configuration file that not only bootstrapped a Hibernate SessionFactory (allowing configuration details to be specified via standard XML), but also streamlined the invocation of myriad Hibernate operations through the use of well-crafted abstractions founded on time-tested design patterns, such as HibernateTemplate and OpenSessionInView. We will discuss these core SpringHibernate integration details in the next few chapters The important point here is that combining Spring and Hibernate affords developers an extremely powerful solution.

Not only does Spring simplify the integration of Hibernate, but it also reduces the coupling of Hibernate to an application If the need arises to switch to a different ORM or persistence technology, this migration effort becomes much easier because there are few direct dependencies on Hibernate itself. For example, Spring provides a generic exception hierarchy for persistence-related errors. Although not required, it is considered good practice to convert Hibernate exceptions to Spring’s generic exception hierarchy, which further decouples your application from Hibernate. Spring includes built-in mechanisms to simplify this conversion, to the point that it is fairly transparent. Additionally, Spring’s integration code for other persistence technologies (such as JDBC, JPA, TopLink, etc ) will also handle the translation to Spring’s generic exception hierarchy, further simplifying a migration from one persistence technology to another.

Establishing loosely coupled dependency relationships is one of Spring’s core purposes In fact, the framework itself limits direct coupling to itself as much as possible, meaning that your application will rarely be directly tied to Spring classes.

Best Practices for Architecting an Application

The more your code is abstracted away from interfacing directly with a database (and dealing with these lower-level concerns), the easier it is to switch to a different database or persistence technology. Similarly, Hibernate also offers an abstraction over your data model, allowing you to focus on your application’s persistence details rather than on the particulars of your database. Through these decouplings, a persistence tier becomes far more portable across disparate databases.

Spring centralizes the wiring of dependencies within your application, making maintenance and configuration easier, and coercing developers to code to interfaces, which brings about cleaner and better code. It also allows you to focus more on your application’s business logic, with less concern over how this information is physically stored and retrieved. This concept is often called layering. Each layer is focused specifically on accomplishing a particular task (with little knowledge or coupling to other layers within the application).

The Layers of a Persistence Tier

The application tier that deals with persistence is often called the persistence tier. Spring helps to enforce a modular architecture in which the persistence tier is divided into several core layers that contain the following:

• The Domain Model

• The Data Access Object (DAO) Layer

• The Service Layer (or Service Façade)

Each of these layers is representative of proven design patterns that are key to building a solid, maintainable architecture. Outside the persistence tier, a typical Spring MVC application also has a controller layer, which handles user interaction, delegating to the service facade and shuttling necessary data back to the view. We will get into these implementation details over the next few chapters. Here, we’ll take a brief look at the domain model, DAO, and service layers.

The Domain Model

The domain model represents the key entities within an application, defining the manner in which they relate to one another. Each entity defines a series of properties, which designates its characteristics, as well as its relationships to other entities. Each class within the domain model contains the various properties and associations that correlate to columns and relationships within the database. Typically, there is a domain entity for each table within the database, but this is not always the case.

For example, we might need to define a Person domain entity, designed to represent the concept of a person within the application and the database. The Person class could be represented as follows:

@Entity

public class Person implements Serializable {

private Long id; private String firstName; private String lastName; private String username; private String password; private Integer roleLevel;

private Integer version; public Person() {

}

@Id public final Long getId() { return id; }

@Version public Integer getVersion() { return version; }

.

. . Remaining Getters and Setters Omitted

Part of Hibernate’s job is to convert between domain model instances and rows in the database. Developers provide hints to help Hibernate perform these conversions, by specifying mapping rules using XML or annotations. This metadata is used by Hibernate to define the characteristics of the domain model and how the object-oriented properties within a domain model class map to columns and relationships within the database.

Although XML was initially used to define mapping rules, we recommend using annotations as this approach is simpler and more concise. In fact, by applying the @Entity annotation to a class, it is assumed that a class property should be persisted to the database using the property name as the database column name and using the field type as a hint for the database column type. Of course, all these details can be explicitly configured or overridden, but thanks to sensible defaults, your mapping configuration should be relatively terse most of the time.

The Data Access Object (DAO) Layer

The DAO layer defines the means for saving and querying the domain model data. A DAO helps to abstract away those details specific to a particular database or persistence technology, providing an interface for persistence behavior from the perspective of the domain model, while encapsulating explicit features of the persistence technology. The goal of the DAO pattern is to completely abstract the underlying persistence technology and the manner in which it loads, saves, and manipulates the data represented by the domain model. The key benefit of the DAO pattern is separation of concerns—the lower-level details of the persistence technology and datasource are abstracted into a series of methods that provide querying and saving functionality. If the underlying persistence technology changes, most of the necessary changes would be limited to defining a new DAO implementation, following the same interface.

For example, we might create a PersonDAO class to define all the application’s persistence needs related to the Person entity. In PersonDao, we would likely have a method like the following:

public Person getPersonById(Long id);

This method would be responsible for loading a Person entity from the database using its unique identifier.

The following might be another method for our application: void savePerson(Person person);

This method would be designed to handle all updates to a given row in the Person table (that is, creation or modifications).

When defining a DAO, it is good practice to first write the interface, which delineates all the core persistence-related methods the application will need. We recommend creating separate DAOs for each persistent entity in your domain model, but there are no clear rules in this area. However, defining DAO methods in a separate interface is crucial, as it decouples the purpose and contract of the DAO from the actual implementation, and even allows you to write more than one implementation for a given DAO interface.

It’s important to note that such an interface is agnostic to the persistence technology being used behind the scenes. In other words, the interface only depends on the relevant domain model classes, decoupling our application from the persistence details. Of course, the DAO implementation class will use Hibernate, JPA, or whatever persistence technology we have chosen to employ. However, the higher layers of our application are insulated from these details by the DAO interface, giving us portability, consistency, and a well-tiered architecture.

As we mentioned earlier, the Spring Framework also provides a generic data exception hierarchy, suitable for all types of persistence frameworks and usage. Within each persistence framework integration library, Spring does an excellent job of converting each framework-specific exception into an exception that is part of Spring’s generic data-access exception hierarchy. All of the exceptions in Spring’s generic exception hierarchy are unchecked, meaning your application code is not required to catch them. Spring helps to decouple your application from a particular persistence framework, allowing you to code to a generic and well-defined exception hierarchy that can be used with any persistence technology.

In Chapter 6, we will dive deeper into DAO implementation strategies, exploring the flexible querying and save/update capability afforded by Hibernate and JPA. Querying in particular can require quite a bit of complexity, and to this end, Hibernate and JPA provide two different approaches for searching and accessing your data. HQL and JPQL (Hibernate Query Language and Java Persistence Query Language, respectively) both offer an object-oriented syntax for expressing queries that is very similar to SQL. Although concise and intuitive, HQL and JPQL are interpreted at runtime, which means you cannot use the compiler to verify the correctness and integrity of a query.

To address this limitation, Hibernate also includes a Criteria API, which allows queries to be expressed programmatically. Until recently, JPA did not offer a Criteria API, which meant developers would have to go outside the JPA standard if they required this type of querying facility. However, with the introduction of JPA 2.0, a Criteria API is now available as part of the standard.

Whether to use HQL/JPQL or the Criteria API is sometimes a matter of style. However, there are some cases where the Criteria API is more effective and maintainable. For instance, if you are building a feature that requires dynamic filtering or ordering, being able to dynamically create a query programmatically, based on the user’s runtime specifications, is much cleaner than attempting to dynamically generate a JPQL query string via concatenation. We will discuss these types of implementation decisions further in Chapter 6.

The Service Facade

The layer that handles the application business logic is (surprisingly enough) called the service layer. The service layer typically defines an application’s public-facing API, combining one or more lower-level DAO operations into a single, cohesive transactional unit.

To help you understand how a service layer is built and used, let’s take a look at a few examples:

Person loginUser(String username, String password);

The loginUser() method is intended to authenticate a user (that is, verify that the specified username and password match), and then load important user information into the session (grab user information, such as name, previous login date, role type, and so on). These tasks would likely not be handled by a single DAO method. Instead, we would probably build upon the functionality of two distinct DAO classes, a PersonDAO and a RoleDAO:

interface PersonDao {

Person authenticatUser(String username, String password);

interface RoleDao {

List<Role> getRolesForPerson(Person person);

Together, these DAO methods accomplish a core business goal that is greater than the sum of its parts. In this example, we are using two read-only methods, but imagine a scenario in which we have a business method, such as the following:

boolean transferMoney(Long amount, Account fromAccount, Account destAccount) throws InvalidPermissionException, NotEnoughFundsException;

Now, assume that the preceding service layer method is composed of several DAO methods:

boolean validateSufficientFundsInAccount(Long accountId);

boolean removeFunds(Long accountId, Long amount);

boolean addFunds(Long accountId, Long amount);

It’s easy to see what’s going on here: we verify that enough cash exists in a particular account, and then pull the funds from one account and transfer them to another. The task is simple enough, but it doesn’t take an overactive imagination to visualize the hysteria that might ensue should this business method fail halfway through the process—the funds might be withdrawn but never get deposited into the destination account. That might be good for the bank at first, but after a short while the entire economy collapses, and civilization is left with only a rudimentary barter system based on crazy straws and Star Wars action figures.

Leveraging Declarative Transactions

Service facade methods typically group together multiple DAO methods to accomplish business logic as a single unit of work. This is the concept of a transaction: the entire method (and all of its side effects) completes 100 percent successfully, or the application is rolled back to the state before the method was called. Before Spring persistence came on the scene, transactional requirements often prompted developers to look toward EJBs, which let them declaratively configure transactional semantics for each facade method. When they cannot specify transactional requirements declaratively, developers must instead use a programmatic approach. Not only does this add code complexity and obscure the intentions of the persistence logic, it further couples the persistence technology to the application. Transactional demarcation is often considered a cross-cutting concern, meaning it represents functionality that affects many parts of the codebase, but is orthogonal to their other features. Crosscutting concerns add redundancy to code, since they need to be repetitively interweaved into the fabric of the business logic of an application, reducing code modularity. Aspect-Oriented Programming is aimed at solving this problem by allowing these concerns to be expressed once, and once only, as aspects, and then weaved into business logic as necessary.

In Spring, the service layer typically is intended to accomplish three primary tasks:

• Serve as the core API through which other layers of your application will interface (this is the incarnation of the facade pattern)

• Define the core business logic, usually calling on one or more DAO methods to achieve this goal

• Define transactional details for each facade method

Understanding Aspect Oriented Programming (AOP)

The service layer is where Spring’s AOP support is best utilized. Spring ships with transactional support that can be applied to application code through the use of interceptors that enhance your service layer code, by weaving in the transactional goodness. An interceptor is code that can be mixed into the execution flow of a method, usually delegating to the interceptor before and/or after a particular method is invoked. Simply speaking, an interceptor encapsulates the behavior of an aspect at a point in a method’s execution.

It’s not enough to specify that a method should be transactional. You shouldn’t just force each method to occur within the confines of a transaction, rolling back if an error occurs and committing if all goes well. Perhaps certain methods don’t attempt to modify any data, and therefore should execute within the context of a read-only transaction. Or more likely, perhaps some exceptions will trigger a rollback, while others will allow the transaction to carry on.

Pointcuts are another important component of Spring AOP. They help to define where a particular aspect (modularized functionality that can be weaved into application logic, such as transactional behavior) should be weaved. With Spring’s transactional support, you have fine-grained control over which exceptions may trigger a commit or rollback, as well as the details over the transaction itself, such as determining the isolation level and whether a method should trigger a new transaction or a nested transaction, or execute within the existing transaction.

At a basic level, Spring accomplishes AOP through the use of the proxy design pattern. When you advise your classes by injecting cross-cutting behavior into various methods, you’re not actually injecting code throughout your application (although in a way, that is the net effect of using AOP). Rather, you’re requesting that Spring create a new proxy class, in which functionality is delegated to your existing class along with the transactional implementation (or whatever aspect you are trying to weave into your code). This explanation is an oversimplification of what actually happens under the hood, but

the important thing to remember is that when you weave cross-cutting behavior into your classes via AOP, Spring is not directly inserting code; rather, it is replacing your classes with proxies that contain your existing code intertwined with the transactional code. Under the hood, this is implemented using JDK dynamic proxies or CGLIB bytecode enhancement.

Again, it’s easy to see how this is a natural fit for a lightweight, IOC container like Spring. Since you’re already entrusting Spring with handling your dependencies, it makes perfect sense to let Spring also take care of proxying these dependencies so you can layer on new cross-cutting behavior.

Although Spring AOP is amazingly powerful when you need to define and introduce new aspects to be weaved into your implementations, key transactional functionality is available out of the box and without the need to learn the details of AOP programming concepts. Still, understanding the basics of what Spring does under the hood is helpful. Keep in mind that AOP is useful for more than just applying transactional behavior—it is helpful for weaving any cross-cutting concern into your application, such as logging or security. We will discuss AOP in more detail later in this book.

Simplifying Transactions

Although applying transactions using Spring used to require a bit of AOP know-how, this process has been greatly simplified in recent versions of the framework. Now, applying transactional behavior to a service layer class is a matter of specifying the @Transactional annotation at either the class or method level. This annotation can be parameterized with attributes to customize its behavior, however the most significant detail is whether a transaction is read-only. Many developers don’t recognize the importance of using transactions—even within a read-only context. Transactions can be useful for more than just ensuring atomicity. Transactions can also be used to specify a database isolation-level, and to delineate other contextual details that might be ambiguous outside a transactional scope. We strongly recommend that all database operations occur within the scope of some transaction—even if just to gain control over the contextual state of the database. We will discuss some of these details, such as understanding isolation levels and advanced transactional options, in Chapter 8.

The Benefit of Coding to Interfaces

We can rely on Spring to wire DAO dependencies into our service layer classes, ensuring that this integration happens in a consistent way and that the integration point between these two layers is through interfaces rather than specific implementation classes. As we mentioned earlier in this chapter, this is a fundamental concept for leveraging Spring’s dependency injection: by coding to interfaces, we get more for our money. We can always rely on Spring to automatically inject required dependencies, but by using interfaces we gain the added benefit of being able to change which implementation should be injected at runtime. Without interfaces, there are no other options—we have hard-coded which dependencies must be injected into our components. Interfaces and Spring’s dependency injection capability are a dynamic duo that offer significantly increased flexibility. For instance, without changing any code, you can choose to inject one set of dependencies for unit-testing and another in production deployment. Or you can choose which implementations to use for each environment. These are some of the benefits afforded by adherence to best practices and leveraging the Spring Framework.

Testing your Persistence Tier

As you’ll see in later chapters, this separation of concerns helps keep your code clean and ensures that details from one layer don’t interfere with the code from another layer. When it comes time for refactoring, this advantage can be significant. Perhaps even more important, these best practices are instrumental for ensuring an effective testing strategy. In Chapter 8, you will learn how Spring greatly simplifies the creation of unit and integration tests. When it comes to testing, it’s rather intuitive to see

how swapping implementations can really come in handy. Spring 3 includes a powerful TestContext framework that simplifies the creation and management of unit and integration tests—even abstracting away which test framework you happen to be using. Integration tests can often be a tricky matter, especially when you consider the details of instantiating all of a test’s dependencies and components. For example, an integration test might require access to a database, as well as test data. Spring can bootstrap the ApplicationContext and then automatically inject any required dependencies. In the case of testing persistence-related code, you can choose to have your data occur within the scope of a transaction and then automatically rollback the transaction at the completion of the test to ensure that modifications made by the test are removed.

Advanced Features and Performance Tuning

This book will also cover some more advanced persistence concepts that are indispensable in most applications, such as optimization techniques for loading and managing complex relationships and collections within your domain model. We will discuss performance and optimization strategies, such as eager fetching and caching (at both the domain level and higher abstractions). As we mentioned earlier, Hibernate offers numerous features that can be leveraged to improve application performance. For instance, Hibernate and JPA offer a great deal of flexibility for tuning HQL/JPQL and Criteria API queries. These features enable developers to minimize round-trips to the database, allowing even large data sets to be accessed with minimal SQL queries. Hibernate also provides features such as lazy-loading and powerful caching mechanisms, which can be tuned to control the size and expiration time for cached entities. Understanding how these features work, as well as the myriad options available for controlling them, is critical for maximizing performance.

Caching is an often overlooked feature which can prevent an application from realizing its full potential. In the case of caching, it is either not fully utilized, or not enough time and attention are given to tuning and testing. However, if left untuned, caching can significantly degrade application performance. In Chapter 10, you will learn how Hibernate caching works, strategies for tuning and improving performance, and how to integrate a cache provider, such as ehcache. We will also explore several common pitfalls responsible for performance problems, such as the N+1 Selects issue, and how to go about identifying and resolving these issues.

Hibernate-Search

Sometimes, your application will require more than Hibernate or Spring have to offer. So we will discuss some important frameworks that extend Spring and Hibernate, such as Hibernate-Search. HibernateSearch integrates the popular open source search framework, Lucene, into a Hibernate or JPA application. For features that require true search functionality, a relational database is not able to provide the capability that Lucene is able to offer. Hibernate-Search seamlessly integrates Lucene into your persistence tier, allowing you to execute Lucene queries within the scope of a Hibernate Session or a JPA Persistence Context. In Chapter 10, you will learn how this integration works, as well as the range of functionality afforded by Lucene and Hibernate-Search.

Building a REST Web Service

Since many applications use Spring and Hibernate as part of a web application, we will explore some of the potential issues and work-arounds related to building web applications. We will develop a RESTbased web service, to explore some strategies for marshalling domain entities back and forth between Java and JSON or XML. We will examine frameworks, such as Dozer, which help to reduce some of the

Another random document with no related content on Scribd:

lime that sulfuric acid exists almost always. In addition to this there is also some sulfur combined with the organic matter of the soil.

By digesting a soil for six hours with hot, concentrated nitric acid the sulfates are dissolved, and there is transformed into sulfuric acid an important part of the sulfur which is combined with the humic substances. The quantity of soil to be operated upon should be about fifty grams.

After filtering and washing with hot water the filtered liquor is collected, in the French Commission method,[249] in a flask and carried to boiling, and five cubic centimeters of a saturated solution of barium chlorid or sufficient to be in slight excess are added. The boiling is continued for some minutes and the flask is allowed to stand for twenty-four hours. The filtrate is received upon a filter and washed with boiling water. The filter is dried and incinerated, allowed to cool, and as there may have been a slight reduction of the sulfate a few drops of nitric acid are added and a drop of sulfuric acid. It is now evaporated to dryness on a water-bath, heated to redness for a few moments, cooled and weighed. The weight of the barium sulfate obtained multiplied by 0.3433, gives the quantity of sulfuric acid obtained from the fifty grams of soil.

If it is desired to estimate only the sulfur which exists in the form of sulfate it is necessary to treat the soil with hydrochloric acid in a very dilute state, heating for a few moments only and afterwards precipitate by barium nitrate. If, on the other hand, it is desired to estimate the total sulfur which is sometimes of great interest, it is necessary to employ the process of Berthelot and André.

385. Method of Berthelot and André.—Sulfur may exist in the soil in three forms; viz.,

1. Mineral compounds, consisting generally of sulfates and sometimes of sulfids.

2. Sulfur, existing in ethereal compounds or their analogues, as in urine.

3. Organic compounds containing sulfur.

Estimation of Total Sulfur.—The principle on which this operation, as described by Berthelot and André, rests is that already described for phosphorus; viz., oxidation in a current of oxygen and

passing the vapors over a column of alkaline carbonate at or near a red heat.[250]

The ordinary methods of oxidation in the wet way give generally inexact results.

Estimation of Sulfur Pre-existing as Sulfates.—The sample is treated with cold, dilute hydrochloric acid. The filtrate is treated with barium chlorid, the precipitate collected, dried, ignited, washed with a mixture of sulfuric and hydrofluoric acids to remove silica, and afterwards weighed as barium sulfate.

Estimation of Sulfur as Sulfids.—The sample is distilled with dilute hydrochloric acid, and the hydrogen sulfid produced is made to pass through an acidulated solution of copper sulfate in such a way as to transform the sulfur in the hydrogen sulfid into a sulfid, which is afterwards collected and weighed in the usual way. The use of a titrated solution of iodin is not advisable on account of the organic matter which may be present.

Estimation of Sulfur in Ethereal Compounds.—These compounds can be decomposed by boiling with a solution of potash or concentrated hydrochloric acid. The resulting sulfuric acid is precipitated with barium chlorid. Subtract from the sulfates thus obtained those pre-existing as sulfates; the difference represents the sulfur present in ethers.

Estimation of Sulfur in Other Organic Compounds.—This is estimated indirectly by subtracting from the total sulfur that present as sulfates, sulfids, and ethers.

386. Method of Von Bemmelén.—As Von Bemmelén[251] observes, the estimation of sulfuric acid in soils presents a number of difficulties. A small part of it can be present as sulfate insoluble in water. In addition to this, there is always some sulfur in the organic bodies present. If the soil is extracted with water then the sulfuric acid can be estimated therein when only a trace of humus substance has gone into solution. On the contrary, if there is much humus substance in solution, and also iron oxid, as is the case when the extraction is made with hydrochloric acid, then both of these must be removed, otherwise the estimation is very inexact. By fusing the residue of the solution with sodium carbonate and a little potassium nitrate the organic substance is destroyed, and after treatment with

water the iron oxid is separated. If any sulfur has been dissolved in the organic substance present, this is then oxidized to sulfuric acid. The estimation of the sulfuric acid and of the sulfur, therefore, remains unsatisfactory.

In a sample of clay from Java, which was rich in calcium carbonate, but which contained no basic iron sulfate, there was found the following percentages of sulfuric acid:

Exhausted in the cold with very weak hydrochloric acid, 0.04 per cent; the residue treated in the cold with concentrated hydrochloric acid, the solution evaporated and fused with sodium carbonate and potassium nitrate, 0.07 per cent; again, the residue treated with aqua regia to oxidize the sulfur, the solution evaporated to dryness, fused with sodium carbonate and potassium nitrate, 0.14 per cent; in all 0.25 percent. A sample of the same soil treated directly with aqua regia, and then evaporated and fused as above, gave two-tenths per cent sulfuric acid. A sample of the same soil ignited in a crucible with sodium carbonate and potassium nitrate gave 0.16 per cent of sulfuric acid. The difference between 0.04 and 0.07 per cent can be attributed to the sulfur in the organic substance which was dissolved by the concentrated hydrochloric acid; the quantity, however, is too small to draw any safe conclusion. Possibly it might have been that the very dilute hydrochloric acid did not dissolve all of the sulfate. The quantity of sulfur combined in the organic substance in the above soil may be derived from the following equation; viz., 0.2–0.07 80 × 32 = 0.05 per cent of sulfur.

The estimation of the sulfur in a sample of soil from Deli was carried on with still greater exactness by three different methods.

The quantities of hydrochloric acid, nitric acid, and sodium carbonate employed were measured or weighed, and the minute content of sulfuric acid therein estimated and subtracted from the final results. The methods employed were as follows:

(A) Extraction with water and afterwards with very dilute hydrochloric acid.

(B) Extraction with cold hydrochloric acid, one part to three of water.

(C) Extraction with aqua regia.

(D) Ignition with sodium carbonate and potassium nitrate.

(F) Ignition in a combustion tube with sodium carbonate in a stream of oxygen.

The percentages of sulfuric acid obtained by the different methods were as follows:

(A) 0.058 per cent. (B) 0.070 „ „ (C) 0.140 „ „ (D) 0.125 „ „ (F) 0.106 „

387. Method of Wolff.—In regard to the sulfuric acid Wolff calls attention to the fact that in soils which have been ignited, a larger quantity of this acid is found than in soils containing humus.[252] This, doubtless, arises from the oxidation of the organic sulfur. The following special method for determining the sulfuric acid is therefore proposed:

Fifty grams of fresh air-dried soil are placed in a platinum dish with a concentrated solution of pure sodium nitrate. After drying, the heat is raised gradually to redness. In this way the complete ignition of the humus present takes place. After cooling, the mass is diluted with hydrochloric acid, with the addition of a little nitric acid, and boiled. In the solution, the silicic acid is first separated and the sulfuric acid estimated in the usual way with barium sulfate.

388. Method of the Italian Chemists.—The determination of the sulfuric acid is conducted as follows by the Italian chemists:[253] The soil is completely extracted by diluted hydrochloric acid and the sulfuric acid precipitated in the solution with barium chlorid. If a soil is very rich in calcium sulfate it should first be treated with a warm solution of sodium carbonate to decompose the calcium sulfate, and the sulfuric acid determined in the solution after having added hydrochloric acid.

389. Estimation of the Chlorin.—The estimation of the chlorin is of great importance in certain cases. When this element is lacking in the soil, which, however, is rare, certain plants appear to suffer from its absence. The quality of the forage plants in particular is influenced by it; but when the chlorids are too abundant, which is

a frequent case, they prevent or arrest completely the progress of vegetation. Salty soils are, in general, completely sterile. In the proportion of one pound in a thousand in the earth, sodium chlorid is to be regarded as injurious. It is necessary, therefore, in analysis to take account of two cases; viz., those of soils poor in chlorids and those of soils rich in chlorids.

For soils poor in chlorids the French method directs that[254] 200 grams of the earth are to be washed on a filter with boiling water. The liquor is evaporated to dryness and gently heated to a temperature inferior to redness in order to destroy the organic matter. The residue is taken up by small quantities of water and to the filtered liquor the volume of which should not exceed forty to fifty cubic centimeters are added ten cubic centimeters of pure nitric acid and a sufficient quantity of silver nitrate to produce a complete precipitation. The precipitate is vigorously shaken and allowed to stand for a few hours in a darkened locality. The precipitate is collected upon a double filter and the silver chlorid, after proper desiccation, is weighed.

When the soil is rich in chlorids it is washed as has just been described upon a filter. The wash-waters are made up to one liter and fifty cubic centimeters, equivalent to ten grams of the soil, are taken for analysis. This quantity is treated exactly as described above.

390. Wolff’s Method of Estimating Chlorin in Soils.[255]

Three hundred grams of the soil are treated with 900 cubic centimeters of pure water containing a little nitric acid for forty-eight hours with frequent shaking. Four hundred and fifty cubic centimeters are then filtered and the clear liquid evaporated to 200 cubic centimeters. The chlorin is then precipitated with silver nitrate. The quantity obtained, corresponds to that found in 150 grams of the air-dried soil.

A second method, Mohr’s, is as follows: Fifty grams of the soil are placed in a platinum dish and moistened with a concentrated solution of potassium nitrate, free from chlorin. The mass is evaporated to dryness and gradually heated to a red heat. After cooling it is moistened with water and washed into a beaker and the solid mass quickly separated. The clear liquid is poured off and the residue again washed with water. The clear liquid obtained is

saturated with acetic acid, carefully evaporated to dryness and after solution in water, filtration and the addition of a little nitric acid, the chlorin therein is precipitated by a silver nitrate solution, and the precipitate collected and weighed as usual.

391. Method of Petermann.[256]—Chlorin in the soil is estimated at the Gembloux station by digesting 1,000 grams of the sample with two liters of distilled water with frequent shaking for thirty-six hours. After allowing to stand for twelve hours with the addition of one gram of powdered magnesium sulfate to facilitate the deposition of suspended matter one liter of liquid is siphoned and evaporated in a platinum dish with the addition of a few drops of a solution of potassium carbonate free from chlorin and nitric acid. The concentrated solution is filtered, washed, and made up to 250 cubic centimeters. Take 100 cubic centimeters of the solution add some nitric acid and precipitate the chlorin with silver nitrate. The rest of the solution is reserved for the estimation of nitrate.

392. Estimation of Silicic Acid.—Direct Estimation.—The sample of soil in the method of Berthelot and André[257] is mixed with two or three times its weight of pure sodium carbonate and fused in a silver crucible until complete decomposition has taken place. The residue is dissolved in water and dilute hydrochloric acid. The silicates are decomposed by this treatment and the solution is evaporated to dryness on the water-bath, and when dry slightly heated. The silicic acid (silica) is by this treatment rendered insoluble. It is collected on a filter, washed, ignited, and weighed. The resulting compound should be mixed with ammonium fluorid and sulfuric acid, and after the disappearance of the silica the residue should be dried and weighed. The loss in weight represents the true silica. The loss in weight should be corrected by calculating the sulfates of the alkalies back to oxids. This correction can be neglected when the work has been carefully done, and the washing of the original silica has been well performed.

Indirect Estimation.—The total silica may be estimated indirectly by subtracting from the total weight of the sample the sum of the weights of the other constituents resulting from the separate estimation of each of them after decomposing the sample with hydrofluoric acid.

393. Simultaneous Estimation of Different Elements.

The operations and processes for the estimation of each of the elements have been described, but it is often best to carry on an operation in such a way as to gain time by making a single decomposition upon a quantity of soil of some considerable magnitude, and using the results of the solution for the determination of the different substances. From the operations already described it will be easy to make a combination of methods by which all or nearly all the important constituents in a soil may be determined in a single sample. Of the various methods proposed, that of the commission of the French agricultural chemists may be taken as a type.[258] In the case of the estimation of lime, potash, magnesia, and sulfuric acid, in which the operation is carried on in a soil which is not incinerated, time may be saved by digesting a considerable quantity of the soil with concentrated nitric acid for a period of five hours. It is best to take 100 grams of the soil and increase proportionally the nitric acid. The filtrate, after washing, is made up to one liter and thoroughly shaken. From this amount of liquid, portions are taken corresponding to the weights of soil upon which the operation for the determination of each of the constituents would be conducted. For example, for the estimation of lime in the case of a very calcareous earth, ten cubic centimeters representing one gram of the original sample, in the case of a soil poor in carbonates 100 cubic centimeters representing ten grams, and for the estimation of potash, magnesia, and sulfuric acid 200 cubic centimeters, representing twenty grams of the soil, should be taken. This method avoids frequent weighings of the earth and separate treatments thereof by the acid.

On the other hand, in the same portion of the solution, the different elements can be estimated. For example, for the estimation of the potash as has been indicated, in the place of precipitating as a whole the sulfuric acid, lime, etc., and of afterwards separating the magnesia in the sole aim of eliminating these bodies, they can be collected separately and weighed, thus securing at a single operation several determinations.

At the end, some barium chlorid is added and if the barium sulfate is then collected and weighed, the estimation of the sulfuric acid is effected. To the filtrate there are afterwards added some ammonia

and ammonium carbonate to precipitate, at once, the excess of barium and the iron and aluminum oxids, the lime and the phosphoric acid. This separation being effected the filtrate contains still the magnesia and the alkalies. The first can be separated by carbonation by means of oxalic acid, collected, and weighed. Finally the potash itself can be estimated in the state of perchlorate. It has thus been possible in the same suite of operations to estimate in a given quantity of the liquid, the sulfuric acid, the magnesia, the lime, and the potash.

394. Estimation of Kaolin in Soils.—True kaolin is a hydrated aluminum silicate, having the formula H₄Al₂Si₂O₉. This substance is, even in concentrated hydrochloric acid, almost completely insoluble. It contains, theoretically, 13.94 per cent of water of combination. The following methods, due to Sachsse and Becker,[259] can be used for its determination.

Estimation of the Water of Combination.—Heat from one to two grams of kaolin, dried at 100°, for half an hour in a covered platinum crucible to a temperature which shows an incipient red heat when the crucible is partly protected from the daylight with the hand. This treatment does not quite give the whole of the water of combination but nearly all of it. A kaolin is changed by this treatment into a substance which is easily soluble in dilute hydrochloric acid.

Estimation of the Kaolin in Impure Kaolins.—Mineral kaolin, or the kaolin obtained by silt analysis, is dried at 100° to constant weight. It is then heated with strong hydrochloric acid until all the matters which will pass into solution have been dissolved. The residual kaolin is then washed thoroughly with water and ignited for half an hour at a low red heat. The residual mass is a second time extracted with hydrochloric acid until silica no longer passes into solution. The soluble silica is then estimated in the usual way and calculated to kaolin. The result will give the pure kaolin in the sample examined.

The estimation may also be made as follows: Two samples of the impure kaolin are taken and dried to constant weight at 100°. One is extracted with hydrochloric acid in the manner described above and the amount of silica determined. The second is treated directly by ignition to low redness for half an hour, dissolved in hydrochloric acid and the amount of silica determined. The difference in the two

percentages of silica corresponds to the silica equivalent to the pure kaolin.

Statement of Results.—It is convenient to incorporate the data obtained by the above methods with the complete mass analysis of the silicate examined. In the sample given below the analysis was made on a clay silt obtained with a velocity of two-tenths millimeter per second.

The mass analysis gave the following data:

The loss on ignition was made up of the combined water and a trace of humus. On gentle ignition only 7.52 per cent of water came off.

The examination of the non-ignited and the gently ignited silica by means of dilute hydrochloric acid, gave the following data:

ignited.

By a comparison of these data with those obtained by the mass analysis, the following representation of the distribution of the various components in the clay is obtained:

23.52 per cent SiO₂ in the form of quartz and undecomposed silicates.

2.73 per cent SiO₂ in the form of kaolin.

25.27 per cent in the form of easily decomposable silicates and of the hydrates of SiO₂.

7.93 per cent Al₂O₃ in the form of undecomposed silicates.

0.96 per cent Al₂O₃ in the form of kaolin.

9.04 per cent Al₂O₃ in the form of easily decomposed silicates and of hydrates.

0.15 per cent Fe₂O₃ in the form of undecomposed silicates.

1.31 per cent Fe₂O₃ in the form of kaolin.

5.96 per cent Fe₂O₃ in the form of easily decomposable silicates and hydrates.

3.40 per cent of alkalies and alkaline earths in the form of undecomposed silicates.

9.69 per cent of alkalies and alkaline earths in the form of easily decomposable silicates.

10.04 per cent of water, including a trace of humus.

Collecting these results the following statement is obtained.

The clay analyzed contained:

10.04 per cent of water, a trace of humus.

35.15 per cent of undecomposed silicates and quartz.

5.00 per cent of kaolin.

50.27 per cent of easily decomposable silicates, hydrates of SiO₂ and hydroxids.

ESTIMATION OF NITROGEN IN SOILS.

395. Introductory Considerations.—The great economic and biologic value of nitrogen as a plant food renders its estimation in soils of especial importance. It is necessary, first of all, to remember that the nitrogen present in soils may be found in three forms; viz., first, in organic compounds, second, as ammonia, and third, as nitric or nitrous acid. Further than this each of these classes of nitrogen may be subdivided. The organic nitrogen may be in a form easily nitrified and rendered available for plant food, or it may be inert and resistant to nitrification, as in humus, or exist in an amid state. The ammoniacal nitrogen may exist in small quantities as gaseous ammonia, or be combined with mineral or organic acids. As nitric or nitrous acid the nitrogen will be found combined with bases, or perhaps in minute quantities as free acid, in passing under the influence of the nitrifying ferment from the organic to the inorganic state. To the latter state it must finally come before it is suited to absorption by plants.

In general, far the largest part of soil nitrogen, excluding the atmosphere diffused in the pores of the soil, is found in the organic state and is derived from the débris of animal and vegetable life and from added fertilizers. As ammonia, the nitrogen can only be regarded as in a transition state, arising from the processes of decay, or incomplete nitrification. As nitric acid, it is found as a completed product of nitrification, or as the result of electrical action. The processes of nitrification and the isolation and determination of the nitrifying organisms will be considered in a special chapter of this manual. By reason of the great solubility of the nitrates, and the inability of the soil to retain them, there can never be a great accumulation of nitric acid in the soil save in localities deficient in rain-fall or in specially protected spots, such as caves. The nitric acid, therefore, produced in the soil passes at once into growing vegetation, or is found eventually in the drainage waters.

The formation of ammonia in soil containing much vegetable matter is thought by Berthelot and André[260] to be due to the

progressive decomposition of amid principles under the influence of dilute acids or alkalies, either in the cold or at an elevated temperature. Soils of the above description, of themselves, contain neither free ammonia nor ammoniacal salts, and the ammonia which is found in the analysis of these soils comes from the reaction above indicated. The ammonia which comes from these soils, in place of what is given off to the surrounding atmosphere, comes from the same class of decompositions, and these decompositions, in this case, are effected by the water itself, and by the alkaline carbonates of the soil. The amid principles which are thus decomposed belong either to the class of amids proper, derived by the displacement of hydrogen in ammonia by acids, or to the class of alkalamids derived from nitrogenous bases, both volatile and fixed. Among these alkalamids some are soluble in water and some insoluble, and the decomposition of these last by acids or by alkalies may furnish bodies which themselves are either soluble or insoluble in water.

To determine the nature of the nitrogenous principles in a soil rather rich in humus, Berthelot and André applied the following treatment:

A soil containing 19.1 grams of carbon and 1.67 grams of nitrogen per kilogram was first subjected to treatment, at room temperature, with a concentrated solution of potash. By this treatment 17.4 per cent of the nitrogen content was set free under the form of ammonia. One-quarter of this was obtained during the first three days; oneeighth during the next three days. Afterward the action became much more feeble and was continued during forty days longer, and the evolution of the gas was diminished almost proportionately to the time. It appears from the above observations that the amid principles of the soil, decomposable by potash, belong to two distinct groups, which are broken up with very unequal velocities. The soil, treated on the water-bath for three hours at 100° with strong potash, showed the following behavior in respect of its nitrogenous constituents: Nitrogen eliminated in the form of ammonia, sixteen per cent; nitrogen remaining in the part soluble in potash, ten per cent; nitrogen remaining in the part insoluble in potash, seventy-four per cent.

Treatment with Acid.—The nitrogenous compounds of the soil are also decomposed by dilute acids, and often more rapidly than by the

alkalies. The method of treatment is substantially the same as that set forth above. The decompositions effected either by alkalies or by acids tend in general to lower the molecular weights of the resulting products. The prolonged action of alkalies at the temperature of boiling water rendered soluble, after twenty-four hours of treatment, 93.6 per cent of the organic nitrogen found in the vegetable mould. By treating the earth successively with alkalies and acids 95.5 per cent of the total nitrogen were decomposed. These experiments show how the insoluble nitrogen in humic compounds can be gradually rendered assimilable. The action of vegetables is not assuredly identical with those which acids and alkalies exercise. However, both present certain degrees of comparison from the point of view of the mechanisms set in play by the earthy carbonates and carbon dioxid, as well as by the acids formed by vegetation. The reactions which take place naturally, while they are not so violent as those produced in the laboratory, make up by their duration what they lack in intensity.

For a more detailed study of the nature of the nitrogenous elements in soil the following method of treatment, due to Berthelot and André, is recommended:

Treatment of the Soil with Alkalies.—1. Reaction with cold, dilute solution of potash. Take fifty grams of the sample, dried at 110°, and mix with a large excess of ten per cent potash solution and place under a bell-jar containing standard sulfuric acid. The mixture is left for a long time in order to secure as fully as possible the ammonia set free.

Example: Fifty grams of a soil contained 0.0814 gram of nitrogen. Treated as above it gave the following quantities of nitrogen as ammonia:

Nitrogen as Ammonia. After 3 days 0.0034 gram. „ 6 „ 0.0054 „ „ 11 „ 0.0065 „ „ 17 „ 0.0078 „ „ 25 „ 0.0093 „ „ 41 „ 0.0107 „ „ 46 „ 0.0141 „

It is seen that the action still continued after forty days. In the space of forty days 17.4 per cent of the total nitrogen contained in the soil had been converted into ammonia by dilute potash. According to the above observations the amid principles transformed into ammonia under the influence of dilute potash, exist in groups which are acted on with very unequal rapidity.

2. Reaction with hot dilute solution of potash. Take 200 grams of the soil sample, mix with one and one-half liters of dilute potash solution containing fifty grams of potash. Place in a flask and heat on boiling water-bath for six hours. The flask is furnished with a stopper and tubes, and a current of pure hydrogen is made to pass through the liquid, having the double object of preventing any oxidizing effect from the air and of carrying away the ammonia which may be formed. The escaping hydrogen and ammonia are passed into a bulb apparatus containing titrated sulfuric acid.

The sample of soil employed contained in 200 grams, 0.3256 gram of nitrogen. There was obtained at the end of six hours’ heating, 0.0366 gram of nitrogen. In other words, 11.24 per cent of the total nitrogen in the sample appeared as ammonia.

Examination of Residue.—After the separation of the ammonia as above described, pour the residue in the flask on a filter, wash with hot water, and determine nitrogen in filtrate and in solid matter on the filter by combustion with soda-lime. The filtrate is, of course, first evaporated to dryness after being neutralized with sulfuric acid.

The insoluble part contained 0.041 gram of nitrogen, i. e., 12.84 per cent of the entire amount.

The soluble part contained 0.2411 gram of nitrogen, i. e., 74.05 per cent of the whole.

Summary of Data.—In the sample analyzed the following data were obtained:

Of the whole.

Nitrogen as ammonia 11.24 per cent.

in insoluble part 12.84 „ „

„ soluble part 74.05 „ „ „ not determined 1.87

The same experiment in which the heating on the water-bath was continued for thirteen hours gave the following data:

Of the whole. Nitrogen as ammonia 16.03 per cent.

part

Further Treatment of Matter Insoluble in Hot Dilute Potash.—A portion of the insoluble portion from the last experiment was treated for thirteen hours longer under the same conditions with dilute hot potash. The soluble and insoluble portions were determined as already described. Of the nitrogen insoluble after thirteen hours, 64.21 per cent remained insoluble after the second thirteen hours. This fact shows that slow and progressive decomposition of the alkalamids in the soil occurs under the influence of hot dilute potash.

Treatment of Matter Insoluble in Hot Dilute Potash with Hydrochloric Acid.—A part of the material insoluble in hot potash after thirteen hours is mixed with dilute hydrochloric acid, in such proportion as to have one-fifth the weight of pure hydrochloric acid to the dry solid matter. Heat in flask on a boiling water-bath for thirteen hours. Determine the nitrogen in the insoluble residue.

Example: In the case given it was found that 54.91 per cent of the nitrogen insoluble in dilute hot potash were dissolved by the hot hydrochloric acid.

This fact shows that insoluble nitrogen compounds contained in the soil are dissolved by dilute acids even more readily than by dilute alkalies at the temperature of boiling water.

Several reactions appear to take place simultaneously when potash is brought into contact with the nitrogenous principles of arable earth. Some of these principles, during the first period of the action become soluble and even form compounds which are not precipitable by acids. When, however, the action of the potash is prolonged, the dissolved bodies lose little by little a part of their nitrogen as ammonia or as soluble alkalamids. They become thus changed either to compounds no longer soluble in the potash, or to those insoluble in the solution when acidified. These compounds, it is true, contain nitrogen, but are poorer in this element and have a higher molecular weight, or, in other words, are condensation products. These last principles are not absolutely stable in the presence of potash, but are decomposed much more slowly than the original principles from which they were derived.

In general, it may be said that under the influence of alkalies on the nitrogenous principles of the soil there is a tendency to form two classes of bodies, the one more soluble with a lower molecular weight, the other less soluble with a higher molecular weight. The inverse relation between solubility and condensation is in agreement with what is observed in similar reactions with organic bodies in general. It certainly plays an important rôle in the transformations which an arable soil undergoes, either through the mild influences of the air and natural waters, or the more energetic action of vegetables themselves.

The methods of estimating nitric nitrogen will be made the theme of a special study in connection with the chapter on nitrification. There will be considered first, therefore, the methods of determining organic and ammoniacal nitrogen with only such incidental treatment of the methods for nitric nitrogen as the processes applicable to the other forms may contain.

396. Provisional Methods of the Association of Official Agricultural Chemists.[261]—The nitrogen compounds in the soil

are usually placed in three classes.

1. The nitrogen combined with oxygen as nitrates, or nitrites, existing as soluble salts in the soil.

2. The nitrogen combined with hydrogen as ammonia, or organic nitrogen easily convertible into ammonia. The ammonia may exist as salts, or may be occluded by hydrated ferric or aluminum oxids and organic matter in the soil.

3. The inert nitrogen of the soil or the humus nitrogen.

Active Soil Nitrogen.—The material proposed for reducing the nitrates to ammonia, and at the same time to bring ammonia salts and organic nitrogen into a condition for separation by distillation, is sodium amalgam. Liquid sodium amalgam may be readily prepared by placing 100 cubic centimeters of mercury in a flask of half a liter capacity, covering the warmed mercury with melted paraffin, and dropping into the flask at short intervals pieces of metallic sodium, the size of a large pea (taking care that the violence of the reaction does not project the contents from the flask), till 6.75 grams of sodium have combined with the mercury. The amalgam contains one-half per cent of sodium and may be preserved indefinitely under the covering of paraffin. To estimate the active soil nitrogen, weigh fifty grams of air-dried soil and place it in a clean mortar. Take 200 cubic centimeters of ammonia-free distilled water, rub up the soil with a part of the water to a smooth paste, transfer this to a flask of one liter capacity, washing the last traces of the soil into the flask with the rest of the water. Add twenty-five cubic centimeters of the liquid sodium amalgam and shake the flask so as to break the sodium amalgam into small globules distributed through the soil. Insert a stopper with a valve and set aside in a cool place for twenty-four hours. Pour into the flask fifty cubic centimeters of milk of lime, and distill, on a sand-bath, 100 cubic centimeters into a flask containing twenty cubic centimeters of decinormal sulfuric acid, and titrate with decinormal soda solution, using dimethyl-orange as indicator. Estimate the nitrogen of the ammonia found as active soil nitrogen. If the ammonia produced is too small in amount to be readily estimated volumetrically, determine the ammonia by nesslerizing the distillate.

Estimation of Nitrates in the Soil.—When it is desired to estimate separately the nitrates in the soil the following method may be used: Evaporate 100 cubic centimeters of the soil extract to dryness on the water-bath, dissolve the soluble portion, of the residue in 100 cubic centimeters of ammonia-free distilled water, filtering out any insoluble residue, place the solution in a flask and add ten cubic centimeters of liquid sodium amalgam, insert stopper with valve, set it aside to digest in a cool place for twenty-four hours, add fifty cubic centimeters of milk of lime, distill and titrate as above, and estimate the nitrogen as N₂O₅.

Nesslerizing may be substituted for titration when the amount of nitrates is small.

An approximate estimation of the amount of nitrates will be of value in determining which method of estimation to use. This may be done by evaporating a measured quantity of the soil extract, say five cubic centimeters, on a porcelain crucible cover on a steam-bath or radiator, having first dissolved a minute fragment of pure brucin sulfate in the soil extract. When dry pour over the residue concentrated sulfuric acid, free from nitrates, and observe the color reactions produced.

If the nitrate (reckoned as KNO₃) left upon evaporating the quantity of water taken does not exceed the two-thousandth part of a milligram, only a pink color will be developed by adding the sulfuric acid; with the three-thousandth part of a milligram, a pink with faint reddish lines; with the four-thousandth part, a reddish color; with the five-thousandth part, a red color.

By increasing or diminishing the amount of soil extract evaporated to secure a color reaction of a certain intensity, an approximate estimate may be made of the amount of nitrates present.

Blank experiments to test the acid and the brucin sulfate will be required before confidence can be placed in such estimations.

Total Nitrogen of Soils.—The total nitrogen of soils may be determined by the usual combustion with soda-lime, but this process is often unsatisfactory because of the large amount of material required when the organic matter or humus is small in amount.

A modification of the kjeldahl method is more easy to carry out and gives results equally satisfactory. Place twenty grams of soil in a

kjeldahl flask, and add twenty cubic centimeters of sulfuric acid (free from ammonia) holding in solution one gram of salicylic acid. If the soil contain much lime or magnesia in the form of carbonate, enough more sulfuric acid must be added to secure a strongly acid condition of the contents of the flask. Add gradually two grams of zinc dust, shaking the contents of the flask to secure intimate mixture. Place the flask in a sand-bath and heat till the acid boils, and maintain the boiling for ten minutes. Add one gram of mercury and continue the boiling for one hour, adding ten cubic centimeters of sulfuric acid if the contents of the flask are likely to become solid. Cool the flask and wash out the soluble materials with 200 cubic centimeters of pure water, leaving the heavy earthy materials. Rinse the residue with 100 cubic centimeters of water, and add this to the first washings. Place this soluble acid extract in a liter digestion flask, add thirty-five cubic centimeters of a solution of potassium sulfid, and shake the flask to secure intimate mixture of the contents. Introduce a few fragments of granulated zinc, pour in seventy-five cubic centimeters of a saturated solution of caustic soda, connect the flask with a condenser and distill 150 cubic centimeters into a flask containing twenty cubic centimeters of acid, using the same acid and alkali for titration used in the kjeldahl method under fertilizers.

Enter the nitrogen found in this operation as total soil nitrogen. The difference between the total soil nitrogen and the active soil nitrogen will express the inert nitrogen of the soil.

397. Hilgard’s Method.[262]—The humus determination will, in the case of virgin soils, usually indicate approximately the store of nitrogen in the soil, which must be gradually made available by nitrification. Ordinarily (outside of the arid regions) the determination of ammonia and nitrates present in the soil is of little interest for general purposes, since these factors will vary with the season and from day to day. Kedzie proposes to estimate the active soil nitrogen (ammonia plus nitrates and nitrites) by treatment of the whole soil with sodium amalgam and distillation with lime. The objection to this process is that the formation of ammonia by the reaction of the alkali and lime upon the humus amids would greatly exaggerate the active nitrogen and lead to a serious overestimate of the soil’s immediate resources.

The usual content of nitrogen in black soil-humus is from six to eight per cent in the regions of summer rains. From late determinations it would seem that in the arid regions the usually small amount of humus (often less than two-tenths per cent) is materially compensated by a higher nitrogen percentage. It thus becomes necessary to determine the humus nitrogen directly; and this is easily done by substituting in the grandeau process of humus extraction potash or soda-lye for ammonia water, and determining the nitrogen by the kjeldahl method in the filtrate.

The lye used should have the strength of four per cent in the case of potassium hydroxid, three per cent in that of sodium hydroxid. The black humus filtrate is carefully neutralized with sulfuric acid, evaporated to a small bulk in a beaker or evaporating basin, and the reduced liquid finally evaporated to dryness in the kjeldahl flask itself by means of a current of air. The beaker or basin is washed either with some of the alkaline lye, or, after evaporation, with warm concentrated sulfuric acid, which is then used in the nitrogen determination in the usual way.

For the determination of nitrates in the soil it is, of course, usually necessary to use large amounts of material, say not less than 100 grams, and, according to circumstances, five or more times that amount. In the evaporated solution the nitric acid is best determined by the reduction method, as ammonia.

Usually the soil filtrate is clear and contains no appreciable amount of organic matter that would interfere with the determination; yet in the case of alkaline soils (impregnated with sodium carbonate) a very dark colored solution may be obtained. In that case the soil may advantageously be mixed with a few per cent of powdered gypsum before leaching; or the gypsum may be used in the filtrate to discolor it by the decomposition of sodium carbonate and the precipitation of calcium humate. The evaporated filtrate can then be used for the nitrate determination by either the kjeldahl, griess, or the nessler process, which will, of course, include such portions of the ammoniacal salts as may have been leached out.

For the separate determination of these and of the occluded ammonia, when desired, it is probably best to mix the wetted soil intimately with about ten per cent of magnesium oxid and distill into

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.