Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.spring.model; import com.google.gson.Gson; import com.spring.entity.Sesion; import java.math.BigDecimal; import java.util.List; import org.hibernate.Hibernate; import org.hibernate.Query; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; /** * * @author carlosantonio */ @Repository("sesionService") @Transactional public class SesionModel extends AbstractModel<BigDecimal, Sesion> { public Sesion findById(BigDecimal id) { Sesion sesion = getByKey(id); if (sesion != null) { Hibernate.initialize(sesion.getTratamiento()); Hibernate.initialize(sesion.getTratamiento().getExpediente()); Hibernate.initialize(sesion.getTratamiento().getExpediente().getPaciente()); Hibernate.initialize(sesion.getTratamiento().getExpediente().getPaciente().getPersona()); } return sesion; } public void saveSesion(Sesion sesion) { persist(sesion); } public void deleteExpedienteById(BigDecimal codSesion) { Query query = getSession().createSQLQuery("delete from sesion where cod_sesion = :cod_sesion"); query.setBigDecimal("cod_sesion", codSesion); query.executeUpdate(); } @SuppressWarnings("unchecked") public String findAllSesionesById(BigDecimal cod_tratamiento) { String q = "select * from sesion where id_tratamiento = :cod_tratamiento"; Query query = getSession().createSQLQuery(q); query.setBigDecimal("cod_tratamiento", cod_tratamiento); List<Object> results = query.list(); String sesiones = new Gson().toJson(results); return sesiones; } }