jbjares2

fiesPU

Dec 5th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 KB | None | 0 0
  1. ====================================XML===============================================
  2.  
  3. <?xml version="1.0" encoding="UTF-8"?>
  4.  
  5. <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  6.              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7.              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
  8.             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
  9.              version="2.1">
  10.  
  11.     <persistence-unit name="fiesta_iot_rr_PU">
  12.         <description>Database handling all FIESTA-IoT stored SQL based information</description>
  13.         <provider>org.hibernate.ejb.HibernatePersistence</provider>
  14.         <!-- <jta-data-source></jta-data-source> -->
  15.  
  16.         <class>eu.fiestaiot.dataregistry.entity.AbstractEntity</class>
  17.         <class>eu.fiestaiot.dataregistry.entity.RegisteredTestbedEntity</class>
  18.  
  19.  
  20.         <properties>
  21.        
  22.             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
  23.             <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
  24.             <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/fiesta_iot_rr?zeroDateTimeBehavior=convertToNull"/>
  25.             <!--
  26.             <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/fiesta_iot_erm?autoReconnect=true&amp;zeroDateTimeBehavior=convertToNull"/>
  27.             -->
  28.             <property name="hibernate.connection.username" value="fiesta-iot"/>
  29.             <property name="hibernate.connection.password" value="F135t@pass"/>
  30.            
  31.             <property name="hibernate.show_sql" value="true" />
  32.  
  33.  
  34.             <!-- Use one to auto-generate the DB schema  -->
  35.             <!--
  36.             <property name="hibernate.hbm2ddl.auto" value="create-drop" />
  37.             <property name="hibernate.hbm2ddl.auto" value="update" />
  38.             <property name="hibernate.hbm2ddl.auto" value="validate" />
  39.             -->
  40.  
  41.              <property name="hibernate.hbm2ddl.auto" value="update" />
  42.  
  43.             <property name="hibernate.format_sql" value="true"/>
  44.            
  45.  
  46.            
  47.         </properties>
  48.     </persistence-unit>
  49.  
  50. </persistence>
  51.  
  52.  
  53.  
  54. ====================================XML===============================================
  55.  
  56.  
  57.  
  58. ===================================JAVA Annotation=====================================
  59. package eu.fiestaiot.dataregistry.dao;
  60.  
  61.  
  62. import eu.fiestaiot.dataregistry.entity.AbstractEntity;
  63. import org.springframework.stereotype.Repository;
  64. import org.springframework.transaction.annotation.Transactional;
  65.  
  66. import javax.annotation.PostConstruct;
  67. import javax.persistence.EntityManager;
  68. import javax.persistence.PersistenceContext;
  69. import java.lang.reflect.ParameterizedType;
  70. import java.util.List;
  71.  
  72. @Repository
  73. public abstract class AbstractJPAPersistenceDAO <K, E extends AbstractEntity> implements IPersistenceDAO<K, E> {
  74.  
  75.     protected Class<E> entityClass;
  76.  
  77.     @PersistenceContext
  78.     protected EntityManager em;
  79.  
  80.     @PostConstruct
  81.     public void init() {
  82.         ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
  83.         this.entityClass = (Class<E>) genericSuperclass.getActualTypeArguments()[1];
  84.     }
  85.  
  86.     @Override
  87.     public E save(final E entity) {
  88.         em.persist(entity);
  89.         return entity;
  90.     }
  91.    
  92.     public E update(final E entity) {
  93.         return em.merge(entity);
  94.     }
  95.  
  96.     public void remove(final E entity) {
  97.         em.remove(em.merge(entity));
  98.     }
  99.  
  100.     public E findById(final K id) {
  101.         return em.find(entityClass, id);
  102.     }
  103.  
  104.     public List<E> findAll() {
  105.         return em.createNamedQuery(AbstractEntity.FIND_ALL).getResultList();
  106.     }
  107.  
  108.     public Long getTotalResult() {
  109.         return (Long) em.createNamedQuery(AbstractEntity.TOTAL_RESULT).getSingleResult();
  110.     }
  111.  
  112.     public EntityManager getEm() {
  113.         return em;
  114.     }
  115.  
  116.     @PersistenceContext(unitName="fiesta_iot_rr_PU")
  117.     public void setEm(EntityManager em) {
  118.         this.em = em;
  119.     }
  120. }
  121.  
  122.  
  123. ===================================JAVA Annotation=====================================
Advertisement
Add Comment
Please, Sign In to add comment