alvin's blog

How to add exception annotations to Scala methods so they can be called from Java

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 17.2, “How to add exception annotations to Scala methods so they can be called from Java.”

Problem

You want to let Java users know that a method can throw one or more exceptions so they can handle those exceptions with try/catch blocks.

Solution

Add the @throws annotation to your Scala methods so Java consumers will know which methods can throw exceptions and what exceptions they throw.

How to go to and from Java collections in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 17.1, “How to go to and from Java collections in Scala.”

Problem

You’re using Java classes in a Scala application, and those classes either return Java collections, or require Java collections in their method calls.

Solution

Use the methods of Scala’s JavaConversions object to make the conversions work.

Scala: How to delete documents in a MongoDB with Casbah

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 16.8, “How to delete documents in a MongoDB with Casbah.”

Problem

You want to delete one or more documents in a MongoDB collection.

Solution

Use the findAndRemove method of the Casbah MongoCollection class to delete one document at a time, or use the remove method to delete one or more documents at a time.

Scala: How to access the MongoDB document '_id' field with Casbah

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 16.7, “How to access the MongoDB document 'ID' field (_id) with Casbah.”

Problem

You want to get the ID field for a document you’ve inserted into a MongoDB collection.

Solution

Perform a query to get the document you want, and then call get("_ID") on the resulting MongoDBObject, like this:

Scala: How to update documents in a MongoDB collection with Casbah

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 16.6, “How to update documents in a MongoDB collection with Casbah.”

Problem

You want to update one or more documents in a MongoDB collection.

Solution

Use either the findAndModify or update methods from the Casbah MongoCollection class, as shown in this example:

How to search a MongoDB collection with Scala and Casbah

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 16.5, “How to search a MongoDB collection with Scala and Casbah.”

Problem

You want to find objects in your MongoDB collection using Scala and the Casbah driver.

Solution

Use the find* methods of the MongoCollection class to get the elements you want, specifically the find and findOne methods.

Scala: How to insert documents into MongoDB with insert, save, and +=

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 16.4, “How to insert documents into MongoDB with insert, save, and +=.”

Problem

You want to save documents to a MongoDB collection from a Scala application.

Solution

Use the insert, save, or += methods of the Casbah MongoCollection class.

How to connect to a MongoDB and insert data with Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 16.3, “How to connect to a MongoDB and insert data with Scala.”

Problem

You want to use the MongoDB database with a Scala application, and want to learn how to connect to it, and insert and retrieve data.

Scala: How to connect to a SQL database with the Spring Framework

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 16.2, “How to connect to a database with the Spring Framework.”

Problem

You want to connect to a database using the Spring Framework. This gives you a nice way to add connection pooling and other capabilities to your SQL code.

Syndicate content