I have a Java web application and I'm trying to retrieve a list of Clients from a Postgres database via class configured Hibernate. As far as I can tell, the application does get the connection because I checked the metadata from the connection and I see all my tables in there so it's alright. The problem is that I don't get any data from the table.
Here is my model for the Client:
@Entity
@Table(name="Client")
public class Client implements Serializable {
@Id
@Column(name="id", nullable= false)
private Integer id;
@Column(name="age", nullable = false)
private Integer age;
@Column(name="numberofrentals", nullable = false)
private Integer numberofrentals;
@Column(name="name", nullable = false)
private String name;
@Column(name="address", nullable = false)
private String address;
public Client() {
}
public Client(Integer id, Integer age, Integer numberofrentals, String name, String address) {
this.id = id;
this.age = age;
this.numberofrentals = numberofrentals;
this.name = name;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Integer getnumberofrentals() {
return numberofrentals;
}
public void setnumberofrentals(int numberofrentals) {
this.numberofrentals = numberofrentals;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Client client = (Client) o;
if (age != client.age) return false;
if (numberofrentals != client.numberofrentals) return false;
if (!id.equals(client.id)) return false;
if (!name.equals(client.name)) return false;
return address.equals(client.address);
}
@Override
public int hashCode() {
int result = id.hashCode();
result = 31 * result + age;
result = 31 * result + numberofrentals;
result = 31 * result + name.hashCode();
result = 31 * result + address.hashCode();
return result;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Client{" +
"id=" + id +
", age=" + age +
", numberofrentals=" + numberofrentals +
", name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}
Here is the code for the JPAConfiguration
@Configuration
@EnableJpaRepositories("repository")
@EnableTransactionManagement
@EnableCaching
public class JPAConfig {
// @Value("${db.jdbcURL}")
private String jdbcURL = "jdbc:postgresql://localhost:5432/Shop";
// @Value("${db.user}")
private String user = "postgres";
// @Value("${db.password}")
private String password = "*****";
// @Value("${db.generateDDL}")
private Boolean generateDDL = true;
@Bean
public DataSource dataSource() {
PGPoolingDataSource dataSource = new PGPoolingDataSource();
try {
dataSource.setUrl(jdbcURL);
} catch (SQLException e) {
e.printStackTrace();
}
dataSource.setUser(user);
dataSource.setPassword(password);
dataSource.setMaxConnections(4);
return dataSource;
}
@Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabase(Database.POSTGRESQL);
vendorAdapter.setGenerateDdl(generateDDL);
vendorAdapter.setShowSql(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("model");
factory.setDataSource(dataSource());
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public EntityManager entityManager() {
return entityManagerFactory().createEntityManager();
}
@Bean
PlatformTransactionManager transactionManager() {
JpaTransactionManager manager = new JpaTransactionManager();
manager.setEntityManagerFactory(entityManagerFactory());
return manager;
}
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}
@Bean
public CacheManager cacheManager() {
GuavaCacheManager guavaCacheManager = new GuavaCacheManager();
guavaCacheManager.setCacheBuilder(CacheBuilder.newBuilder().expireAfterAccess(2, TimeUnit.HOURS));
return guavaCacheManager;
}
}
I get connected but no data from my Client table which has these exact same fields: id(PK), age, numberofrentals, name, address. Any ideas what's wrong? I'm using the findall() from the JpaRepository. I don't understand what could be the problem, everything seems to be right.
If there's more code necessary, I can post more.
Is there any way to debug this or find out what is wrong because so far I could only get the connection and the list of tables and the rest of information sounds alright? Any ideas what could be wrong?