我是Heroku的新手,但过去曾使用过Hibernate。我正在尝试查询我的Heroku数据库,但我没有得到任何结果。我知道我查询的数据库表中有一条记录。
如果有人能够发现我忽略的东西(经过多次搜索,头脑刮擦等),我将非常感激
这是我的servlet.xml文件(Spring)
<bean id="dbUrl" class="java.net.URI">
<constructor-arg value="#{systemEnvironment['DATABASE_URL']}"/>
<!-- <constructor-arg value="postgres://hfdovnidtaqsou:hK6uveO4d7nls_ogdrKjsSYzGs@ec2-54-195-243-250.eu-west-1.compute.amazonaws.com:5432/db7nnh2r11om9c"/> -->
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="#{ 'jdbc:postgresql://' + @dbUrl.getHost() + ':' + @dbUrl.getPort() + @dbUrl.getPath() }"/>
<property name="username" value="#{ @dbUrl.getUserInfo().split(':')[0] }"/>
<property name="password" value="#{ @dbUrl.getUserInfo().split(':')[1] }"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="${hibernate.config}" p:packagesToScan="com.yomi.*">
<property name="annotatedClasses">
<list>
<value>com.yomi.beans.User</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />
这是我的hibernate.cfg
<hibernate-configuration>
<session-factory>
<!-- <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<property name="defaultNChar">true</property>
</session-factory>
最后,这是我尝试查询数据库的类:
@Transactional
public class UserTestServiceImpl implements UserTestService {
private SessionFactory sessionFactory;
private final Log logger = LogFactory.getLog(getClass());
/**
* @param sessionFactory the sessionFactory to set
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public List<User> readUsers() {
// Retrieve session from Hibernate
Session session = sessionFactory.getCurrentSession();
logger.warn("=============== session=" + session + " ================");
logger.warn("=============== session.isOpen()=" + session.isOpen() + " ================");
// Create a Hibernate query (HQL)
Query query = session.createQuery("FROM com.yomi.beans.User");
List<User> users = query.list();
logger.info("=======================================users.size():" + users.size());
return users;
}
}