Hibernate : Issues

Many vacancies have one Employer,

I tried to map it with something like below,

@ManyToOne
@Column(name="OWNER")
public Employer getOwner() {
	return owner;
}

@Column(s) not allowed on a @ManyToOne property

According to the above mentioned post, solution…

@ManyToOne
@JoinColumn(name="OWNER")
public Employer getOwner() {
	return owner;
}

Spring: MVC

Eclipse Project
MySpringMVC

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>MySpringMVC</display-name>
	<welcome-file-list>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

	<context:component-scan base-package="com.sam.test.controllers" />
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/views/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
</beans>

DogController.java

package com.sam.test.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DogController {

	@RequestMapping(value="test")
	public String test() {
		return "test";
	}	
}

test.jsp(nothing interesting, just a jsp)

<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
    <jsp:directive.page language="java"
        contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" />
    <jsp:text>
        <![CDATA[ <?xml version="1.0" encoding="ISO-8859-1" ?> ]]>
    </jsp:text>
    <jsp:text>
        <![CDATA[ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ]]>
    </jsp:text>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>TEST</title>
</head>
<body>
TEST PAGE
</body>
</html>
</jsp:root>

Copy all jars from following directories to /WEB-INF/lib
spring-framework-3.1.0.RELEASE\dist
and
spring-framework-3.1.0.RELEASE\projects\spring-build\lib\ivy

MySQL: Triggers

Simple Example

-- begin

DROP DATABASE IF EXISTS `test`;

CREATE DATABASE `test`;

USE `test`;

-- DROP TABLE IF EXISTS `test`;

CREATE TABLE `test` (
	`val` int
);

CREATE TABLE `test_log` (
	`val` int
);

DROP TRIGGER IF EXISTS `t1`;

DELIMITER //

CREATE TRIGGER `t1`
AFTER INSERT ON `test` FOR EACH ROW
BEGIN
	INSERT INTO `test_log` VALUES (NEW.val);
END //

DELIMITER ;

SET @x = 99;

SELECT * FROM `test`;
SELECT * FROM `test_log`;

INSERT INTO `test` VALUES (@x);

SELECT * FROM `test`;
SELECT * FROM `test_log`;

-- end

Another example

CREATE TABLE `test_updated` (
	`val` int,
	`val1` int
);

DELIMITER //

CREATE TRIGGER `t2`
BEFORE INSERT ON `test` FOR EACH ROW
BEGIN
	IF NEW.val <= 0 THEN
	INSERT INTO `test_updated` VALUES (NEW.val, -1);
	ELSE
	INSERT INTO `test_updated` VALUES (NEW.val, 1);
	END IF;
END //

DELIMITER ;

SELECT * from `test_updated`;

INSERT INTO `test` values(-9);
INSERT INTO `test` values(9);

SELECT * from `test_updated`;

References
Introduction to MySQL Triggers

MySQL: Stored Procedures


-- begin

DROP DATABASE IF EXISTS `test`;

CREATE DATABASE `test`;

USE `test`;

-- DROP TABLE IF EXISTS `test`;

CREATE TABLE `test` (
	`val` int
);

DROP PROCEDURE IF EXISTS `p1`;

DELIMITER //

CREATE PROCEDURE `p1` (IN v1 INT, OUT v3 INT)
BEGIN
	DECLARE v2 INT; -- declare it here, not inside else block
	IF v1 <= 0 THEN
	SELECT "no iterations";
	ELSE
		SET v2 = 0;
		WHILE v1 >= v2 DO
			INSERT INTO `test` values(v1);
			SET v1 = v1 - 1;
		END WHILE;
		SET v3 = V1;
	END IF;
END //

DELIMITER ;

SET @x = 99;
SET @y = @x;

SELECT COUNT(*) FROM `test`;

SELECT @x, @y;

CALL `p1`(@x, @y);

SELECT COUNT(*) FROM `test`;

SELECT @x, @y;

-- end

References
An Introduction to Stored Procedures in MySQL 5

Prep: SDLC

SDLC is the process of understanding how an information system (IS) can support business needs, designing the system, building it and delivering it to users.

The SDLC has four fundamental phases: Planning, Analysis, Design and Implementation.

Planning
The planning phase is the fundamental process of understanding why an information system should be built and determining how the project team will go about building it.

The first step is called project initiation during which the system’s business value to the organization is identified.
Ideas for a new system come in the form of a system request. the IS department works together with the person/department that presented the system request to conduct a feasibility analysis.

Feasibility Analysis

  • Technical feasibility (can we build it?)
  • Economic feasibility (will it provide business value?)
  • Organizational feasibility (if we build it, will it be used?)

System request and feasibility analysis are presented to an information systems approval committee (steering committee), which decides whether the project should be undertaken. If they approve the second step of project initiation occurs-project management. During project management project manager creates a work plan, staff the project and puts the techniques in place to help him control and direct the project through the entire SDLC. The deliverable for project management is project plan that describes how the project team will go about developing the system.

Analysis
In the phase the project team investigates any current system(s), identifies improvement opportunities, and develops a concept for the new system.

Analysis begins with the development of an analysis strategy that guides the project teams efforts. Such a strategy usually includes an analysis of the current system (as-is system) and it’s problems ans then an analysis of ways to design a new system (to-be system). The next step is information gathering (through interviews and questionnaires). The analysis of this information and input from project sponsors leads to development of a new system. The system concept is then used as a basis to develop a business process model that describes how the business will operate if the new system was developed. Finally, a data model is developed to describe the information that is needed to support the process.

The analysis, system concept, process model, and data model are combined in a document called the system proposal (SRS?), which is presented to the project sponsor and key members (members of the approval committee), they will decide whether the project should continue to move forward. System proposal is the initial deliverable that describes what the new system should look like.

Design
Te design phase describes how the system will operate in terms of the hardware, software and network infrastructure. The steps in the design phase determine exactly how the system will operate.

The first step in the design strategy, whether the system will be developed by the company’s own programmers, whther it will be outsourced to another firm or whether the company wil buy an existing software package. This leads to the development of the basic infrastructure design for the system that describes the hardware, software and network infrastructure that will be used.

Implementation
During this phase the system is actually built (or purchased). For most systems this is the longest and most expensive single part of the development process.

The first step in implementation is system construction, during which the system is built and tested to ensure it performs as designed. After that, Installation is the process by which the old system is turned off and the new one turned on. Installation approaches:

  • Direct cut-over approach (new system immediately replaces the old system)
  • Parallel conversion approach (both new and old systems are operated for a month or two untill it is clear that there are no bugs in the new system)
  • Phased conversion strategy (new system is installed in one part of the organization as an initial trial and then gradually installed in others)
  • One of the most important aspects of conversion is the development of a training plan to teach users how to use the new system and help manage the changes caused by the new system.

    Once the system has been installed, the analyst team establish a support plan for the system. This plan usually includes a formal or informal post implementation reviews as well as a systematic way for identifying major and minor changes needed for the system.

Prep : Functional and Non-Functional requirements

Functional and Non-Functional Requirements

Functional Requirements.
Functional requirement specifies what the system should do
Functional requirements specify specific behavior or functions, for example:
“Display the heart rate, blood pressure and temperature of a patient connected to the patient monitor.”

  • Administrative functions
  • Authentication
  • Audit Tracking
  • External Interfaces
  • Reporting Requirements

Non-Functional Requirements
Non-functional requirement specifies how the system should behave
Non-functional requirements specify all the remaining requirements not covered by the functional requirements. They specify criteria that judge the operation of a system, rather than specific behaviors, for example: “Display of the patient’s vital signs must respond to a change in the patient’s status within 2 seconds.”

  • Performance – Response Time, Throughput, Utilization, Static Volumetric
  • Availability
  • Recoverability
  • Maintainability
  • Security
  • Data Integrity
  • Usability

Original Source

Ant: Simple build file

build.xml

<?xml version="1.0"?>
<project name="ant test project" default="dist" basedir=".">

	<property name="src" location="src"/>
	<property name="build" location="build"/>
	<property name="dist"  location="dist"/>

	<target name="init">
		<tstamp/>
		<mkdir dir="${build}"/>
	</target>

	<target name="compile" depends="init" >
		<javac srcdir="${src}" destdir="${build}"/>
	</target>
	
	 <target name="dist" depends="compile" >
		<mkdir dir="${dist}/lib"/>
		<jar jarfile="${dist}/lib/${DSTAMP}-${TSTAMP}.jar" basedir="${build}"/>
	</target>
	
	<target name="clean" description="clean up" >
		<delete dir="${build}"/>
		<delete dir="${dist}"/>
	</target>
  
</project>

To run this

>ant -f build.xml
for specific task (eg. clean)
>ant -f build.xml clean

Java : OOP concepts applied

TextEntity.java

public interface TextEntity {
	public void print();
}

TextContainer.java

import java.util.List;

public abstract class TextContainer implements TextEntity {

	protected List<TextEntity> textEntities;
	protected String title;
	
	public void addEntity(TextEntity textEntity) {
		textEntities.add(textEntity);
	}

	public void print() {
		for(TextEntity textEntity : textEntities) {
			textEntity.print();
		}
	}
}

Book.java

import java.util.ArrayList;

public class Book extends TextContainer {

	public Book(String title) {
		this.title = title;
		this.textEntities = new ArrayList<TextEntity>();
	}
	
	public void print() {
		System.out.println("- Book : " + this.title);
		super.print();
	}

}

Chapter.java

import java.util.ArrayList;

public class Chapter extends TextContainer {
	
	public Chapter(String title) {
		this.title = title;
		this.textEntities = new ArrayList<TextEntity>();
	}
	
	public void print() {
		System.out.println("\tChapter : " + this.title);
		super.print();
	}
	
}

Section.java

import java.util.ArrayList;

public class Section extends TextContainer {

	public Section(String title) {
		this.title = title;
		this.textEntities = new ArrayList<TextEntity>();
	}

	public void print() {
		System.out.println("\t\tSection : " + this.title);
	}
	
}

Main.java

import java.util.List;
import java.util.ArrayList;

public class Main {
	public static void main(String[] args) {
		
		// Book 1 : Chapter 1
		TextContainer chapterA1 = new Chapter("1");		
		chapterA1.addEntity(new Section("A"));		
		chapterA1.addEntity(new Section("B"));
		// Book 1 : Chapter 2
		TextContainer chapterA2 = new Chapter("2");
		chapterA2.addEntity(new Section("A"));		
		chapterA2.addEntity(new Section("B"));		
		// Add chapters
		TextContainer bookA = new Book("ABC");
		bookA.addEntity(chapterA1);
		bookA.addEntity(chapterA2);
		
		// Book 2 : Chapter 1
		TextContainer chapterB1 = new Chapter("1");		
		chapterB1.addEntity(new Section("A"));		
		chapterB1.addEntity(new Section("B"));
		// Book 2 : Chapter 2
		TextContainer chapterB2 = new Chapter("1");		
		chapterB2.addEntity(new Section("A"));		
		chapterB2.addEntity(new Section("B"));
		// Add chapters
		TextContainer bookB = new Book("XYZ");
		bookB.addEntity(chapterB1);
		bookB.addEntity(chapterB2);

		List<TextContainer> bookList = new ArrayList<TextContainer>();		
		bookList.add(bookA);
		bookList.add(bookB);
		
		// Print
		for(TextContainer book : bookList) {
			book.print();
		}
	
	}
}	

run.bat

javac Main.java
java Main
pause
Tagged

Jetty with Eclipse

  1. Install jetty in Eclipse, http://download.eclipse.org/jetty/updates/jetty-wtp
  2. configure a server, new -> other -> server -> jetty
  3. java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor?
  4. Open server config, the add external jars under user entries, add asm from jetty/lib/annotation link

Useful Links

  1. http://pipecuts.wordpress.com/2011/01/30/classnotfoundexception-org-objectweb-asm-classvisitor/
  2. http://wiki.eclipse.org/Jetty/Howto/Configure_JSP

Maven : Creating simple project

  1. create a folder for the test project (D:\sandbox\junk)
  2. exceute the following from that folder
    • mvn archetype:create -DgroupId=sam.test -DartifactId=mvntest
    • Let it download some jar files
    • [INFO] Parameter: groupId, Value: sam.test
      [INFO] Parameter: packageName, Value: sam.test
      [INFO] Parameter: package, Value: sam.test
      [INFO] Parameter: artifactId, Value: mvntest
      [INFO] Parameter: basedir, Value: d:\SANDBOX\junk
      [INFO] Parameter: version, Value: 1.0-SNAPSHOT
      [INFO] project created from Old (1.x) Archetype in dir: d:\SANDBOX\junk\mvntest
      [INFO] ------------------------------------------------------------------------
      [INFO] BUILD SUCCESSFUL
      [INFO] ------------------------------------------------------------------------
      [INFO] Total time: 3 minutes 50 seconds
      [INFO] Finished at: Thu Mar 08 18:12:49 IST 2012
      [INFO] Final Memory: 12M/31M
      [INFO] ------------------------------------------------------------------------
      
    • pom.xml
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
      
        <groupId>sam.test</groupId>
        <artifactId>mvntest</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
      
        <name>mvntest</name>
        <url>http://maven.apache.org</url>
      
        <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
      
        <dependencies>
          <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
          </dependency>
        </dependencies>
      </project>
      
  3. How to perform an archetype:create from Eclipse
  4. Importing Maven 2 project to Eclipse