我有一个Java Web应用程序,我试图通过类配置的Hibernate从Postgres数据库中检索客户端列表。据我所知,应用程序确实获得了连接,因为我检查了连接中的元数据,我看到了所有表格,所以它没问题。问题是我从表中得不到任何数据。
以下是我的客户模型:
@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 + '\'' +
'}';
}
}
以下是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;
}
}
我连接但没有来自我的Client表的数据,这些数据具有以下完全相同的字段:id(PK),age,numberofrentals,name,address。什么想法有什么不对?我正在使用JpaRepository中的findall()。我不明白可能是什么问题,一切似乎都是正确的。
如果还需要更多代码,我可以发布更多代码。
有没有办法调试这个或找出问题所在,因为到目前为止我只能得到连接和表的列表,其余的信息听起来不错? 什么想法可能是错的?
答案 0 :(得分:0)
当hibernate创建一个表时,由于某种原因它会使用所有小写字母创建它,并且它也会从那些中读取。我认为它访问区分大小写,所以我将数据添加到错误的表格中,而正确的表格始终为空。