Saturday, July 25, 2009

JDK and JRE version conflict: major.minor version 50.0

The error mentioned above in the post heading is a frequent arrival when different version of jdk and jre conflict. This error occurs when somebody compiles a program with higher version of javac and tries to run with lower version of jre or vice-versa.

For example:

#javac -version
javac 1.6_0_10

#java -version
java version "1.3.1_01"
java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_01)
Java HotSpot(TM) Client VM (build 1.3.1_01, mixed mode)

Solution - Temp

1. Download Jre1.6 or whatever version same as the javac version.
2. Manually set path to the new jre.
3. open a command window and type

#set path="C:\Program Files\Java\jre1.6_0_10\bin";

3. Now, for this session of the cmd window whenever you invoke java -- new jre will be invoked.

Sunday, July 19, 2009

Hibernate

Few hibernate tutorials:

1. https://www.hibernate.org (Hibernate core and annotations downloads)
2. https://www.hibernate.org/255.html (Hibernate Eclipse Plugin)
3.https://www.hibernate.org/hib_docs/tools/viewlets/createcfgxml_viewlet_swf.html (introductory video)
4. http://www.roseindia.net/hibernate/firstexample.shtml (First example)

A simple Hibernate implementation with a POJO object. (Eclipse)

Step1:
** Create a simple Java project in eclipse.
** Configure the build path and add hibernate.jar and mysql-connector-driver.jar in it.
**Further include all the required jar files. (see the hibernate distribution and all the jar files that are in the required folder.)
** Download sl4j from http://www.slf4j.org/download.html and include any one of(and only one) slf4j-nop.jar, slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar in the build path.
**** Be very careful not to mix different version of sl4j. Use the same version of sl4j-api.jar and any one of the above mentioned jar files. If mixed an error will occur that read:

java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory at org.slf4j.LoggerFactory.(LoggerFactory.java:60)

**
The Java build path libraries should look like:
5. Now create a JavaBean. This bean maps to a table called user which stores id, username and password information in a mysql database.

package yogidilip.tutorial;

public class HibernateBean {

private String username;
private String password;
private long id;

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}

6. Create a bean implementation class. This is the man hibernate implementation class. It uses the session factory to read the hibernate configuration file and opens the connection to the database.

package yogidilip.tutorial;


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


public class HibernateImpl {

public static void main(String[] args) throws MappingException {
Session session = null;

try{
//this step will read hibernate.cfg.xml
SessionFactory sessionfactory = new Configuration().configure().buildSessionFactory();
session = sessionfactory.openSession();

//Create new instance of user and set values in it by reading them from form object

System.out.println("Inserting Record");
HibernateBean user = new HibernateBean();
user.setId(7);
user.setUsername("Dilip");
user.setPassword("Rachana");
session.save(user);
/*session.beginTransaction().commit();*/
System.out.println("Done");
}
catch(Exception e){
System.out.println(e);
}
finally{
session.flush();
session.close();
}
}

}

7. Create a hibernate.cfg.xml file in the src directory. This file will contain all the database related connection information.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/User</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!--<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
--><mapping resource="user.hbm.xml"/>
</session-factory>
</hibernate-configuration>


8. The actual ORM mapping file user.hbm.xml should look like.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="yogidilip.tutorial.HibernateBean" table="USER">
<id name="id" type="long" column="ID" >
<generator class="assigned"/>
</id>

<property name="username">
<column name="USERNAME" />
</property>
<property name="password">
<column name="PASSWORD"/>
</property>
</class>
</hibernate-mapping>

9. The mysql syntax for creating the database and table user should look like.

CREATE DATABASE User;

CREATE TABLE user (
id INT NOT NULL AUTO_INCREMENT ,
username VARCHAR( 255 ) NOT NULL ,
password VARCHAR( 255 ) NOT NULL ,
PRIMARY KEY ( id )
) TYPE = INNODB;

10. To run the example the HibernateImpl.java file can be run as a regular Java program. If everything goes well you should be able to see a success message that looks like.

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Inserting Record
Done
Hibernate: insert into USER (USERNAME, PASSWORD, ID) values (?, ?, ?)

11. This post is a modified version of the tutorial http://www.roseindia.net/hibernate/firstexample.shtml at roseindia.net . All the credit goes to the author of the above tutorial.

Saturday, July 4, 2009

Weblogic Tutorial

Collection of some Weblogic Tutorials.

1. http://edocs.bea.com/wls/docs70/adminguide/startstop.html -- Starting and Stopping server.
2. http://egeneration.beasys.com/platform/docs81/confgwiz/index.html -- Configuring domains and templates.