Issue
I am new to Hibernate, so there might be many problems, but I have an Account
entity displayed here:
@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;
}
}
and I am trying to select all accounts in the database in the following function:
public List<Account> getAllAccounts() {
Query q = em.createQuery("select a from Account a");
return q.getResultList();
}
The problem is that this returns an empty list, but when run the result sql (shown in the console by <entry key="hibernate.show_sql" value="true" />
) in the Oracle SQL Developer editor, I do get results back.
What am I doing wrong?
Edit:
I am trying to create a spring web app, so my connection an hibernate are configured in spring here:
<?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>
Solution
Found the problem,
I used the wrong ojdbc
driver that did not support my exact oracle
database version, so some things worked, and some didn't.
Thanks for all the help in comments!
Answered By - Tomer Amir
Answer Checked By - Mary Flores (JavaFixing Volunteer)