我是Hibernate的新手,所以可能会有很多问题,但是我Account
在这里显示了一个实体:
@Entity
@Table(name = "TABLE_NAME")
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private int id;
@Column(name = "EMAIL", unique = true)
private String email;
@Column(name = "PASSWORD", length = 128)
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
我正在尝试通过以下功能选择数据库中的所有帐户:
public List<Account> getAllAccounts() {
Query q = em.createQuery("select a from Account a");
return q.getResultList();
}
问题是,这将返回一个空列表,但是<entry key="hibernate.show_sql" value="true" />
在Oracle SQL Developer编辑器中运行结果sql(在控制台中显示为)时,我确实获取了结果。
我究竟做错了什么?
编辑:
我正在尝试创建一个Spring Web应用程序,因此我在这里在Spring中配置了一个休眠连接:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.checkpoint.core.repositories.jpa" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@[[url]]:[[port]]:[[sid]]" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<map>
<entry key="hibernate.hbm2ddl.auto" value="validate" />
<entry key="hibernate.show_sql" value="true" />
<entry key="hibernate.format_sql" value="true" />
<entry key="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
</map>
</property>
<property name="packagesToScan" value="com.checkpoint.core.models.entities" />
</bean>
<!-- Setup transaction management -->
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" />
<context:component-scan base-package="com.checkpoint.core.services.impl" />
</beans>
发现问题后,我使用了ojdbc
不支持我的确切oracle
数据库版本的错误驱动程序,因此某些事情起作用了,而有些却不起作用。
感谢您在评论中提供的所有帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句