- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
Иногда чтобы не засорять наш проект SQL запросами, нам приходиться создавать хранимые процедуры со всеми вытекающими требованиями. Возникает вопрос как вызвать процедуру и распределить по объектам с помощью Spring Hibernate.
Начнем с того какие нам нужны для этого инструменты.
Настройки spring - context.xml
Создадим DAO класс - HiberDAO
Создадим bean класс - IFriendsOnline.java
Начнем с того какие нам нужны для этого инструменты.
- IDE: IntelliJ IDEA 10.5
- Builder: Maven 3.0.2.
- Framework: Spring Hibernate.
- Servlet Container: Tomcat 7.
- DataBase: PostgreSQL 8.3
Процедура выглядит следующим образом(friends_online):
CREATE OR REPLACE FUNCTION "public"."friends_online" ( "user_id" bigint ) RETURNS SETOF record AS $body$ DECLARE user_id BIGINT = $1; BEGIN return QUERY select su.user_id, su.name, su.midname from user_friends uf INNER JOIN user su ON su.user_id=uf.friend_id INNER JOIN user_onlines uo ON uo.user_id=su.user_id where uf.user_id=user_id; END; $body$ LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER COST 100 ROWS 30;
Настройки spring - context.xml
<beans xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:/jdbc.properties</value> </list> </property> </bean> <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.databaseurl}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <import resource="hibernate.cfg.xml" /> <bean class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="transactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> <bean class="com.test.dao.HiberDAO" id="hiberDAO"> <property name="sessionFactory" ref="sessionFactory" /></bean> </beans>Настройки Hibernate: hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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-2.5.xsd"> <!-- Hibernate session factory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="annotatedClasses"> <list> <value>com.test.beans.IFriendsOnline</value> </list> </property> <property name="hibernateProperties"> <props> prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.c3p0.minPoolSize">5</prop> <prop key="hibernate.c3p0.maxPoolSize">10</prop> <prop key="hibernate.c3p0.timeout">600</prop> <prop key="hibernate.c3p0.max_statement">50</prop> <prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop> <!--<prop key="hibernate.hbm2dll.auto">create</prop>--> </props> </property> </bean> </beans>
Создадим DAO класс - HiberDAO
public class HiberDAO{ private HibernateTemplate hibernateTemplate; public void setSessionFactory(SessionFactory sessionFactory) { this.hibernateTemplate = new HibernateTemplate(sessionFactory); @SuppressWarnings("unchecked") public List<IFriendsOnline> findByLastNameUsingStoredProcedure(final Long id) { return (List<IFriendsOnline>) hibernateTemplate.execute(new HibernateCallback() { public Object doInHibernate(final Session session) throws HibernateException, SQLException { return session.getNamedQuery("friends_online").setParameter("id", 26).list(); } }); }}
Создадим bean класс - IFriendsOnline.java
@Entity @NamedNativeQueries({@NamedNativeQuery(name="friends_online", query=" select fr_id,friend_name,friend_midname from friends_online(:id) as t(fr_id bigint,friend_name varchar, friend_midname varchar )", resultClass=IFriendsOnline.class)}) public class IFriendsOnline { private Long fr_id; private String friend_name; private String friend_midname; @Id public Long getFr_id() { return fr_id; } public void setFr_id(Long fr_id) { this.fr_id = fr_id; } @Type(type = "string") public String getFriend_midname() { return friend_midname; } public void setFriend_midname(String friend_midname) { this.friend_midname = friend_midname; } @Type(type = "string") public String getFriend_name() { return friend_name; } public void setFriend_name(String friend_name) { this.friend_name = friend_name; } }
Теперь решающий аккорд, достаем это все в list :
List<IFriendsOnline> friendsOnline = ContextLoader.getHiberDAO(). findByLastNameUsingStoredProcedure(users.getId());
Comments
Post a Comment