简体   繁体   中英

Spring boot + Postgres + JPA + Heroku not persisting data and not logging

I have a very simple Spring Boot application deployed on Heroku. The application itself works fine in terms of the Controller - the API returns some simple JSON, nothing too special.

I've added a Postgres database to Heroku and on application startup I see that Spring creates the table as expected. It is only 1 table with a super simple mapping class:

@Entity
public class Score implements Serializable {
private static final long serialVersionUID = -2899157432342241888L;

private String customer;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

private Date date = new Date();

private int score;

//setters and getters

The repository is just an interface with no explicit implementation:

@Repository
public interface ScoreRepository extends CrudRepository<Score, Long>

I call repository.save(...) in a @Service annotated class that has the repository auto-wired in. I assume that is working ok, because I'm not seeing any errors - yet I don't see any records being persisted to the database! No idea what could be going wrong here; I suppose I could have set up Heroku wrong, or maybe there's something obvious that I missed with the Postgres setup in Heroku?

Secondary question

Maybe related, but logging doesn't seem to work too well either. According to Heroku documentation I can print to standard out and it should appear in the log, but this doesn't happen. Moreover I have this in properties file:

spring.jpa.show-sql=true

But I see no such logging of SQL queries in the heroku logs.

Any help much appreciated! Thanks in advance.

Logging issue: when you start your application, do you see any errors in console (first few lines even before spring logo) saying multiple version of logging jar (slf4j.jar) , if so you may not see logs. If this is true, then check your maven depencies and exclude all unncessary slf4j.jar

and start your spring application with debug.

java -jar myapp.jar --debug

persistence issue: Make sure you run it in transaction (in spring context)

After you solve your logging issue you can figure this out i believe, But you can debug your code and see if request reaches your save method, and then you can step into to find if there is any issue, But i would definitely see logs to find errors if any.

I don't see a setter. Do you even have it as all I see is comment // Getter and setter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM