gpt4 book ai didi

java - Hibernate 查询返回空结果列表

转载 作者:太空宇宙 更新时间:2023-11-04 13:10:14 24 4
gpt4 key购买 nike

我是 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();
}

问题是这会返回一个空列表,但是当在 Oracle SQL Developer 编辑器中运行结果 sql(在控制台中显示为 <entry key="hibernate.show_sql" value="true" /> )时,我确实得到了结果。

我做错了什么?

编辑:

我正在尝试创建一个 Spring Web 应用程序,因此我的连接和 Hibernate 是在 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 数据库版本,因此有些功能有效,有些则无效。

感谢评论中的所有帮助!

关于java - Hibernate 查询返回空结果列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34039750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com