changed dao dependencies from hibernate to JPA api.

This commit is contained in:
Lucas Pleß 2012-03-25 19:47:16 +02:00
parent 8f2610314e
commit b224e1b0ba
5 changed files with 96 additions and 66 deletions

View File

@ -2,35 +2,46 @@ package de.ctdo.bunti.dao;
import java.util.List;
import de.ctdo.bunti.model.*;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import de.ctdo.bunti.model.BuntiDMXDevice;
import de.ctdo.bunti.model.BuntiDevice;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
public final class BuntiDevicesDAOImpl extends HibernateDaoSupport implements BuntiDevicesDAO {
@Repository
public final class BuntiDevicesDAOImpl implements BuntiDevicesDAO {
private EntityManager em;
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.em = entityManager;
}
@Override
public List<BuntiDMXDevice> getAllDMXDevices() {
return getHibernateTemplate().loadAll(BuntiDMXDevice.class);
//TODO: hier noch nur die DMX Geräte suchen!
return em.createQuery("SELECT d FROM BuntiDevice d").getResultList();
}
@Override
public List<BuntiDevice> getAllDevices() {
return getHibernateTemplate().loadAll(BuntiDevice.class);
return em.createQuery("SELECT d FROM BuntiDevice d").getResultList();
}
@Override
public BuntiDevice getDeviceById(int deviceId) {
return getHibernateTemplate().get(BuntiDevice.class,deviceId);
return em.find(BuntiDevice.class, deviceId);
}
@Override
public void addDevice(BuntiDevice device) {
getHibernateTemplate().save(device);
em.persist(device);
}
@Override
public void removeDevice(int deviceId) {
getHibernateTemplate().delete(getDeviceById(deviceId));
em.remove(getDeviceById(deviceId));
}
}

View File

@ -7,6 +7,6 @@ import java.util.List;
public interface RoomsDAO {
List<Room> getRooms();
Room getRoom(int id);
Room addRoom(Room room);
void addRoom(Room room);
void removeRoom(int id);
}

View File

@ -1,29 +1,38 @@
package de.ctdo.bunti.dao;
import de.ctdo.bunti.model.Room;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
public final class RoomsDAOImpl extends HibernateDaoSupport implements RoomsDAO {
@Repository
public final class RoomsDAOImpl implements RoomsDAO {
private EntityManager em;
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.em = entityManager;
}
@Override
public List<Room> getRooms() {
return getHibernateTemplate().loadAll(Room.class);
return em.createQuery("SELECT r FROM Room r").getResultList();
}
@Override
public Room getRoom(int id) {
return getHibernateTemplate().get(Room.class, id);
return em.find(Room.class, id);
}
@Override
public Room addRoom(Room room) {
getHibernateTemplate().save(room);
return room;
public void addRoom(Room room) {
em.persist(room);
}
@Override
public void removeRoom(int id) {
getHibernateTemplate().delete(getRoom(id));
em.remove(getRoom(id));
}
}

View File

@ -1,43 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
xmlns:jdbc="http://www.springframework.org/schema/jdbc">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:init_schema.sql" />
<jdbc:script location="classpath:init_data.sql" />
</jdbc:embedded-database>
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan" value="de.ctdo.bunti.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!--<prop key="hibernate.hbm2ddl.auto">create</prop>-->
</props>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="roomsDAO" class="de.ctdo.bunti.dao.RoomsDAOImpl">
<property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>
<bean id="devicesDAO" class="de.ctdo.bunti.dao.BuntiDevicesDAOImpl">
<property name="sessionFactory" ref="hibernateSessionFactory" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="de.ctdo.bunti.model" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
<!-- <property name="database" value="DERBY" />-->
<property name="database" value="H2" />
</bean>
</property>
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernateSessionFactory" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<tx:annotation-driven />
<context:component-scan base-package="de.ctdo.bunti.dao" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
</beans>

View File

@ -1,39 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:init_schema.sql" />
<jdbc:script location="classpath:init_data.sql" />
</jdbc:embedded-database>
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan" value="de.ctdo.bunti.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!--<prop key="hibernate.hbm2ddl.auto">create</prop>-->
</props>
</property>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="de.ctdo.bunti.model" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
<!-- <property name="database" value="DERBY" />-->
<property name="database" value="H2" />
</bean>
</property>
</bean>
<bean id="roomsDAO" class="de.ctdo.bunti.dao.RoomsDAOImpl">
<property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>
<bean id="devicesDAO" class="de.ctdo.bunti.dao.BuntiDevicesDAOImpl">
<property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernateSessionFactory" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<tx:annotation-driven />
<context:component-scan base-package="de.ctdo.bunti.dao" />
</beans>