Hibernate没有连接到oracle数据库

时间:2014-02-06 19:15:59

标签: java spring oracle hibernate rest

我正在尝试让我的应用程序通过Hibernate从我的Oracle数据库中检索结果。当我用webservice点击它时,我得到了一个奇怪的SQL语句(我将hibernate.show_sql设置为true)。我没有收到任何错误,但我也没有得到任何结果。 (我很擅长冬眠,并对任何反馈表示感谢。谢谢)

来自控制台:

INFO: Server startup in 5743 ms
Hibernate: select standards0_.STANDARD_CODE as STANDARD1_0_, standards0_.STANDARD_NAME as STANDARD2_0_, standards0_.STANDARD_TYPE as STANDARD3_0_ from STANDARDS standards0_
0

这是我的表格的样子:

STANDARDS Columns:
Column Name             Type            Size    Primary Key    Foreign Key    Nullable    Scale
STANDARD_CODE       VARCHAR2        25          true                                            N                
STANDARD_NAME       VARCHAR2        100                                                          N                
STANDARD_TYPE       VARCHAR2        25                                                            N 

jdbc.properties:

jdbc.driverClassName=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@myurl:1521:mySID
jdbc.username=MYUSERNAME
jdbc.password=MYPASSWORD

持久性context.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       ">
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:/jdbc.properties"/>
    </bean>
    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />     
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>                
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key=" hibernate.use_sql_comments">true</prop>     
            </props>
        </property>
    </bean>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <aop:config>
        <aop:pointcut id="webMethods" expression="execution(* com.yourcompany.web.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="webMethods" />
    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
</beans>

hibernate.cfg.xml中:

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <mapping class="com.yourcompany.model.Standards"/>
    </session-factory>
</hibernate-configuration>

servlet的context.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       ">
    <context:component-scan base-package="com.yourcompany" />
    <mvc:annotation-driven />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

MainController.java:

@Controller
public class MainController {

    @Autowired
    private SessionFactory sessionFactory;

    @RequestMapping(value="/admin", method = RequestMethod.GET)
    @ResponseBody
    public String admin(){
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        List resultsList = session.createQuery("from Standards").list();
        //List resultsList = session.createSQLQuery("SELECT * FROM STANDARDS").list();
        System.out.println(resultsList.size());
        for( Standards standards : (List<Standards>) resultsList){
            System.out.println("Event (" + standards.getSTANDARD_CODE() );
        }
        session.getTransaction().commit();
        session.close();

        return "Return From Admin";
    }       
}

0 个答案:

没有答案