From b224e1b0ba3e1377ee135a7c0064c0caa4493f36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Sun, 25 Mar 2012 19:47:16 +0200 Subject: [PATCH] changed dao dependencies from hibernate to JPA api. --- .../ctdo/bunti/dao/BuntiDevicesDAOImpl.java | 27 ++++++--- src/main/java/de/ctdo/bunti/dao/RoomsDAO.java | 2 +- .../java/de/ctdo/bunti/dao/RoomsDAOImpl.java | 25 +++++--- .../META-INF/spring/hibernate-beans.xml | 58 ++++++++++--------- .../de/ctdo/bunti/hibernate-test-context.xml | 50 +++++++++------- 5 files changed, 96 insertions(+), 66 deletions(-) diff --git a/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java b/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java index 0510847..bef44fe 100644 --- a/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java +++ b/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java @@ -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 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 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)); } } diff --git a/src/main/java/de/ctdo/bunti/dao/RoomsDAO.java b/src/main/java/de/ctdo/bunti/dao/RoomsDAO.java index ecb686d..e84754a 100644 --- a/src/main/java/de/ctdo/bunti/dao/RoomsDAO.java +++ b/src/main/java/de/ctdo/bunti/dao/RoomsDAO.java @@ -7,6 +7,6 @@ import java.util.List; public interface RoomsDAO { List getRooms(); Room getRoom(int id); - Room addRoom(Room room); + void addRoom(Room room); void removeRoom(int id); } diff --git a/src/main/java/de/ctdo/bunti/dao/RoomsDAOImpl.java b/src/main/java/de/ctdo/bunti/dao/RoomsDAOImpl.java index d82e74b..15415d0 100644 --- a/src/main/java/de/ctdo/bunti/dao/RoomsDAOImpl.java +++ b/src/main/java/de/ctdo/bunti/dao/RoomsDAOImpl.java @@ -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 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)); } } diff --git a/src/main/resources/META-INF/spring/hibernate-beans.xml b/src/main/resources/META-INF/spring/hibernate-beans.xml index ccc9010..b8f1cad 100755 --- a/src/main/resources/META-INF/spring/hibernate-beans.xml +++ b/src/main/resources/META-INF/spring/hibernate-beans.xml @@ -1,43 +1,47 @@ + 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"> - - - - - org.hibernate.dialect.HSQLDialect - true - - - - - - - - - - - - + + + + + + + + + + + - - - - + + + + + - + + + diff --git a/src/test/resources/de/ctdo/bunti/hibernate-test-context.xml b/src/test/resources/de/ctdo/bunti/hibernate-test-context.xml index 3def185..40fcd05 100644 --- a/src/test/resources/de/ctdo/bunti/hibernate-test-context.xml +++ b/src/test/resources/de/ctdo/bunti/hibernate-test-context.xml @@ -1,39 +1,45 @@ + 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"> - - - - - org.hibernate.dialect.HSQLDialect - true - - - + + + + + + + + + + - - - - - - - - - - - + + + + + + + \ No newline at end of file