0

I am asking a conceptual question. I want to only read data from a database. For that I have implemented the db connection in hibernate and spring.

My applicationContext.xml is specified like that:

<?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>

As you can see I would like to use the entity manager to read from the database. However, for that I need to define the complete table structure of the entire database I am connecting to in my domain. However, I only need to make sql queries on the database. How can this be done with hibernate?

I appreciate your "conceptual" answer!

2 Answers 2

2
  1. You don't need to define the structure for the entire database, just the tables you need to read
  2. If you only need to make sql queries on the database, you are much better off using something more lightweight like SpringJDBC or even raw JDBC
Sign up to request clarification or add additional context in comments.

Comments

1

for that I need to define the complete table structure of the entire database Not for entire db, if you want hibernate manage data for a single table then just create domain object for single table.

However, I only need to make sql queries on the database if You need plain sql query then you are not really taking advantage of hibernate orm or ORM theory, just use spring jdbc or even plain jdbc framework.

Without domain object you cannot use hibernate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.