0

I am trying to retrieve a list from a crudRepository by doing a .findAll() request and then pass it onto a html which lists them in a table. It retrieves the data fine except for the Dates which gives me a strange error. I am using Postgresql.

My create table sql:

--Table:conference_table
CREATE TABLE conference_table (
conference_id SERIAL NOT NULL,
user_id INT NOT NULL,
name VARCHAR(32),
description VARCHAR(255),
startConference DATE,
endConference DATE,
PRIMARY KEY (conference_id),
FOREIGN KEY (user_id) REFERENCES user_table(user_id)
);

In my conference.java I have:

@Column(name = "startConference")
private Date startConference;

@Column(name = "endConference")
private Date endConference;

In my controller class I have the method:

@GetMapping(path="/configure")
public String showConfigurePage(Model model){
    List<Conference> conferenceList = conferenceRepository.findAll(); // This gives me the error
    model.addAttribute("conferenceList", conferenceList);
    return "configure";
}

Error:

org.postgresql.util.PSQLException: ERROR: column conference0_.end_conference does not exist
  Hint: Perhaps you meant to reference the column "conference0_.endconference".

Any ideas why this might be happening? I can provide further info if I missed any.

Lukas
  • 105
  • 1
  • 2
  • 9
  • Can I see your database configuration? You surely have the tables generated or are they already there ? if you have them generated have you tried to view the ddls during the process ? – H4x9r Apr 26 '21 at 05:48
  • By database configuration do you mean my database script? I do insert a test value with my script that I create the database with: `INSERT INTO conference_table(user_id, name, description, startconference, endconference) VALUES (3, 'testConference', 'this is a test conference', '2021-04-26', '2021-04-29');` – Lukas Apr 26 '21 at 05:53
  • I tweaked the Conference.java class and changed the startConference & endConference to all lower case and now I get a different error : `Property or field 'startconference' cannot be found on object of type 'com.lukas.ramonas.cms.Model.Conference' - maybe not public or not valid?` – Lukas Apr 26 '21 at 06:10
  • i mean the configuratin you wrote in your application.yml. Currently I suspect that the selected dialect is not correct, but who knows ^^. There are also other options that can help you, like options for field naming. Here is a sample-configuration https://github.com/springframeworkguru/spring-boot-postgress-example/blob/master/src/main/resources/application.properties – H4x9r Apr 26 '21 at 06:14
  • Thank you for your reply! My application has a application.properties instead of a .yml and I dont think there is anything wrong in that file. Only thing related to the database there is a link to the address of the database. Furthermore, everything works fine if I set the startconference and endconference public, but doing that on a model is really bad practice and I need to figure out a way to do it private. – Lukas Apr 26 '21 at 06:20
  • Oh i see, did you also create getter and setter for the attributes, according to the java-bean convention ? – H4x9r Apr 26 '21 at 06:22
  • I just noticed that I had those commented out. I uncommented them and all is working well now(I assume CrudRepository uses these methods for .findAll()). So my issue is resolved by turning my startConference and endConference lowercase and having getters and setters for them. Thank you for your help! – Lukas Apr 26 '21 at 06:34

1 Answers1

0

What you see is correct behavior: The default strategy for @Column(name="endConference") is end_conference.

You can add below config to application.properties to follow PhysicalNamingStrategy:

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

Check this and this for more info.

References:

adarsh
  • 1,393
  • 1
  • 8
  • 16