Oracle 11g user password expired locked

To find the details of the users in your DB, execute the following statement:

select username, account_status, expiry_date, lock_date from dba_users;

To unlock the account, execute the following:

alter user se account unlock;

To change the password, execute following:

alter user se identified by sepm;

NOTE:  Old and new password can be same.

101: Spring + Hibernate Entity Manager

Here is a short description on setting up a Spring with Hibernate Entitymanger (JPA) application.

I am asuming that you have a project structure ready

Step 1:

Create jpa configuration file:
repository-jpa.xml

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
         <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
		<property name="dataSource" ref="dataSource" />
		<property name="persistenceUnitName" value="osm" />
		<property name="jpaProperties">
			<props>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
			</props>
		</property>
	</bean>
<bean id="jpaVendorAdapter"
		class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
		<property name="showSql" value="false" />
		<property name="generateDdl" value="false" />
		<property name="databasePlatform" value="${database.platform}" />
	</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName" value="jdbc/nameDS" />
		<property name="resourceRef" value="true" />
	</bean>

NOTE: values like database.platform come from .properties i.e. a resource file
Example:

# DataSource Properties

database.platform=org.hibernate.dialect.Oracle10gDialect
database.driver=oracle.jdbc.driver.OracleDriver
database.url=jdbc:oracle:thin:@localhost:1521:xe
database.user=sa
database.password=sa
hibernate.hbm2ddl.auto=validate

This file is loaded as a resource:
configuration.xml

<bean id="gmPropertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>database.properties</value>
			</list>
		</property>
	</bean>

Step 2:
You can create DAO / JPA classes. Here is a sample class I am using:
It is always a good idea to write a generic inteface for such classes

public interface IGenericRepository<E> {
	void persist( E entity );

	E merge( E entity );

	void remove( E entity );

	E findById( Long id );

	void executeQuery( String strQuery );

	List <E> executeNativeQuery( String strQuery );
	
	EntityManager getEntityManager();

}

My concrete class:


@Transactional
@Repository
public class JpaGenericRepository<E> implements IGenericRepository <E> {

	private Class <E> entityClass;

	@PersistenceContext
	private EntityManager entityManager;

	@SuppressWarnings("unchecked")
	public JpaGenericRepository() {

		// Get "E" and assign it to this.entityClass
		ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
		java.lang.reflect.Type type = genericSuperclass.getActualTypeArguments()[0];
		if( type instanceof Class ) {
			this.entityClass = (Class <E>) type;
		} else if( type instanceof ParameterizedType ) {
			this.entityClass = (Class <E>) ((ParameterizedType) type).getRawType();
		}

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void persist( E entity ) {
		entityManager.persist(entity);
		int i =1/0;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public E merge( E entity ) {
		entity = entityManager.merge(entity);
		return entity;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void remove( E entity ) {
		entityManager.remove(entityManager.contains(entity) ? entity : entityManager.merge(entity));
	}

	@Override
	public E findById( Long id ) {
		return entityManager.find(entityClass, id);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW, timeout = 180)
	public void executeQuery( String strQuery ) {
		Query query = entityManager.createNativeQuery(strQuery, entityClass);
		query.executeUpdate();
	}

	@Override
	@Transactional
	public List <E> executeNativeQuery( String strQuery ) {
		Query query = entityManager.createNativeQuery(strQuery, entityClass);
		@SuppressWarnings("unchecked")
		List <E> rs = query.getResultList();
		return rs;
	}

	@Override
	public EntityManager getEntityManager() {
		return entityManager;
	}

}

Repository class as a service


public class JpaProductRepository extends JpaGenericRepository <Product> implements IProductRepository {

}

Now create an xml which has bean definations for the JpaProductRepository:
repository.xml


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"

	default-lazy-init="false" default-dependency-check="none">

		<bean id="productRepositry"
		class="com.gide.csolp.osm.service.repository.jpa.JpaProductRepository" />

</beans>

That is preety much it!

If you have are developing a web application, then your application should load these xml’s via web.xml.

If you are creating a stand alone application you can uses any of the classes to load the application context.

Example application context:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/lang 
		http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-2.5.xsd"

	default-lazy-init="false" default-dependency-check="none">
	<import resource="classpath:/META-INF/spring/repository.xml" />
	<import resource="classpath:/META-INF/springtest/configuration.xml" />
	<import resource="classpath:/META-INF/springtest/repository-jpa.xml" />
</beans>

Changing default oracle XE http port

Default port after installation in oracle is 8080; You can check and change it it by doing the following on the command prompt:

 
sqlplus system@xe 

Enter password:

Verbunden mit:
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production

SQL>select dbms_xdb.gethttpport as "httpport" from DUAL;  

  httpport
----------
      8080

Change the port:

 
SQL>begin
2 dbms_xdb.sethttpport('8085');
3 end;
4 /

SQL> select dbms_xdb.gethttpport as "httpport" from dual;
  httpport
----------
      8085
 

There you go! cheers!

Simple Java Objects

I have taken and given a lot of inteviews and worked with lot of developers. Here is one of the most common, simple but confusing question:

 

class A 
{ 
  public int addA()
  {
    //do something
  }
} 

class B extends A 
{ 
  public int addB()
  {
    //do something
  }
} 
 

1. A a = new B() is right?

or

2. B b = new A() right?

1 is right. B can be said to be specific implementation of A.

So an object of B will always have the characteristics of A but not vice versa.

 
 

Why use Transfer Objects

Enterprise applications with server side implementation of business logic normally expose some methods for accessing the business components. These methods are call 1…* times by clients to obtain the data that is required by them. These methods, which are normally exposed through remote methods will result in a network overhead.

  • Client applications/portlets normally require data from more than one domain objects or enterprise beans.
  • Client achieves this by making multiple calls to the business components.
  • A lot of chatter over the network degrades network and application performanceperformance.
  • Also the number of read cycles out number the write cycle in majority of applications.
  • On the other hand a enterprise bean may provide more data than needed to a client request.

The solution to this problem is to have a single method call to the server side implementation of the business compoents to extract the required data. This can be done by making a serialized Transfer Object from 1…* domain objects. Transfer Object is constructed on demand by the enterprise bean and returned to the remote client.  

When the client makes a single remote method invocation to the enterprise bean to request the Transfer Object, the enterprise bean then constructs a new Transfer Object instance, copies required values into the object and returns it to the client. The client receives the Transfer Object and uses accessor methods on the Transfer Object to get data from the Transfer Object.

The same transfer object can be used to transfer data back to the business layer in a single remote invocation. 

Example:

TRANSFER OBJECT

public class WorkTO implements java.io.Serializable {
  	public String workID; // can be private. wil have to provide getter
         // setter
  	public String days;
  	public String workdays;
  	public String payLevel; // Transfer Object constructors...
 }

ENTITY

 @Entity
 // for Hibernate Simple POJO
 public class Work {
  Long id;
  private String workID;
  private String days;

  	public WorkTO getWorkTO()
  	{
   		WorkTO to = new WorkTO();
   		to.workID=workID;
   		to.workdays = new WorkDays().getWorkDays();//some other Entity bean implementation
   		to.days=days;
   		to.payLevel = new PayLevel().getPayLevels();
   		return to;
  	}

 }

Reducing Code Dependancy between layers/classes

The concept of programing to an interface or to an abstration is well known and understood. But when it comes to actual implementation lot of mistakes are made. Here I will try to show an example of how “The Dependency Inversion Principle” as suggested by Robert C. Martin.

Basically it helps reduce 3 things i.e. RFI (Rigidity, Fragility, and Immobility) of your code.

Robert C. Martin states that the three defining factors of “bad code” are:

  1. It is hard to change because every change affects too many other parts of the system (Rigidity)
  2. When you make a change, unexpected parts of the system break (Fragility)
  3. It is hard to reuse in another application because it cannot be disentangled from the current application (Immobility)

How do you fix these problems:

1. Any high level or low level component should be dependent on abstraction and not concrete implementations.

2. Abstractions should not depend upon details of your implementation. Interface or an abstraction should cater to the common functionality of your business logic and the lower level modules.

Example:

package example;

public class Account{

    SavingsAccount savingsAccount= new SavingsAccount();

    public void newAccount(Customer customer) {
        SavingsAccount.create(customer);
    }

    class SavingsAccount{
        public void create(Customer customer) {
            //TODO: your implementation 

        }
    }

}

In this example, the Account class is directly dependent on SavingsAccount, which has no abstractions and is the implementing class.

A better way of writing the same code would be:

1. Create an interface Account.

package example;

public interface IAccount{

    public void create(Customer customer) ;

}

2. Implement IAccount:

package example;

public class SavingsAccount implements IAccount{.
public void create(Customer customer) {
            //TODO: your implementation 

        }   
}

3. Implement Account:

package example;

public class Account{

    IAccount account= null;

    public void setAccountType(IAccount account) {
        this.account= account;
    }

    public void newAccount(Customer customer) {
        account.create(customer);
    }

}

As we can see, we have decoupled the invocation from the actual implementation that is your business logic.  Any changes in your acount type class wil not affect the implementation whioch could be a third party.so you are now not breaking the high level code. According to the rules of DIP, this is all we need in order to get rid of RFI in the code.