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 java.util.List;
import de.ctdo.bunti.model.*; import de.ctdo.bunti.model.BuntiDMXDevice;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 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 @Override
public List<BuntiDMXDevice> getAllDMXDevices() { 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 @Override
public List<BuntiDevice> getAllDevices() { public List<BuntiDevice> getAllDevices() {
return getHibernateTemplate().loadAll(BuntiDevice.class); return em.createQuery("SELECT d FROM BuntiDevice d").getResultList();
} }
@Override @Override
public BuntiDevice getDeviceById(int deviceId) { public BuntiDevice getDeviceById(int deviceId) {
return getHibernateTemplate().get(BuntiDevice.class,deviceId); return em.find(BuntiDevice.class, deviceId);
} }
@Override @Override
public void addDevice(BuntiDevice device) { public void addDevice(BuntiDevice device) {
getHibernateTemplate().save(device); em.persist(device);
} }
@Override @Override
public void removeDevice(int deviceId) { 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 { public interface RoomsDAO {
List<Room> getRooms(); List<Room> getRooms();
Room getRoom(int id); Room getRoom(int id);
Room addRoom(Room room); void addRoom(Room room);
void removeRoom(int id); void removeRoom(int id);
} }

View File

@ -1,29 +1,38 @@
package de.ctdo.bunti.dao; package de.ctdo.bunti.dao;
import de.ctdo.bunti.model.Room; 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; 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 @Override
public List<Room> getRooms() { public List<Room> getRooms() {
return getHibernateTemplate().loadAll(Room.class); return em.createQuery("SELECT r FROM Room r").getResultList();
} }
@Override @Override
public Room getRoom(int id) { public Room getRoom(int id) {
return getHibernateTemplate().get(Room.class, id); return em.find(Room.class, id);
} }
@Override @Override
public Room addRoom(Room room) { public void addRoom(Room room) {
getHibernateTemplate().save(room); em.persist(room);
return room;
} }
@Override @Override
public void removeRoom(int id) { 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"?> <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" <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" 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 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"> 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:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:init_schema.sql" /> <jdbc:script location="classpath:init_schema.sql" />
<jdbc:script location="classpath:init_data.sql" /> <jdbc:script location="classpath:init_data.sql" />
</jdbc:embedded-database> </jdbc:embedded-database>
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="de.ctdo.bunti.model"/> <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" /> <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>
<bean id="roomsDAO" class="de.ctdo.bunti.dao.RoomsDAOImpl"> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="sessionFactory" ref="hibernateSessionFactory" /> <property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean> </bean>
<bean id="devicesDAO" class="de.ctdo.bunti.dao.BuntiDevicesDAOImpl">
<property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>
<tx:annotation-driven /> <tx:annotation-driven />
<context:component-scan base-package="de.ctdo.bunti.dao" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
</beans> </beans>

View File

@ -1,39 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" <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" 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:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:init_schema.sql" /> <jdbc:script location="classpath:init_schema.sql" />
<jdbc:script location="classpath:init_data.sql" /> <jdbc:script location="classpath:init_data.sql" />
</jdbc:embedded-database> </jdbc:embedded-database>
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="de.ctdo.bunti.model"/> <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" /> <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>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<bean id="roomsDAO" class="de.ctdo.bunti.dao.RoomsDAOImpl"> <property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="sessionFactory" ref="hibernateSessionFactory" /> <property name="jpaDialect">
</bean> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
<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> </bean>
<tx:annotation-driven /> <tx:annotation-driven />
<context:component-scan base-package="de.ctdo.bunti.dao" />
</beans> </beans>