Jdbc连接到数据库但不会检索行

时间:2016-05-06 16:12:41

标签: java spring hibernate postgresql jdbc

我正在使用Spring,Hibernate,Tomcat和Postgres创建一个应用程序。我只想从表中检索所有行并将它们发送到localhost / clients。

我的连接正常,我检查了从dataSource检索的信息,但我认为我的域类不正确。这是:

@Entity
@Table(name="Client")
public class Client implements Serializable {

    @Id
    @Column(name="id", nullable= false, unique = true)
    private Integer id;

    @Column(name="age", nullable = false)
    private int age;

    @Column(name="numberofrentals", nullable = false)
    private int numberOfRentals;

    @Column(name="name", nullable = false)
    private String name;

    @Column(name="address", nullable = false)
    private String address;

    public Client() {
    }

    public Client(Integer id, int age, int 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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int 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 + '\'' +
                '}';
    }
}

在我的名为Client的表中,我有id(PK),年龄,numberofrentals,地址,姓名。

我的控制器看起来像这样:

RestController
public class ClientController {

    @Autowired
    private ClientService clientService;

    @RequestMapping(value="/clients", method = RequestMethod.GET, produces = "application/vnd.api+json")
    public @ResponseBody ClientsDto getClients() {
        List<Client> clientList = clientService.findAll();
        clientList.add(new Client(1,19,0,"gtttt","aaa"));
        return new ClientsDto(clientList);
    }

clientService只有一个空的存储库,它基本上实现了JpaRepository。

clientService实现:

@Service
public class ClientServiceImpl implements ClientService {

    @Autowired
    private ClientRepository clientRepository;

    @Override
    public List<Client> findAll() {
        return clientRepository.findAll();
    }
}

我使用的是JPAConfig类,而不是xml配置。在这里我还启用JPARepository给它的路径,我使用@COnfiguration确保它被识别。 JPAConfig类如下所示:

@Configuration
@EnableJpaRepositories("shop.tryHW.core.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("shop.tryHW.core.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;
    }
}

它有什么问题吗?

我的桌子中有2个实体,但我只在我的页面上找到了硬编码的实体。可能是什么问题呢? (我检查了数据源,它实际上从数据库中看到了我的所有表,所以我认为这是可以的)。我没有得到任何错误,没有任何与数据库相关的信息。有没有办法检查发生了什么?

我是否需要在客户实体处添加更多注释?

0 个答案:

没有答案