通过hibernate从数据库中读取数据

时间:2014-08-04 12:46:59

标签: java spring hibernate

我在问一个概念问题。我只想从数据库中读取数据。为此我在hibernate和spring中实现了db连接。

我的applicationContext.xml被指定为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                        http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <context:annotation-config />

    <context:component-scan base-package="com.TestProj"
        annotation-config="true" />

    <tx:annotation-driven transaction-manager="transactionManager"
        proxy-target-class="true" />

    <bean id="mainGUI" class="com.TestProj.gui.MainWindow" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
        <property name="url"
            value="jdbc:oracle:thin:@test:1234:test" />
        <property name="username" value="test" />
        <property name="password" value="testdb" />
        <property name="maxActive" value="20" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="jpaData" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="entityManager"
        class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

</beans>

如您所见,我想使用entity manager从数据库中读取。但是,为此,我需要定义我在域中连接的整个数据库的完整表结构。但是,我只需要在数据库上进行sql查询。如何用hibernate完成?

感谢您的“概念”答案!

2 个答案:

答案 0 :(得分:2)

  1. 您不需要为整个数据库定义结构,只需要您需要阅读的表格
  2. 如果你只需要在数据库上进行sql查询,你最好使用像SpringJDBC那样更轻量级的东西,甚至是原始的JDBC

答案 1 :(得分:1)

for that I need to define the complete table structure of the entire database 不是对于整个数据库,如果你想要hibernate管理单个表的数据,那么只需为单个表创建域对象。

However, I only need to make sql queries on the database如果您需要简单的SQL查询,那么您并没有真正利用hibernate orm或ORM理论,只需使用spring jdbc甚至是普通的jdbc框架。

如果没有域对象,则无法使用hibernate。