We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Fernando Zavaleta Muriel • 5 years ago

You really want to use your entities as payload request?

mpjmarshall • 5 years ago

Very good tutorial. I was able to get it running with few issues. Thank you.

RicardoGeek • 5 years ago

I keep getting this:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field {the autowired field} in {the ontroller path and name} required a bean of type '{the repository path and name}' that could not be found.

How do you get that Autowired to work?

Tchiko • 5 years ago

If i want to get All the Answers with Question (not by question id), how to do this ?
i mean if i've 10 Questions and 40 Answers, i want to get the 40 Answers and for each one i can see the related question

Madatek • 5 years ago

Hi! Thanks for the tutorial!

Question: do you create the database beforehand or does the program create the tables somehow?

Marcinho92 • 5 years ago

The program creates the tables. Hibernate does this.

Aferkiw N Tamazgha • 6 years ago

Hello, Thank you for your tutorial.

But I have a bug. Whane I runn the projet it's ok but when I make test with postman :

{
"title": "how to use PostgresSQL with Spring Boot and JPA",
"description" : "I want to use PostgreSQL with Spring boot and jpa and develop Restful aPIs. Please help!"
}

I'have this result

{
"timestamp": "2020-03-10T15:00:31.626+0000",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'text/plain;charset=UTF-8' not supported",
"path": "/questions"
}

I don't know what is the problem. UTF 8 with posgres database ? Pb with Spring boot utf-8 ???

I don't know ...

Hendi Santika • 6 years ago

Use POSTMAN then dont chose plaint text. Use JSON instead.

Shin Sun • 6 years ago

Hello. May I know how did you create tables? And you gave which path for postgresql? Did you export tables' and gave that file's path?

Nguyễn Thanh Việt • 6 years ago

Thank you so much !!!. It's so useful for me ^_^

Alex • 6 years ago

Thanks so much for this beautiful tutorial. Can you add some html page using ThymeLeaf ? Thanks

Julius Tanuwijaya • 6 years ago

Hi thankyou for the tutorial. I have a question.
how to make list of object?
I want to post array of JS
ON..
thankyou

Kidddd • 6 years ago

hi!!

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.1.RELEASE:run (default-cli) on project postgres-demo: An exception occurred while running. null: InvocationTargetException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]: NullPointerException -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/con...

Thakun Uppathi • 6 years ago

read a lot of fun

Saher Y • 6 years ago

Hey There, thanks for the article & the code repo.... very useful.

quick question..
is there a reason for you using findById() function every time in your controller class for (post, put, and delete), instead of just using save() & deleteById()


@DeleteMapping("/questions/{questionId}")
public Question deleteQuestion(@PathVariable Long questionId) {
return questionRepository.deleteById();
}

Also with save, it will check for the question id, if it's not already in the table it'll create a new record or else it updates the current record.

I'm still experimenting with spring-boot, and would like to know if you had a specific reason to write your functions that way, or it's just a different logic for the same results?

Thank you.

Rafa • 6 years ago

Hi,

Thank you for your article, very helpful.

I have a question:

How would you specify the schema you are using in the properties? I assume you are using spring boot 2.
You didn't mention any, so I am assuming you are using the default one called 'public' by postgres

I am actually having some issues and am unable to find the correct way to make the application go to the schema I want.

Many thanks for your help.

Best,
R.

Rajeev Singh • 6 years ago

Hi,

Check out the spring.datasource.url property. In the url jdbc:postgresql://localhost:5432/postgres_demo, "postgres_demo" is the database name. You can change it to whatever database you have created.

Rafa • 6 years ago

Yes I agree for the database, but I am referring to the schema in that database.

In previous version of springboot, if one was using hibernate, it was possible to use the spring.jpa.properties.hibernate.default_schema. But I dont see it anymore,

Do you have any idea of an alternative?

Many thx,
Rafa

Rajeev Singh • 6 years ago

Check Spring boot's common application properties. You can find something there. You can also try currentSchema parameter in the URL. Check this stackoverflow answer.

Phạm Minh Tín • 6 years ago

Hi, I just started learning Spring boot connect postgresql.
In the example, the AnswerController class, I have a problem "Cannot resolve the setQuestion" method and medthod "setText and getText". Similar in class QuestionController with method GET and SET.
I can't fix it, you can help me. Thank you

Rajeev Singh • 6 years ago

Hi, I have omitted getters and settera from Question and Answer classes for brevity.
Please define them, it will work.

You can also download the project from github

Phạm Minh Tín • 6 years ago

I understand.
thank you so much

Benedict Suarez • 6 years ago

I am having errors.
It says that "Driver org.postgresql.Driver claims to not accept jdbcUrl".

What am I doing wrong?

Felipe Belluco • 6 years ago

Did you find any solution?

Alex • 6 years ago

Very very good. Thanks man.

Officer Meow Meow • 6 years ago

Where is findByQuestionId() implemented? Does the framework parse and implement the method?

Mariusz MW • 6 years ago

This is the spring data jpa that takes care of the implementation in background

Brax TheBot • 7 years ago

silly question this is not an embedded database? postgresql needs to be running for this to work?

Martin Penckert • 7 years ago

Yes, you have to have a PostgreSQL database running somewhere. If you follow the post above literally the database has to be on the local machine listening on port 5432 (which is the default port for postgresql).

Alex • 6 years ago

Tables are created automatically when running mvn spring-boot:run? Or i have to created manually?

Ed Steel • 7 years ago

Nice article! I need some example to start with Spring and it is the best solution that I find! Thanks!

Prenel Thomas • 7 years ago

Hi Rajeev,

When I do a GET localhost: 8080 / questions / 1000 / answers he says to me:

{
"timestamp": "2019-02-22T10: 36: 16.541 + 0000",
"status": 500,
"error": "Internal Server Error",
"message": "Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate .proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList [0] -> tuto.relation.onetomany.model.Answer [ \ "subject \"] -> tuto.relation.onetomany.model.Question $ HibernateProxy $ ytAa1NLR [\ "hibernateLazyInitializer \"]) "
"path": "/ questions / 1051 / answers"
}

Prenel Thomas • 7 years ago

I resolve my problem. I just fixed this : "spring.jackson.serializatio...-on-empty-beans=false" on my application.properties.
Now it's work, but i don't know why ? If someone can tell me why ?

karan chapaneri • 7 years ago

Hello Rajeev,
Thanks for amazing tutorial. However, I've one issue. I'm getting beancreation exception for controller on running.
Can you please help?

depaniprasad • 7 years ago

Hi
Thank you so much for those tutorials keep it up.

Neetin Nagap • 7 years ago

Heyy,

How can we add another level to the existing design. Say we also have comments on an answer. Then how should the api design look like:
1. "/question/1/answer/1/comments"
OR
2. "/answer/1/comments"

Thanks,
Neetin

Rajeev Singh • 7 years ago

Hi Neetin,

As per the REST design. Option 1 is more appropriate- /questions/1/answers/4/comments

Samuel Altamirano • 7 years ago

Me funciono bien el POST cuando quise probar el GET me salio el siguiente mensaje:
{
"timestamp": "2018-12-12T21:50:04.729+0000",
"status": 500,
"error": "Internal Server Error",
"message": "No default constructor for entity: : com.example.postgresdemo.model.Question; nested exception is org.hibernate.InstantiationException: No default constructor for entity: : com.example.postgresdemo.model.Question",
"path": "/questions"
}
no se si alguien me puede orientar a que se debe

Devarajeshwaran Alagu • 7 years ago

Rajeev, thanks for the complete tutorial.
However, i have a question. 1. The property QuestionId is not available in the question repo where id is available. How is the question_id field generated in the answer table. 2. Also, the answer repository does not contain questionId property. How is that findByQuestionId is being used?

Rajeev Singh • 7 years ago

Hi,

1. The question_id column is generated and mapped by Hibernate (ORM). It's an implementation of JPA (Java Persistence API). The @ManyToOne JPA annotation is used to specify how two entities are related. Everything else is taken care of internally. Read JPA / Hibernate One to Many Mapping Example with Spring Boot for more details.

2. Spring-Data-JPA's dynamic query methods support nested fields. findByQuestionId() works because Question is a field in the Answer class and Id is a field in the Question class. You see the nested relationship: Question - Id

krachBummEnte • 7 years ago

Just tried to implement this, and I have a problem. For some reason, when ever I try to insert into the database via post request, the id is null and I get an error. The sequence is created in the database, but it seems it's never used or what ever. Anyone can help me?

Carlos Muluba • 7 years ago

working very well thanks for this tutorial.

Milan • 7 years ago

I am getting this error

Description:

Field userRepository in com.example.demo.SpringBootOneToOneApplication required a bean of type 'com.example.repository.UserRepository' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'com.example.repository.UserRepository' in your configuration.

Thanks Rajeev, for effort you put in this fantastic tutorial. You are the best! Keep going.

Rajeev Singh • 7 years ago

Thank you :-)

Marco Carballo • 7 years ago

Hello, if on console dont compile, Ill earn some time for java up to 9,add this in your pom.xml:

<dependency>
<groupid>javax.xml.bind</groupid>
<artifactid>jaxb-api</artifactid>
<version>2.3.0</version>
</dependency>

other java versions add this

<dependency>
<groupid>org.hibernate</groupid>
<artifactid>hibernate-core</artifactid>
<version>4.1.4.Final</version>
</dependency>

and

<dependency>
<groupid>org.hibernate</groupid>
<artifactid>hibernate-entitymanager</artifactid>
<version>5.2.3.Final</version>
</dependency>

I hope this help you

Molinex • 7 years ago

Hey man, I'm starting with spring, and I followed his tutorial, by showing a complete Rest API ...
However, when I test the service, the POST method returns the following error:


Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.example.postgresdemo.model.Question com.example.postgresdemo.controller.QuestionController.createQuestion(com.example.postgresdemo.model.Question)


Can you help me?
Do you know why it happens?

Rajeev Singh • 7 years ago

It says, request body is missing. Are you passing the request body as application/json?
Please refer the first Postman screenshot titled Create Question - POST /questions.

Molinex • 7 years ago

No, I was not. My mistake...
Then I did tests with the curl, and with ajax, and everything worked as expected. The problem was actually the way I made the request in the postman (without the body request, wrong) ...
Thanks for your attention, and patience, sorry for my inattention, and congratulations for the great article / tutorial, it is helping me a lot ...

Kevin auryn • 7 years ago

How to run the project in eclipse? I'm using eclipse oxygen and postman to run the API.

Please help. Thank you