[英]Hibernate connects to Postgres db but it doesn't retrieve data
I have a Java web application and I'm trying to retrieve a list of Clients from a Postgres database via class configured Hibernate. 我有一个Java Web应用程序,我试图通过配置为Hibernate的类从Postgres数据库中检索客户端列表。 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 这是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. 我已连接,但没有来自我的“客户”表的数据,这些表具有以下完全相同的字段:id(PK),年龄,租用数量,名称,地址。 Any ideas what's wrong?
任何想法有什么问题吗? I'm using the findall() from the JpaRepository.
我正在使用JpaRepository中的findall()。 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?
任何想法可能有什么问题吗?
When hibernate creates a table, for some reason it creates it with all lower case letters and it will also read from those. 当hibernate创建一个表时,由于某种原因,它会使用所有小写字母创建该表,并且也会从中读取它们。 I thought it accesses case-sensitive so I was adding data into the wrong table tables whereas the correct ones remained always empty.
我以为它访问区分大小写,所以我将数据添加到错误的表中,而正确的表始终为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.