Hibernate, lesson2 – Queries

In the last post I have showed you a very basic example of usage in Hibernate. In this post I would like to show you all the different ways of executing a query while using Hibernate. The examples in this post use the same example from the last post. 1. Using the session functions The session object itself gives you methods to persist objects to the DB. For example: This code will fetch a row from the DB with primary key=111 and convert it to Contact object. Which is the equivalent to this SQL code: Another example: This code will insert a new row to the table. In other words this is the equivalent to this SQL code: Another example: This code will update a row in the table where the primary key is 111. In other words this is the equivalent to this SQL code: But as you probably noticed, the possibilities here are vary narrow. But there are other and better ways creating queries in Hibernate. 2. Using a Query object with HQL HQL is a query language designed by Hibernate It resembles SQL but has more options to it. Example: This will return a single result. That Continue reading Hibernate, lesson2 – Queries

Hibernate, lesson 1 – first encounter

Hibernate is one of the most popular java technologies there is and it seems it is not going anywhere for at least 10 years. If you are a java developer you can’t afford not to know it, at least on the surface. This post is for those of you who never used hibernate, or used it and just want to start from scratch. If hibernate seems like a tech-monster for you, you’ll come to the right place. I’ll show you how to get hibernate to work in a very short time. What do you need? DB connection Basic SQL knowledge Java knowledge + IDE Step 1 – Download Hibernate Download Hibernate Core¬† zip file form http://www.hibernate.org/6.html. Unzip it. The most impotent file is hibernate3.jar. Locate it under the root. Other important jars can be found under the lib folder Step 2 – Start a new java project with your IDE Add the hibernate3.jar to the classpath. Also add these jars: dom4j, log4j, slf4j-api,¬† slf4j-jcl, commons-logging, commons-collection, javassist, jta, antlr (most of them come with hibernate) Add also the driver jar + license to the classpath. Step 3 – Create a java bean A bean is a java class with a Continue reading Hibernate, lesson 1 – first encounter

Receive POP3 Emails using Java

Take a look at this example on how to read pop3 emails. But there are 3 parameters you need to prepare: your pop3 server, username and passowrd for that server. Pretty straight forward, not much to explain. All that is missing is the readContent() function which prints the message.getContent(). First you need to understand that an Email message content can be just a regular String or an input stream, but in most cases it is a Multipart object which can be composed of different parts of different types. It can also actually contain inner multiparts. Take a look at readContent() You will also need the mail.jar that can be found here. download source

Sending Email through GMail

In my last 2 posts I have shown you how to send SMTP email using Java. But I still haven’t shown how to send an Email through a secured SMTP server. To do so I’ll show you how to use GMail secured SMTP server to send Emails. Take a look at this class: The basic is the same, like sending a simple SMTP message. What’s different here is the creation of the Session object in the function getGmailSession(). The Session object is created with much more properties and with the Authenticator which holds the authentication properties. You will also need the mail.jar that can be found here. download source