Advertisement
Guest User

Untitled

a guest
Jan 18th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. import java.util.Date;
  2.  
  3. import javax.persistence.Column;
  4. import javax.persistence.Entity;
  5. import javax.persistence.GeneratedValue;
  6. import javax.persistence.Id;
  7. import javax.persistence.Table;
  8. import javax.persistence.Temporal;
  9. import javax.persistence.TemporalType;
  10. @Entity
  11. @Table(name="EGRESOS")
  12. public class Egreso {
  13. @Id
  14. @GeneratedValue
  15. @Column(name="id")
  16. private Long id;
  17. @Temporal (TemporalType.DATE)
  18. @Column(name="fecha")
  19. private Date fecha;
  20. @Column(name="importe")
  21. private Double importe;
  22. @Column(name="detalle")
  23. private String detalle;
  24.  
  25. public Egreso() {
  26. // TODO Auto-generated constructor stub
  27. }
  28.  
  29. //all getters and setters
  30.  
  31. public abstract class CustomHibernateSupport<T> extends HibernateDaoSupport implements IGenericDAO<T> {
  32. private Class<T> persistentClass;
  33. @Autowired
  34. private SessionFactory sessionFactory;
  35.  
  36. @SuppressWarnings({ "unchecked", "deprecation" })
  37. public CustomHibernateSupport() {
  38. this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
  39. .getGenericSuperclass()).getActualTypeArguments()[0];
  40. this.sessionFactory = new Configuration().configure() // configures
  41. // settings from
  42. // hibernate.cfg.xml
  43. .buildSessionFactory();
  44. }
  45.  
  46. @PostConstruct
  47. public void init(){
  48. setSessionFactory(sessionFactory);
  49. getHibernateTemplate().setCacheQueries(true);
  50. }
  51.  
  52. public Class<T> getPersistentClass() {
  53. return persistentClass;
  54. }
  55.  
  56. public void setPersistentClass(Class<T> persistentClass) {
  57. this.persistentClass = persistentClass;
  58. }
  59.  
  60. public void insert(T entity) {
  61. getHibernateTemplate().save(entity);
  62. }
  63.  
  64. public void delete(T entity) {
  65. getHibernateTemplate().delete(entity);
  66. }
  67.  
  68. public void update(T entity){
  69. getHibernateTemplate().update(entity);
  70. }
  71.  
  72. @SuppressWarnings("unchecked")
  73. public List<T> findAll() {
  74. Session session = getSessionFactory().openSession();
  75. Criteria cr = session.createCriteria(getPersistentClass());
  76.  
  77. return cr.list();
  78. }
  79.  
  80. @SuppressWarnings("unchecked")
  81. public T findById(Long id) {
  82. Session session = getSessionFactory().openSession();
  83. Criteria criteria= session.createCriteria(getPersistentClass());
  84. criteria.add(Restrictions.eq("id",id));
  85. return (T) criteria.uniqueResult();
  86. }
  87.  
  88. @SuppressWarnings("unchecked")
  89. public List<T> findByDate(Date date) {
  90. System.out.println(date);
  91. Session session = getSessionFactory().openSession();
  92. Criteria criteria= session.createCriteria(getPersistentClass());
  93. criteria.add(Restrictions.eq("fecha",date));
  94.  
  95. return criteria.list();
  96. }
  97. }
  98.  
  99. @Transactional(readOnly=false)
  100. public boolean cargarGasto(GastosRequestDTO nuevo){
  101. Egreso e = new Egreso();
  102. try{
  103. e.setDetalle(nuevo.getDetalle());
  104. e.setFecha(nuevo.getFecha());
  105. e.setImporte(nuevo.getImporte());
  106. egreso.insert(e);
  107. log.info("se inserto correctamente el nuevo gasto ID: "+ e.getId());
  108. return true;
  109. }
  110. catch(Exception ex){
  111. log.error(Throwables.getStackTraceAsString(ex));
  112. log.error("No se pudo insertar gasto ");
  113. }
  114. return false;
  115. }
  116.  
  117. public void cargarGasto() {
  118. GastosRequestDTO nuevoGasto = new GastosRequestDTO();
  119. nuevoGasto.setFecha(fecha);
  120. nuevoGasto.setDetalle(descripcion);
  121. nuevoGasto.setImporte(importe);
  122. if (hotel.cargarGastos(nuevoGasto)) {
  123. obtenerBalance();
  124. setImporte(null);
  125. setDescripcion(null);
  126.  
  127. }
  128. }
  129.  
  130. <context:annotation-config/>
  131. <context:component-scan base-package="ar.com.as.hotel"/>
  132. <bean id="recordDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
  133. lazy-init="true" destroy-method="close">
  134.  
  135. <property name="driverClass" value="org.gjt.mm.mysql.Driver" />
  136.  
  137. <!-- CHANGE THE DATABASE CONNECTION-->
  138. <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/hotelTest"/>
  139. <property name="user" value="********" />
  140. <property name="password" value="********" />
  141. <!-- ***************************************** -->
  142.  
  143. <property name="minPoolSize" value="1" />
  144. <property name="maxPoolSize" value="100" />
  145. <property name="initialPoolSize" value="5" />
  146. </bean>
  147. <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  148. <property name="configLocation">
  149. <value>./WEB-INF/classes/hibernate.cfg.xml</value>
  150. </property>
  151. <property name="dataSource">
  152. <ref bean="recordDataSource" />
  153. </property>
  154. </bean>
  155. <bean id="transactionManager"
  156. class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  157. <property name="sessionFactory">
  158. <ref local="sessionFactory" />
  159. </property>
  160. </bean>
  161. <tx:annotation-driven transaction-manager="transactionManager" />
  162.  
  163. <?xml version="1.0" encoding="utf-8"?>
  164. <!DOCTYPE hibernate-configuration SYSTEM
  165. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  166. <hibernate-configuration>
  167. <session-factory>
  168. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  169. <property name="connection.url">jdbc:mysql://127.0.0.1:3306/hotel***</property>
  170. <property name="connection.username">******</property>
  171. <property name="connection.password">******</property>
  172. <property name="hbm2ddl.auto">update</property>
  173. <property name="connection.autocommit">false</property>
  174.  
  175. <!-- <mapping resource="Cliente.hbm.xml" /> -->
  176.  
  177. <mapping class="ar.com.as.hotel.modelo.Ingreso" ></mapping>
  178. <mapping class="ar.com.as.hotel.modelo.Precio"></mapping>
  179. <mapping class="ar.com.as.hotel.modelo.Habitacion"></mapping>
  180. <mapping class="ar.com.as.hotel.modelo.Temporada"></mapping>
  181. <mapping class="ar.com.as.hotel.modelo.Egreso"></mapping>
  182.  
  183.  
  184. <!-- List of XML mapping files -->
  185.  
  186. </session-factory>
  187. </hibernate-configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement