hiberntate.cfg.xml
Código PHP:
<?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.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:xe</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<property name="current_session_context_class">thread</property>
<mapping resource="com/main/hibernate/pojo/Clientes.hbm.xml" />
</session-factory>
</hibernate-configuration>
applicationContext.xml
Código PHP:
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="hibernateSession" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
<bean id="clientesDAO" class="com.main.dao.ClientesDAOImp">
<property name="sessionFactory" ref="hibernateSession"/>
</bean>
</beans>
Ingresar.java
Código PHP:
package com.main.action;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.main.baseDatos.Operaciones;
import com.main.baseDatos.OperacionesImp;
import com.main.dao.ClientesDAO;
import com.main.dao.ClientesDAOImp;
import com.main.hibernate.pojo.Clientes;
import com.main.pojo.IngresoCliente;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class Ingresar implements ModelDriven<Object>,SessionAware{
private IngresoCliente cliente = new IngresoCliente();
int count=0;
private static final long serialVersionUID = 1L;
ApplicationContext appl= new ClassPathXmlApplicationContext("applicationContext.xml");
ClientesDAO clientesDAO= (ClientesDAO) appl.getBean("clientesDAO");
/**
* @param clientesDAO the clientesDAO to set
*/
public void setClientesDAO(ClientesDAOImp clientesDAO) {
this.clientesDAO = clientesDAO;
}
public void setCliente(IngresoCliente cliente) {
this.cliente = cliente;
}
public IngresoCliente getCliente() {
return cliente;
}
@SuppressWarnings("unchecked")
public String execute()
{
System.out.println("el valor es "+cliente.getClave());
System.out.println("cliente que quiero guardar "+cliente.getClave());
System.out.println("el valor null "+clientesDAO);
//valoron= operaciones.entrar(cliente.getClave());
//En una lista tipo clientes guardo los valores que me regresan de la busqueda
List<Clientes> valoron= (List<Clientes>) clientesDAO.buscaCliente(cliente);
Iterator milagro=valoron.iterator();
while(milagro.hasNext())
{
Clientes cliente=(Clientes) milagro.next();
count++;
}
if (count>0)
{
session.put("sesion", cliente);
return "success";
}
else
return "input";
}
@Override
public Object getModel() {
// TODO Auto-generated method stub
return cliente;
}
private Map<String,Object> session;
@Override
public void setSession(Map<String, Object> session) {
// TODO Auto-generated method stub
this.session=session;
}
}
ClientesDAOImp.java
Código PHP:
package com.main.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.main.hibernate.pojo.Clientes;
import com.main.pojo.IngresoCliente;
public class ClientesDAOImp implements ClientesDAO{
private SessionFactory sessionFactory;
/**
* @param sessionFactory the sessionFactory to set
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@SuppressWarnings("unchecked")
@Override
public List<Clientes> buscaCliente(IngresoCliente cliente)
{
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Query query=session.createSQLQuery("select * from Clientes where clave=:clavesuca").addEntity(Clientes.class);
List<Clientes> milagro= query.setInteger("clavesuca", cliente.getClave()).list();
session.beginTransaction().commit();
return milagro;
}
}
Código PHP:
ApplicationContext appl= new ClassPathXmlApplicationContext("applicationContext.xml");
ClientesDAO clientesDAO= (ClientesDAO) appl.getBean("clientesDAO");