Think Beyond

Life's more than just software

Revisiting Hibernate with Spring on Mac OS X – Part I — September 10, 2012

Revisiting Hibernate with Spring on Mac OS X – Part I

I was getting bored and wanted to revisit Spring and Hibernate on my new found love machine – my Macbook Pro. The Mac OS gets a little getting used to, especially if you one is new to the way Mac works. Things are a little difficult to find – unlike the right click world. Rest assured, you can do much more, but as I said earlier – it gets a little getting used to ūüôā

Anyways, back to the problem. If I have to try something, I usually follow the steps:

a. Google about what I am doing (here I started by saying spring + hibernate + maven sample).

b. I found a few links and decided to Download Spring STS, Maven 3.0.4 and MySQL ( The JDBC connectors etc. could be outsourced to Maven).

I setup everything well. Keep in mind to install MySQL from the .dmg file. If you are like me, you might try to do it from the .tar.gz file initially, but trust me, it introduces a lot more troubles on the Mac OS. Just get it installed by using the .dmg file available by following instructions at: http://www.simonwhatley.co.uk/installing-mysql-on-mac-osx-10-6-snow-leopard.

Now, I also downloaded the MySQL Workbench for Mac OS at : http://dev.mysql.com/downloads/workbench/

I started working out a sample from : http://singgihpraditya.wordpress.com/2009/03/19/simple-maven2-spring-25-jpa-and-hibernate-integration/ but I ran into trouble when I ran the POM file РI wanted to see if the project would build properly. You can run the POM file without writing a single line of code and check if the required libraries will be downloaded properly.

It started failing by saying something like :

Downloading: ->
    http://repo1.maven.org/maven2/javax/transaction ->
      /jta/1.0.1B/jta-1.0.1B.jar
[WARNING] Unable to get resource from repository central ->
    (http://repo1.maven.org/maven2)
...
[INFO] Failed to resolve artifact.

required artifacts missing:
 javax.transaction:jta:jar:1.0.1B

Now, this essentially means that the Maven runtime is not able to find the required JAR file in the repository it is searching. The key factors are : which Maven and which repository.

On a Mac OS, the default Maven is usually /usr/bin/mvn. So, to change that in Spring STS, one has to ‘Add’ a ¬†Maven runtime by pointing to the correct Maven installation of your choice instead of using the embedded one.

Secondly, the default repository is ~/.m2/repo. To change that in Spring STS, go to Preferences -> Maven -> User settings and point to the correct user settings file (which is <Maven directory>/conf/settings.xml). and update user settings.

Make sure that you have a local repository like this:

<localRepository>/Users/trishulmukherjee/.m2/repository/</localRepository

>

Now, in order to install the JTA jars, remember to explicitly go to the Maven installation directory as configured above, download the JTA jar files from

http://java.sun.com/products/jta/

and run:

mvn install:install-file \
  -Dfile=./jta-1_0_1B-classes.zip \
  -DgroupId=javax.transaction \
  -DartifactId=jta -Dversion=1.0.1B \
  -Dpackaging=jar

and run the ./mvn <….> command since otherwise, the JAR file will get installed in the wrong repository and Maven runtime will again fail to detect the JTA jar files. Also, make sure that the path to the zip file is correct because Maven is dumb and it won’t even warn if the file is in a different location. It really pissed me off when I found out my mistake.

So thats it! Run the pom file and it should download all the required files without errors.

Advertisements
Ubuntu 12.10 networking fails after installing on one Oracle VM — June 22, 2012

Ubuntu 12.10 networking fails after installing on one Oracle VM

This is more of a note to self. I had a Ubuntu 12.10 running on an Oracle VirtualBox VM (4.16). Now, this was running to too slow and it said an update had been released for VirtualBox(4.18). I uninstalled the previous version of VirtualBox while keeping the Ubuntu VM disk intact.

Now, after installing VirtualBox 4.18, when I used the Ubuntu VDI, I could no longer access the internet. Something must have gone horribly wrong. Then, I came across the following (excerpt from http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1032790):

  1. In the Ubuntu guest, open a command prompt. For more information, see Opening a command or shell prompt (1003892).
  2. As root, open the file /etc/udev/rules.d/70-persistent-net.rules in a text editor:

    sudo vim /etc/udev/rules.d/70-persistent-net.rules

  3. The file consists of a line for each udev rule, associating MAC addresses with ethN interface names. For example:

    SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:fb:d5:1e", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
    SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:fb:d5:1c", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

  4. Identify the old and new interfaces by their MAC address in the ATTR{address}=="" field. For example:

    ATTR{address}=="00:0c:29:fb:d5:1c" ... NAME="eth0"  # old
    ATTR{address}=="00:0c:29:fb:d5:1e" ... NAME="eth1"  # new

  5. Copy the ethN name from the old interface to the new interface line. For example:

    ATTR{address}=="00:0c:29:fb:d5:1c" ... NAME="eth0"  # old
    ATTR{address}=="00:0c:29:fb:d5:1e" ... NAME="eth0"  # new

  6. Delete the line for the old ethN interface. For example:

    ATTR{address}=="00:0c:29:fb:d5:1e" ... NAME="eth0"  # new

  7. Save the udev configuration file.
  8. Reboot the Ubuntu virtual machine.
When the system is rebooted, the network configuration for the previous network interface is applied to the new interface.
Note: If you are unable to reboot the virtual machine, you can manually restart the networking service using this command:
service networking restart
If this command fails with an unknown instance error, run this command:
/etc/init.d/networking restart
It is proof that this works ’cause am putting up this post from within the VM ūüôā
Getting started with Hibernate 4 and Maven — February 12, 2012

Getting started with Hibernate 4 and Maven

For all those folks who are trying to find out how to use Hibernate 4 and Maven here goes:

Check out the attached source code. The main changes are in the pom.xml and in the HibernateUtil class. As you might already know, the earlier Configuration.buildSessionFactory is deprecated. So, one must use the  following to configure a SessionFactory object:

private static SessionFactory SESSIONFACTORY = null;
private static ServiceRegistry SERVICEREGISTRY = null;
try{
Configuration configuration = new Configuration();
configuration.configure(“hibernate.cfg.xml”);SERVICEREGISTRY = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
SESSIONFACTORY = configuration.buildSessionFactory(SERVICEREGISTRY);}catch (Exception e) {
System.err.println(“Initial SessionFactory creation failed.” + e);
throw new ExceptionInInitializerError(e);
}

Other than that, its pretty straight forward if you have the correct Maven installation and the pom.xml ūüôā

Here’s the code. Just download and build using maven. Its an Eclipse project – so you can Import As -> Existing Maven Project provided you have M2E plugin installed: (Use GitHub to access the code)

https://github.com/trishulpani/Hibernate4

SpringMVC-Hibernate-Maven-Glassfish-Netbeans sample project —

SpringMVC-Hibernate-Maven-Glassfish-Netbeans sample project

This post is a slightly modified version of http://viralpatel.net/blogs/2010/11/spring3-mvc-hibernate-maven-tutorial-eclipse-example.html

(For explanations, see the original blog) that uses the following tech stack:

Spring MVC (3.0.2.RELEASE), Hibernate (3.6.7.Final), JEE 6.0, JPA 2.0 (2.0-cr-1) and Spring (3.0.2.RELEASE).

Functionally both are same. This just works with the said tech stack as I have made the correct entries to work with Maven2.

The main modifications boiled down to the Project’s pom.xml and the fact that I had to add @Transactional to the service methods – otherwise I was getting runtime exception that said there was no active transaction.

Download the project and MySQL script files to get started. The application, once built (import to Netbeans and Build) – can be deployed to Glassfish.:

https://docs.google.com/open?id=0B6uhVH07AfMPZmZkYzA0MmItZjAzYS00MjliLTgxMzQtYmY3MTRiOGY1Njlh

MySQL script is here: https://docs.google.com/open?id=0B6uhVH07AfMPMGQ2MmQzYjYtOTMyNi00MmVjLWFkZDQtN2Q1MDdjNTJhMWFm

Getting started with Richfaces 4 and Maven — December 17, 2011

Getting started with Richfaces 4 and Maven

I tried to create a basic Richfaces project using the document : http://docs.jboss.org/richfaces/latest_4_X/Developer_Guide/en-US/pdf/Developer_Guide.pdf but ran into trouble with the following line:

mvn archetype:generate -DarchetypeGroupId=org.richfaces.archetypes -DarchetypeArtifactId=richfaces-archetype-simpleapp -DarchetypeVersion=4.0.0-SNAPSHOT -DgroupId=org.docs.richfaces -DartifactId=new_project.


As of Dec 2011, the issue is that my installation of Maven (ver. 3.0.3) would fail since it can’t find the required archetype. ¬†The latest archetype version being 4.1.0.Final, this ¬†works only when the ‘archetypeVersion’ in the above command line is changed to ‘4.1.0.Final’.

Hope this helps someone who’s just getting started with Richfaces and Maven.