How to convert .docx using Google doc

Wednesday, April 14, 2010 Posted by Gayath Chaminda 0 comments
You may use older version of MS-word (i.e. Word 2000/xp/2003). Once you got new version (.docx) of word document, you can not open it. In such a case you have to select alternative way of opening this document. You can select/download free pdf converter and convert .docx to .pdf version. But you can do it easily with Google Doc.

First open .docx document in Google Doc. (Upload the .docx document or open document with Google Doc). Then select File->Download As->PDF.
Google Doc converts .docx to pdf document and download it to your computer.

Alternatively you can select File->Print. Then document will open in the web page as a pdf document. (I haven't check with printer installed PC)
Labels:

How to avoid Apache starting issue in xampp?

Wednesday, April 7, 2010 Posted by Gayath Chaminda 1 comments
I installed xampp for my new project and installation was fine. But when i stared testing that everything is correctly working, I found that Apache does not start even though MySQL starts properly. Initially I thought that it was due to some issue with installation. First I reinstalled it and checked. Unfortunately I got same issue. Then I checked the windows system log files in event viewer. The log file content was

The Apache2.2 service terminated with service-specific error 1 (0x1).

Suddenly I remembered that I needs to check the Apache log file too. The log file said that it encountered some issue with port (80, default). Then I realized that Apache default port conflict with some application running in the same port. Finally I found that it was Skype . I made change the settings in the skype and problem resolved. The setting that I changed in skype was;

Tools -> Option -> Advanced -> Connection -> Uncheck "Use port 80 and 443 as alternatives for incoming connections"



Labels:

What is cascade in Hibernate?

Friday, February 26, 2010 Posted by Gayath Chaminda 0 comments
Cascade specifies which operations should be casecaded from the parent object to the associated object. These values can be applied for association specified in the hbm files.

The meaningfull values are;

1) cascade="none" the default, tells Hibernate to ignore the association.

2) cascade="save-update" cascades save and update actions from the parent to the child. In other words it tells Hibernate to navigate the association when the transaction is committed and when an object is passed to save() or update() and save newly instantiated transient instances and persist changes to detached instances.

3) cascade="delete" cascades the delete action from the parent to the child or tells Hibernate to navigate the association and delete persistent instances when an object is passed to delete().

4) cascade="all" means to all actions are cascaded from the parent to the child. Cascade both save-update and delete, as well as calls to evict and lock.

5) cascade="all-delete-orphan" means the same as cascade="all" but, in addition, Hibernate deletes any persistent entity instance that has been removed (dereferenced) from the association (for example, from a collection). All actions are cascaded from the parent to the child, orphan children are deleted.

6) cascade="delete-orphan" Hibernate will delete any persistent entity instance that has been removed (dereferenced) from the association (for example, from a collection).

Leave your comment here ...
Labels:

Beginner guide to Java Language

Wednesday, February 3, 2010 Posted by Gayath Chaminda 0 comments
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture,Java philosophy of "Write Once, Run Everywhere" make Java become one of the popular programming languages today. Becasue of this reason, java application runs in various environments, PC, Smart Cards, Games, Printers, ...etc.

If you heard about Java, you may confused with word JDK, Java2, J2SE, J2EE, Java Applet, Java Runtime Environment, ...etc. So, better to first clarify those. Actually Java is a programming platform specification published by Sun Microsystems which can be implemented by any other pary and current platform specification is Java2. Java Standard Edition (Java SE) is core java implementation that provides "real" or "basic" java language support. If you are new to java programming language on PC, this is the first point you should start. Java Development Kit (JDK) is implementation of java2 platform specification by Sun Microsystem, other vendors have implemented their versions of JDk but Sun's JDK is totoally free and most populer. The current version of JDk is JDk6 update 16 (At the time of writing this article). JDK can be considered as tool that can be used for implement java programming language or java compiler. In addition to Java SE, Sun resealses another two version for different application domains, Java Enterprise Edition (Java EE) and Java Micro Edition (Java ME). Enterprise Edition is can be considered as addition to the Java SE for Enterprise Application Development and Micro Edition is to develop application for mobile devices such as Celliuler Phones, PDAs, ...etc. Java Euntime Envieonment is collection of libraries that available in different platforms in order to run java applications. JRE knows how to convert java class file content (byte codes) in to platform specific instructions. This is how java achieved "Write Once, Run Anywhere (WORA)" paradigm. With Java SE you can develope two kinds of java programs, Java Application and Java Applets. These two approaches full fills different programming requirments.

I hope that now you have some sort of clear idea about java language. So, lets taste java programming language with some example. Hence from here on wards, I uses sun's JDk as development tool (You can download latest version of JDK from sun's web site [http://java.sun.com] and install in to you PC). As java is compile language, source code files are included in file with .java extension and compile using java compiler tool and finally generates file with .class extention. .class file can be used to run the java program and get the result. So, JDk provides several tools to manipulate java programs. Following are useful tools;

• Compiler name javac
• Launcher or interpreter to run Java applications, called java
• Java debugger called jdb
• Viewers applet called appletviewer
• Disassembler for file.class named javap
• Generators for API documentation is called javadoc
• Manage JAR files (Java Archive) named jar

The steps making Java Programs

1. Making the program text (source-code) with the Java language syntax. You can type in source-code in any text editor and save it with ".java" extension.
2. To compile the text into a Java application program.
3. Running Java applications with compiler or the applet, the applet viewer if the shape

You have to follow above steps though it applet or application. But following is java application implementation as an example. Select your populer text editor(Notepad, vi editor, Notepad++, ..etc) to write following lines and make sure to save file as Hello.java . Since java is a case sensetive language you have to pay attention to case sensitivity too. Note that you don't have to type number with dot in front of each line, this is just for explanation purpose.


public class Hello {
public static void main (String [] args) {
// Display message
System.out.printIn ( "Hello, world!");
}
}

Description:

1. The first line: make a class named "Hello". The entire framework in Java is always in the class
2. The name of the class must always be the same as the file name
a. In the first line, is the Hello class name and file name are Hello.java
b. Not allowed to use a different name, eg file name replaced with Halo.java. This will cause an error during compilation

3. On the second line written method main (). This method has an array of String parameters. This is a String array argument of Java programs.
a. A Java application program must have the main part, a method called main ()
b. Method main () function as a starting point of the program. That is, when the Java program is run, method main () always run the first time
4. In the third row, there are comments
a. Comments will not be compiled and run
b. Compiler will ignore the comment lines
c. Comment line begins with a slash character uses two "//". (comments by early signs "/ /" should only be on one line)

5. The fourth line is the command to display the message
a. The message of the string "Hello, world!" Displayed by the method called printIn ()
b. PrintIn method () is part of the variable named out
c. Meanwhile, out is part of the class named System
d. To call the dot operator (".") is used to System.out.printIn ()

The process for compiling Java programs are:

1. Once finished creating the Java file, the next step is to compile a file using javac.exe (Being in the bin directory, If you install JDk6 at C:\Program Files\Java\jre1.6.0_16 then javac.exe (also other supportive tools) resides in C:\Program Files\Java\jre1.6.0_07\bin directory. Hence if you issue javac command with java file name you have to copy your source file in to bin directory or inlcude bin directory in to your system path (Temporaly you can set path in windows environment using, set path=%path%;).

2. Running the compiler command in the command-prompt

> Javac Hello.java

3. Hello.class which generated files are output files from the compilation results

Hello.class files can be directly run the program using java.exe.

> java Hello

Hello World

Finally the result obtained, first java program ran. Now you can explore more on java language. If you encountered any difficulties please comment.
Labels:
Thursday, January 7, 2010 Posted by Gayath Chaminda 0 comments

Problem Description

When designing code that will access that you stored in relational tables, the code will need to create queries. Some of the queries are constructed dynamically, and other queries are static. These static queries can take parameters but the query stays the same. As your application gets larger, the code can have a lot of queries and this can get difficult to manage.

Solution

Many of persistace frameworks including Hibernate, iBatis, JPA, ...etc allow the creation of named queries for these predefined queries. You can use a named query whenever you have a query that you run several times and just set parameters for. These are often called static queries. You can do this for all your pre-defined queries.

Using the Named queries is a good practice for the following reasons:

  • An advantage of named queries is that the Persistence provider might be able to precompile them

  • Pre-compiling of queries is useful because it can help to find bugs before deployment

  • Using named queries helps separate the query string from the code using it, so can help clean up your code. Otherwise, you may end up retyping the query in different places that use it in the code. Code not using named queries often puts the query strings into a String constants for reuse, but these are not pre-compiled like named queries are.

Named queries are thread-safe as they are meant to be reused by many classes and instances. Note that the names of named queries are scoped and it is important to avoid name clashes. When annotations are used to define named queries, the scope of the name is visible within a persistent unit. You can use the descriptor or hbm file to make the names visible at application scope. When using a named query in code, we use a naming convention of prefixing the class name of the class where the named queries are defined in front of the query name.

Named queries can only be defined on certain classes. They must be defined and placed only on entities and mapped superclasses.

Note that the named queries can be used for native SQL queries as well as for queries expressed in the Java Persistence query language.

Practical Aspect of using Named queries

Suppose need arises to use these notations/operators along with Hibernate
named query in hbm
XML file. In that case one has to use CDATA along with
the named query in HBM file.


Using an example you might have seen already in one of the examples on
Hibernate:


Employee and Department sharing many to one type of association mapping
with one another.


This example uses following software environment:

1. JDK 5.0
2. Eclipse 3.4.x
3. Hibernate 3.2
4. MyQL 5.0

Java POJO for this example is Employee.java


Employee.java
---------------------------------------------------------


package test.hibernate.namedqueries;

import java.io.Serializable;

public class Address extends Contact implements Serializable {

private long id;
private String employeeName;
private String addrLine1;
private String addrLine2;
private String addrLine3;

public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this. employeeName = employeeName;
}
public String getAddrLine1() {
return addrLine1;
}
public void setAddrLine1(String addrLine1) {
this.addrLine1 = addrLine1;
}
public String getAddrLine2() {
return addrLine2;
}
public void setAddrLine2(String addrLine2) {
this.addrLine2 = addrLine2;
}
public String getAddrLine3() {
return addrLine3;
}
public void setAddrLine3(String addrLine3) {
this.addrLine3 = addrLine3;
}
}


---------------------------------------------------------

Hibernate configuration file for creating Hibernate SessionFactory is as follows:

hibernate.cfg.xml
---------------------------------------------------------

---------------------------------------------------------
The named query is defined in the Employee.hbm.xml file:

Employee.hbm.xml
---------------------------------------------------------

< hibernate-mapping package="demo.profile" >
< class name="Address" table="Address" >
< id name="id" column="employee_id" >
< property name="employeeName" column="Emp_Name" >
< property name="addrLine1" column="address_Line_1" >
< /property >

---------------------------------------------------------
So, next step is to manipulate the date through some other class.
It is very simple and straight forward. It is retrieve named
query from the hbm file (Employee.hbm.xml), and then sets
appropriate parameter value to the query. Final result is
employee details of the employee record fetched from database.

EmployeeTest.java
---------------------------------------------------------

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class EmployeeTest {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public EmployeeTest() {
Session session = sessionFactory.openSession();
session.beginTransaction();
List listEmployee = session.getNamedQuery("findEmployee").
setParameter(0, 123).list();

System.out.println(listEmployee.size());
}
/**
* @param args
*/
public static void main(String[] args) {
new EmployeeTest();
}

}

Labels: