Spring Autowiring by Type

In Spring, “Autowiring by Type” means, if data type of a bean is compatible with the data type of other bean property, auto wire it. For example, a “person” bean exposes a property with data type of “ability” class, Spring will find the bean with same data type of class “ability” and wire it automatically. …

Read more

Spring Autowiring by Name

In Spring, “Autowiring by Name” means, if the name of a bean is same as the name of other bean property, auto wire it. For example, if a “customer” bean exposes an “address” property, Spring will find the “address” bean in current container and wire it automatically. And if no matching found, just do nothing. …

Read more

Spring DI via setter method

A simple Spring example to show you how to dependency inject a bean via setter method, the most common used DI method. 1. IOutputGenerator An interface and implemntation class of it. package com.mkyong.output; public interface IOutputGenerator { public void generateOutput(); } package com.mkyong.output.impl; import com.mkyong.output.IOutputGenerator; public class CsvOutputGenerator implements IOutputGenerator { public void generateOutput() { …

Read more

Spring 3 hello world example

This tutorial shows you how to create a simple hello world example in Spring 3.0. Technologies used in this article : Spring 3.0.5.RELEASE Maven 3.0.3 Eclipse 3.6 JDK 1.6.0.13 P.S Spring 3.0, at least JDK 1.5 is required to work. Spring 3.0 dependencies In Spring 2.5.x, almost the entire Spring modules are grouped into a …

Read more

How to install mongoDB on Ubuntu

This guide shows you how to install MongoDB on Ubuntu. Ubuntu 12.10 MongoDB 2.2.3 1. Add 10gen package to source.list.d Ubuntu 12 comes with a “mongo” package, but not the latest version. $ sudo apt-cache search mongodb mongodb mongodb-clients mongodb-dev mongodb-server It’s recommended to add 10gen package to /etc/apt/sources.list.d, as it contains the latest stable …

Read more

Spring Data MongoDB : Delete document

In Spring data for MongoDB, you can use remove() and findAndRemove() to delete documents from MongoDB. remove() – delete single or multiple documents. findAndRemove() – delete single document, and returns the deleted document. Common Mistake Don’t use findAndRemove() to perform a batch delete (remove multiple documents), only the first document that matches the query will …

Read more

Spring Data MongoDB : Update document

In Spring data – MongoDB, you can use following methods to update documents. save – Update the whole object, if “_id” is present, perform an update, else insert it. updateFirst – Updates the first document that matches the query. updateMulti – Updates all documents that match the query. Upserting – If no document that matches …

Read more

Spring Data MongoDB : Insert document

In Spring data MongoDB, you can use save(), insert() to save a or a list of objects into mongoDB database. User user = new User("…"); //save user object into "user" collection / table //class name will be used as collection name mongoOperation.save(user); //save user object into "tableA" collection mongoOperation.save(user,"tableA"); //insert user object into "user" collection …

Read more

Spring Data MongoDB hello world example

In this tutorial, we show you how to use “SpringData for MongoDB” framework, to perform CRUD operations in MongoDB, via Spring’s annotation and XML schema. Updated on 1/04/2013 Article is updated to use latest SpringData v 1.2.0.RELEASE, it was v1.0.0.M2. Tools and technologies used : Spring Data MongoDB – 1.2.0.RELEASE Spring Core – 3.2.2.RELEASE Java …

Read more

Java MongoDB : Convert JSON data to DBObject

MongoDB comes with “com.mongodb.util.JSON” class to convert JSON data directly to a DBObject. For example, data represent in JSON format : { ‘name’ : ‘mkyong’, ‘age’ : 30 } To convert it to DBObject, you can code like this : DBObject dbObject = (DBObject) JSON.parse("{‘name’:’mkyong’, ‘age’:30}"); Example See a full example to convert above JSON …

Read more

Java MongoDB : Save image example

In this tutorial, we show you how to save an image file into MongoDB, via GridFS API. The GridFS APIs are able to serve other binary files as well, like video and music files. Note For detail explanation, read this MongoDB GridFS manual. 1. Save image Code snippets to save an image file into MongoDB, …

Read more

Java MongoDB : Update document

In this tutorial, we show you how to use Java MongoDB API collection.update() to update documents. Test Data Assume following data / documents are inserted. { "hosting" : "hostA", "type" : "vps", "clients" : 1000 }, { "hosting" : "hostB", "type" : "dedicated server", "clients" : 100 }, { "hosting" : "hostC", "type" : "vps", …

Read more

Java MongoDB : Query document

In this tutorial, we show you few common ways to get or query document from collection. Test Data Insert 5 dummy documents for testing. { "_id" : { "$oid" : "id"} , "number" : 1 , "name" : "mkyong-1"} { "_id" : { "$oid" : "id"} , "number" : 2 , "name" : "mkyong-2"} { …

Read more

Java MongoDB : Delete document

In this tutorial, we show you how to use collection.remove() to delete documents from the collection. 1. Test Data Insert 10 documents from number 1 to 10 for testing. for (int i=1; i <= 10; i++) { collection.insert(new BasicDBObject().append("number", i)); } 2. DBCollection.remove() See below code snippets to delete documents. Example 1 Get first document …

Read more

Java MongoDB : Insert a document

In this tutorial, we show you 4 ways to insert below JSON data into a “document“, via Java MongoDB API. Test Data Test data in JSON format. { "database" : "mkyongDB", "table" : "hosting", "detail" : { records : 99, index : "vps_index1", active : "true" } } } 1. BasicDBObject example BasicDBObject document = …

Read more

Java MongoDB : Get collection from database

In Java, you can use db.getCollection(“your collection name”) to get a single collection to use. DBCollection collection = db.getCollection("yourCollection"); If you do not know the collection name, use db.getCollectionNames() to get the entire list of collection names from a selected database. DB db = mongo.getDB("yourdb"); Set<String> collections = db.getCollectionNames(); for (String collectionName : collections) { …

Read more

old lock file: \data\db\mongod.lock, probably means unclean shutdown

When Staring the mongoDB server, it hits below errors, and the mongoDB server is unable to start. ************** old lock file: \data\db\mongod.lock. probably means unclean shutdown recommend removing file and running –repair see: http://dochub.mongodb.org/core/repair for more information ************* Mon May 09 12:37:43 [initandlisten] exception in initAndListen std::exception: old lock file, terminating Mon May 09 12:37:43 …

Read more

Java MongoDB : Authentication example

By default, MongoDB is run in trust environment (authentication with a username and password is NOT required). In this tutorial, we will show you how to start MongoDB in secure mode / enable authentication, and connect with the Java MongoDB driver. 1. Start MongoDB in Secure Mode Start MongoDB with –auth option, now, MongoDB need …

Read more

Java + MongoDB hello world example

A simple Java + MongoDB hello world example – how to connect, create database, collection and document, save, update, remove, get and display document (data). Tools and technologies used : MongoDB 2.2.3 MongoDB-Java-Driver 2.10.1 JDK 1.6 Maven 3.0.3 Eclipse 4.2 P.S Maven and Eclipse are both optional, just my personal favorite development tool. 1. Create …

Read more

Java – How to convert Char[] to String

In Java, we can use String.valueOf() to convert a char array to a String. JavaSample1.java package com.mkyong.markdown; public class JavaSample1 { public static void main(String[] args) { char[] charArrays = new char[]{‘1’, ‘2’, ‘3’, ‘A’, ‘B’, ‘C’}; String str = new String(charArrays); System.out.println(str); // 123ABC String str2 = String.valueOf(charArrays); System.out.println(str2); // 123ABC } } Output …

Read more

Java – How to convert String to Char Array

In Java, you can use String.toCharArray() to convert a String into a char array. StringToCharArray.java package com.mkyong.utils; public class StringToCharArray { public static void main(String[] args) { String password = "password123"; char[] passwordInCharArray = password.toCharArray(); for (char temp : passwordInCharArray) { System.out.println(temp); } } } Output p a s s w o r d 1 …

Read more

How to create database in MongoDB

MongoDB didn’t provides any command to create “database“. Actually, you don’t need to create it manually, because, MangoDB will create it on the fly, during the first time you save the value into the defined collection (or table in SQL), and database. For developer from SQL background, we need to create a database, table and …

Read more

How to run MongoDB as Windows Service

This Article is outdated! Please refer to this latest guide to install MongoDB as Windows Service. A guide to show you how to installed MongoDB on Windows. 1. mongod –help Get to know all the Windows service related commands by typing “mongod –help“. C:\MongoDB\bin>mongod –help Windows Service Control Manager options: –install install mongodb service –remove …

Read more

How to install MongoDB on Windows

In this tutorial, we will show you how to install MongoDB on Windows. MongoDB 2.2.3 Windows 7 Note The MongoDB does not require installation, just download and extracts the zip file, configure the data directory and start it with command “mongod“. 1. Download MongoDB Download MongoDB from official MongoDB website. Choose Windows 32 bits or …

Read more

Hibernate component mapping example

Hibernate component represents as a group of values or properties, not entity (table). See following tutorial to understand how component works in Hibernate. 1. Customer Table See below customer table. Customer table, SQL script in MySQL database. CREATE TABLE `customer` ( `CUST_ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `CUST_NAME` varchar(10) NOT NULL, `AGE` int(10) unsigned NOT …

Read more

Hibernate – Many-to-Many example – join table + extra column (Annotation)

In this tutorial, we show you how to use Hibernate to implements “many-to-many table relationship, with extra column in the join table“. Note For many to many relationship with NO extra column in the join table, please refer to this @many-to-many tutorial 1. Many-to-many table + extra columns in join table The STOCK and CATEGORY …

Read more

Hibernate – Many-to-Many example (Annotation)

In this tutorial, it will reuse the entire infrastructure of the previous “Hibernate many to many example – XML mapping” tutorial, enhance it to support Hibernare / JPA annotation. Note For many to many with extra columns in join table, please refer to this tutorial. Project Structure Review the new project structure of this tutorial. …

Read more

Hibernate – Unable to insert if column named is keyword, such as DESC

Problem A table named “category” in MySQL database, contains a “DESC” keyword as column name. CREATE TABLE `category` ( `CATEGORY_ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `NAME` varchar(10) NOT NULL, `DESC` varchar(255) NOT NULL, PRIMARY KEY (`CATEGORY_ID`) USING BTREE ); Hibernate XML mapping file <hibernate-mapping> <class name="com.mkyong.stock.Category" table="category" catalog="mkyongdb"> … <property name="desc" type="string"> <column name="DESC" not-null="true" …

Read more

Hibernate – One-to-Many example (Annotation)

In this tutorial, it will reuse the entire infrastructure of the previous “Hibernate one to many relationship example – XML mapping” tutorial, enhance it to support Hibernate / JPA annotation. Project Structure Review the new project structure of this tutorial. Note Since Hibernate 3.6, annotation codes are merged into the Hibernate core module, so, the …

Read more

Hibernate – One-to-One example (Annotation)

In this tutorial, it will reuse the entire infrastructure of the previous “Hibernate one to one relationship example – XML mapping” tutorial, enhance it to support Hibernate / JPA annotation. Project Structure See the final project structure of this tutorial. Note Since Hibernate 3.6, annotation codes are merged into the Hibernate core module, so, the …

Read more

How to configure DBCP connection pool in Hibernate

Note Due to bugs in the old DBCP code, Hibernate is no longer maintain DBCP-based connection provider, read this Hibernate thread. Now, Apache DBCP is back to active development, and many bugs are fixed and it’s more stable now. Even Hibernate doesn’t come with connection provider like C3P0 and Proxool, but you still can configure …

Read more

Hibernate – Could not find C3P0ConnectionProvider

Problem Configured Hibernate to use “c3p0” connection pool, but hits following warning : //… 2011-04-25_12:18:37.190 WARN o.h.c.ConnectionProviderFactory – c3p0 properties is specificed, but could not find org.hibernate.connection.C3P0ConnectionProvider from the classpath, these properties are going to be ignored. 2011-04-25_12:18:37.191 INFO o.h.c.DriverManagerConnectionProvider – Using Hibernate built-in connection pool (not for production use!) //… Look like “org.hibernate.connection.C3P0ConnectionProvider” is …

Read more

How to configure logging in Hibernate – Logback

In this tutorial, we show how to integrate Logback logging framework with Hibernate. Tools and Technologies used in this tutorial : Hibernate 3.6.3.Final slf4j-api-1.6.1 logback-core-0.9.28 logback-classic-0.9.28 Eclipse 3.6 Maven 3.0.3 1. Get SLF4j + Logback To use logback in Hibernate web application, you need 3 libraries : slf4j-api.jar logback-core logback-classic File : pom.xml <project …> …

Read more

Maven 3 + Hibernate 3.6 + Oracle 11g Example (Annotation)

This tutorial will reuse and modify the previous Hibernate3.6 XML mapping tutorial, but replace the Hibernate mapping file (hbm) with Hibernate / JPA Annotation code. Technologies in this article : Maven 3.0.3 JDK 1.6.0_13 Hibernate 3.6.3.final Oracle 11g 1. pom.xml No change in pom.xml file, all previous Hibernate3.6 XML mapping tutorial dependency can be reused. …

Read more

Hibernate – The type AnnotationConfiguration is deprecated

Problem Working with Hibernate 3.6, noticed the previous “org.hibernate.cfg.AnnotationConfiguration“, is marked as “deprecated“. Code snippets … import org.hibernate.cfg.AnnotationConfiguration; //… private static SessionFactory buildSessionFactory() { try { return new AnnotationConfiguration().configure().buildSessionFactory(); } catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } The code is still working, just keep displaying the deprecated …

Read more

Maven 3 + Hibernate 3.6 + Oracle 11g Example (XML Mapping)

In this article, we show you how to integrate Maven3, Hibernate3.6 and Oracle11g together. In the end of this article, you will create a Java project with Maven, and insert a record into Oracle database via Hibernate framework. Tools & technologies used in this article : Maven 3.0.3 JDK 1.6.0_13 Hibernate 3.6.3.final Oracle 11g 1. …

Read more