Wednesday, November 6, 2013

A quick introduction to Hibernate using Maven

Create the project

  1. Run mvn archetype:generate
  2. Select default archetype (maven-archetype-quickstart)
  3. Select the latest version of the archetype (1.1 at the time of making this tuto)
  4. Enter the groupId (I’ll use com.mycompany).
  5. Enter the artifactId (I’ll use hibernate-basic).
  6. Leave the default version (1.0-SNAPSHOT for me).
  7. Leave the default package (same as groupId).
  8. Confirm properties.
  9. Run cd hibernate-basic.


You will end up with the following project structure:


Import into eclipse

  1. Go to File –> Import and select Existing Maven Projects:
    Note: do NOT run the command mvn eclipse:eclipse
  2. Select the root directory of your Maven project:
  3. Hit Finish.
  4. You’ll get a nice looking project: 

Add dependencies

  1. Open your pom file and add these two dependencies:
    <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-entitymanager</artifactId>
     <version>4.2.7.Final</version>
    </dependency>
    <dependency>
     <groupId>com.h2database</groupId>
     <artifactId>h2</artifactId>
     <version>1.2.145</version>
    </dependency>
    We’ll need the hibernate-entitymanager library to be able to use hibernate in our project (EntityManagerFactory, EntityManager, annotations, etc). It depends on other libraries such as hibernate-core and hibernate-commons-annotations, but Maven will automatically handle that for us.
    The h2 library will give us the ability to persist our entities to an in-memory database called H2.
  2. We need to create the folder src/main/resources/META-INF, since it will hold our JPA persistence descriptor:

Add persistence.xml

  1. Right click your META-INF folder and add a new XML file called persistence.xml:
  2. Copy the following code into persistence.xml:
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
     version="2.0">
    
     <persistence-unit name="myPersistenceUnit">
      <description>My persistence unit</description>
    
      <class>com.mycompany.Employee</class>
    
      <properties>
        <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
       <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE" />
       <property name="javax.persistence.jdbc.user" value="sa" />
       <property name="javax.persistence.jdbc.password" value="" />
    
       <property name="hibernate.show_sql" value="false" />
       <property name="hibernate.hbm2ddl.auto" value="create" />
      </properties>
    
     </persistence-unit>
    
    </persistence>
  3. You can give the persistence-unit any name you want, but take note of it. In our case it is myPersistenceUnit.
  4. Give it a description.
  5. The class section tells hibernate which class (or classes) we want to persist. Right now we still haven’t created this class, but that will be our next step.

Add an entity

  1. Add a class called Employee to the package com.mycompany
  2. Give the class 3 private properties, and generate their respective getters and setters:
    • id : long
    • name : String
    • age : int
  3. We have two ways of mapping our Employee class to the database.
    • The hibernate-specific way is using a mapping file for each persisted class, named <class-name>.hbm.xml, in our case it would be Employee.hbm.xml. There is also the posibility to use one single mapping file for all the persisted classes.
    • The JPA compliant way is using annotations inside each of our classes.
    We will go with the JPA compliant option.
  4. Add the following annotations to the class definition:
    @Entity
    @Table (name = "employees")
  5. Add the following annotations to the getId() method:
    @Id
    @GeneratedValue(generator="increment")
    @GenericGenerator(name="increment", strategy = "increment")
  6. For our annotations to work, we need to import the following libraries:
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    import org.hibernate.annotations.GenericGenerator;
    employee annotations
  7. Let’s add two constructors to our Employee class: a default one, required by hibernate, and one for us to use:
    public Employee(){
    }
    
    public Employee(String name, int age){
     this.name = name;
     this.age = age;
    }

Test

  1. Add the following code inside the main method of our App class:
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    em.persist(new Employee("John Doe", 24));
    em.persist(new Employee("Homer Simpson", 43));
    em.getTransaction().commit();
    
    em.getTransaction().begin();
      List<Employee> result = em.createQuery( "from Employee", Employee.class ).getResultList();
    for (Employee employee : result) {
     System.out.println( "Employee " + employee.getName() + " is " + employee.getAge() );
    }
    em.getTransaction().commit();
    em.close();
    Note: The method Persistence.createEntityManagerFactory() should receive the name of the persistence unit we specified in our persistence.xml.
  2. We need to import the following libraries:
    import java.util.List;
    
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;
  3. We have 3 ways or running our app:
    1. Right click App.java and select Run As –> Java Application:

    1. In a command prompt, go to the root of our project and run:
      mvn compile
      mvn exec:java -Dexec.mainClass="com.mycompany.App"
    2. In a command prompt, go to the root of our project and run:
      mvn dependency:copy-dependencies
      mvn compile
      java -cp target\classes;target\dependency\* com.mycompany.App
      Note: mvn dependency:copy-dependencies will copy all the required jars inside target\dependency.
    Note: If you want to get rid of all that output hibernate produces, turn of the logging level by importing java.util.logging.Level and adding this at the beggining of our main method:
    java.util.logging.Logger.getLogger("org.hibernate").setLevel(Level.OFF);

Tuesday, November 5, 2013

A quick introduction to Maven

What is Maven?

Not very long ago, when you had to compile your single-file project you only had to:
 gcc hello.c -o hello
 ./hello
That was easy, but when you had to compile a big project that included lots of libraries and lots of source files under different directories, it could become quite a headache.
So the all-shiny Make came into existence, to keep track of all those source files, under their respective directory and give you an easy way to recompile outdated source files and generate your program. It used a text file, commonly named makefile, to specify all your project’s stuff. Example makefile:
 all: hello  

 hello: main.o factorial.o hello.o
  g++ main.o factorial.o hello.o -o hello

 main.o: main.cpp
  g++ -c main.cpp

 factorial.o: factorial.cpp
  g++ -c factorial.cpp

 hello.o: hello.cpp
  g++ -c hello.cpp

 clean:
  rm -rf *o hello
you just had to run:
 make
 ./hello
and voilĂ , Make compiled all your sources and created your executable file. You could also run single tasks specifying them after the make command:
 make hello
 make clean
and Make would search for your desired task and execure all its steps. For example, with the previous makefile, if you run make clean Make would run rm -rf *o hello.
But it still had some problems. If you had a java project, Make would run a javac command for each .java file, which would make it quite slow for big projects. It would do:
 javac Class1.java
 javac Class2.java
 javac Class3.java
 ...
instead of the more resource-efficient:
 javac Class1.java Class2.java Class3.java
So Maven came into existence, which would solve problems like this and add some other nice features, like dependency management and project management. We’ll get into that later.

Setting up Maven on win 7

  • Download maven from here
  • Extract under C:\Program Files\Apache Software Foundation
  • Add the M2_HOME environment variable with the value C:\Program Files\Apache Software Foundation\apache-maven-3.1.1 (change the version number of apache-maven-x.x.x to your own).
  • Add the M2 environment variable with the value %M2_HOME%\bin.
  • Open a new command prompt and run mvn --version to verify your installation:
    C:\Users\user>mvn --version
    Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 10:22:22-0500)
    Maven home: C:\Program Files\Apache Software Foundation\apache-maven-3.1.1
    Java version: 1.6.0_35, vendor: Sun Microsystems Inc.
    Java home: C:\Program Files\Java\jdk1.6.0_35\jre
    Default locale: es_CO, platform encoding: Cp1252
    OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

Project Management

You can create your project structure using maven:
 mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This will create the following structure for your project:
 my-app
 |-- pom.xml
 `-- src
     |-- main
     |   `-- java
     |       `-- com
     |           `-- mycompany
     |               `-- app
     |                   `-- App.java
     `-- test
         `-- java
             `-- com
                 `-- mycompany
                     `-- app
                         `-- AppTest.java
and you can run it with:
 cd myapp
 mvn compile
 java -cp target\classes com.mycompany.app.App

Dependency Management

Maven uses the file pom.xml to keep your project configuration and other stuff. It also helps you manage dependencies. No more downloading xxxxx.jar files from many different sites and manually puting them in your project. you can specify a required library in your pom.xlm, by telling maven the groupId, artifactId and version:
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-entitymanager</artifactId>
   <version>4.2.7.Final</version>
  </dependency>
 </dependencies>
This tells maven to download the required jar files for Junit version 3.8.1 and Hibernate Annotations version 4.2.7.Final and include them in my projects.

Eclipse integration

  1. Go to File –> Import and select Existing Maven Projects:
    Note: You do not need to run the command mvn
     
  2. Select the root directory of your Maven project:
  3. Hit Finish.

Maven archetype:generate

An excelent explanation about the command mvn archetype:generate, found on http://stackoverflow.com/a/8205447/965342:
mvn archetype:generate command is used to create a project from an existing template. There are several archetype’s defined by many developers and project groups. When you run the command, maven does following things:
  1. Downloads maven-archetype-plugin’s latest version.
  2. Lists all archetype’s that can be used to create a project from. If you defined an archetype while calling the command, maven jumps to step 6.
  3. By default, maven chooses maven-archetype-quickstart archetype which basically creates a maven Hello World project with source and test classes. If you want to create a simple project, you can just press enter to continue. If you want to create a specific type of application, you should find the archetype matching your needs and enter the number of that archetype, then press enter. E.g. If you want to create a webapp project, you can enter 155 (this is the current number for this archetype, it can change in time.)
  4. Since archetypes are templates and they intend to reflect current best practices, they can evolve in time, thus they have their own versions. Maven will ask you which version of the archetype you want to use. By default, maven chooses latest version for you. so if you agree to use the latest version of an archetype, just press Enter at this step;
  5. Every maven project (and module) has its groupId, artifactId and version. Maven will then ask these to you in three steps.
  6. Finally, maven will ask you the package structure for your code. A best practice is to create your folder structure that reflects the groupId, thus Maven sets this as default but you are free to change this.
  7. After entering this information, Maven will show you all the information you entered and ask you to verify project creation. If you press Y and then enter, voilĂ  your project is created with the artifact and settings you chose.

Tuesday, March 12, 2013

Hotmail Outlook.com storage space

Are you considering migrating your corporate email from Google Apps to Hotmail / Outlook.com?
Take this in mind before you do so:

The nice folks at Microsoft are lying confusing the public, stating that they offer Virtually unlimited storage.
and they DON'T.

Hotmail / Outlook.com only offer 5 GB of email storage.

Here's the proof: 

and if they decide to remove the page or modify it, here's a screenshot (taken in march 12, 2013):


Have a look at this forum:

A user asks:
I'm in the process of migrating my email from gmail to Outlook.com. As of today, anyone sending an email to my outlook.com email address gets the email bounced back with the following error:

"Diagnostic-Code: smtp;552 5.2.2 This message is larger than the current system limit or the recipient's mailbox is full. Create a shorter message body or remove attachments and try sending it again."

I have around 10gb of email that I would like to retain - when signing up for outlook.com it seemed to suggest that there were no space limits. While 10gb of email is large, it is by no means astronomical and would be quite common.

What gives? Is there anyway for me to increase the limit? If not, Outlook.com should have really informed me that xGb is the practical limit instead of suggesting that it was limitless... If there are space restrictions there really should also be a way of checking how much space I'm using.

HELP!!
and a Microsoft representant answers:
Hi,
We're sorry for the inconvenience. Hotmail/Outlook's storage is 5gb. With the amount of file storage you have with Gmail, Outlook won't surely be able to accomodate such amount of files. If you try to migrate all your emails to Outlook, it would only store the amount of files allowed to fill in its total storage capacity. So it's possible that the sender is likely to get the bounce message since it would appear as the message is larger than your current system limit. 
You may contact Hotmail Plus through 1-800-002-587/1-800-000-021 9.00am to 9.00am Monday to Monday (AEST) for assistance.
Thanks! 
Proof (taken in march 12, 2013):


 Way to go Microsoft!

It seems that Microsoft deliberately confuses 5GB with "Virtually unlimited space".
I'm curios about how would they define my 8GB pendrive.


Setting up a hotmail account on android with your own domain

Whenever you want to add a hotmail account to your android device, to be able to sync your email/calendar/contacs in your phone, and you have your own domain, feel free to follow this post.

For the sake of this example, lets suppose we have the domain planetexpress.com and our user us bender, hence our email address is bender@planetexpress.com.

Add account, and select Microsoft Exchange ActiveSync:

Add your email and password. Hit Manual setup:

Add Domain\User name, Password. Leave the domain blank, just put \@
Set Exchange server to m.hotmail.com
Leave Use secure connection (SSL) checked
Hit Next:

Select your desired setup:
Peak schedule: how often does the application check for email in work hours (08:00 to 18:00).
Off-peak schedule: how often does the application check for email in work hours (18:00 to 08:00).
Period to sync email: up to how much time in the past will the application show your emails?.
Emails retrieval size: when viewing an email, this downloads the header plus the specified size.
Period to sync Calendar: up to how much time in the past will the application show your calendar?.
Hit Next:
Give the account a name. This does not have any effect other than helping you recognize this account among othe Microsoft Exchange ActiveSync accounts.
Finally, hit Done:

Unix Tips

A nice chunk of useful tips for unix:


I have marked with a * those which I think are absolutely essential
Items for each section are sorted by oldest to newest. Come back soon for more!

BASH
* In bash, 'ctrl-r' searches your command history as you type
- Input from the commandline as if it were a file by replacing
'command < file.in' with 'command <<< "some input text"'
- '^' is a sed-like operator to replace chars from last command
'ls docs; ^docs^web^' is equal to 'ls web'. The second argument can be empty.
* '!!:n' selects the nth argument of the last command, and '!$' the last arg
'ls file1 file2 file3; cat !!:1-2' shows all files and cats only 1 and 2
- More in-line substitutions: http://tiny.cc/ecv0cw http://tiny.cc/8zbltw
- 'nohup ./long_script &' to leave stuff in background even if you logout
- 'cd -' change to the previous directory you were working on
- 'ctrl-x ctrl-e' opens an editor to work with long or complex command lines
* Use traps for cleaning up bash scripts on exit http://tiny.cc/traps
* 'shopt -s cdspell' automatically fixes your 'cd folder' spelling mistakes
* Add 'set editing-mode vi' in your ~/.inputrc to use the vi keybindings
for bash and all readline-enabled applications (python, mysql, etc)

PSEUDO ALIASES FOR COMMONLY USED LONG COMMANDS
- function lt() { ls -ltrsa "$@" | tail; }
- function psgrep() { ps axuf | grep -v grep | grep "$@" -i --color=auto; }
- function fname() { find . -iname "*$@*"; }

VIM
- ':set spell' activates vim spellchecker. Use ']s' and '[s' to move between
mistakes, 'zg' adds to the dictionary, 'z=' suggests correctly spelled words
- check my .vimrc http://tiny.cc/qxzktw and here http://tiny.cc/kzzktw for more

TOOLS
* 'htop' instead of 'top'
- 'ranger' is a nice console file manager for vi fans
- Use 'apt-file' to see which package provides that file you're missing
- 'dict' is a commandline dictionary
- Learn to use 'find' and 'locate' to look for files
- Compile your own version of 'screen' from the git sources. Most versions
have a slow scrolling on a vertical split or even no vertical split at all
* 'trash-cli' sends files to the trash instead of deleting them forever.
Be very careful with 'rm' or maybe make a wrapper to avoid deleting '*' by
accident (e.g. you want to type 'rm tmp*' but type 'rm tmp *')
- 'file' gives information about a file, as image dimensions or text encoding
- 'sort | uniq' to check for duplicate lines
- 'echo start_backup.sh | at midnight' starts a command at the specified time
- Pipe any command over 'column -t' to nicely align the columns
* Google 'magic sysrq' and learn how to bring you machine back from the dead
- 'diff --side-by-side fileA.txt fileB.txt | pager' to see a nice diff
* 'j.py' http://tiny.cc/62qjow remembers your most used folders and is an
incredible substitute to browse directories by name instead of 'cd'
- 'dropbox_uploader.sh' http://tiny.cc/o2qjow is a fantastic solution to
upload by commandline via Dropbox's API if you can't use the official client
- learn to use 'pushd' to save time navigating folders (j.py is better though)
- if you liked the 'psgrep' alias, check 'pgrep' as it is far more powerful
* never run 'chmod o+x * -R', capitalize the X to avoid executable files. If
you want _only_ executable folders: 'find . -type d -exec chmod g+x {} \;'
- 'xargs' gets its input from a pipe and runs some command for each argument
* run jobs in parallel easily: 'ls *.png | parallel -j4 convert {} {.}.jpg'

NETWORKING
- Don't know where to start? SMB is usually better than NFS for most cases.
'sshfs_mount' is not really stable, any network failure will be troublesome
- 'python -m SimpleHTTPServer 8080' shares all the files in the current
folder over HTTP, port 8080
- 'ssh -R 12345:localhost:22 server.com "sleep 1000; exit"' forwards
server.com's port 12345 to your local ssh port, even if you machine
is not externally visible on the net.
Now you can 'ssh localhost -p 12345' from server.com and you will
log into your machine.
'sleep' avoids getting kicked out from server.com for inactivity
* Read on 'ssh-keygen' to avoid typing passwords every time you ssh
- 'socat TCP4-LISTEN:1234,fork TCP4:192.168.1.1:22' forwards your port
1234 to another machine's port 22. Very useful for quick NAT redirection.
- Some tools to monitor network connections and bandwith:
'lsof -i' monitors network connections in real time
'iftop' shows bandwith usage per *connection*
'nethogs' shows the bandwith usage per *process*
* Use this trick on .ssh/config to directly access 'host2' which is on a private
network, and must be accessed by ssh-ing into 'host1' first
Host host2
ProxyCommand ssh -T host1 'nc %h %p'
HostName host2
* Pipe a compressed file over ssh to avoid creating large temporary .tgz files
'tar cz folder/ | ssh server "tar xz"' or even better, use 'rsync'
* ssmtp can use a Gmail account as SMTP and send emails from the command line.
'echo "Hello, User!" | mail user@domain.com' ## Thanks to Adam Ziaja.
Configure your /etc/ssmtp/ssmtp.conf:
root=***E-MAIL***
mailhub=smtp.gmail.com:587
rewriteDomain=
hostname=smtp.gmail.com:587
UseSTARTTLS=YES
UseTLS=YES
AuthUser=***E-MAIL***
AuthPass=***PASSWORD***
AuthMethod=LOGIN
FromLineOverride=YES

-~-
(CC) by-nc, Carles Fenollosa <carles.fenollosa@bsc.es>
Retrieved from http://mmb.pcb.ub.es/~carlesfe/unix/tricks.txt
Last modified: lun 11 mar 2013 05:13:37 CET

Taken from http://mmb.pcb.ub.es/~carlesfe/unix/tricks.txt