I am trying to configure Spring Boot (version 1.4.2) to use my localhost MySQL database but there is something wrong with my configuration. Could you please take a look at this very simple example and tell me what is going on?
I have all the required dependencies in place:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
My entity:
@Entity
@Table(name = "greetings")
public class Greeting {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "greeting_id")
private long id;
@Column(name = "text")
private String text;
// getters and setters here
}
Repository:
@Repository
public interface GreetingRepository extends JpaRepository<Greeting, Long> {
}
Controller:
@RestController
@RequestMapping("/hello")
public class GreetingsController {
@Autowired
private GreetingRepository repo;
@RequestMapping(method = RequestMethod.GET)
public Collection<Greeting> getGreetings() {
return repo.findAll();
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Greeting getGreeting(@PathVariable Long id) {
return repo.findOne(id);
}
}
application.properties:
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.datasource.url=jdbc:mysql://localhost/example
spring.datasource.username=root
spring.datasource.password=secret
spring.datasource.driverClassName=com.mysql.jdbc.Driver
There is a MySQL database running on localhost called 'example' with the following table (and couple of records I inserted in there):
mysql> describe greetings;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| greeting_id | bigint(20) | NO | PRI | NULL | auto_increment |
| text | varchar(255) | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
EDIT: I corrected my controller and updated my question. The problem is that my application uses in-memory datastore instead of my MySQL database. IF I create a POST endpoint and insert new entity into the database, I am able to retrieve it but it seems that it is different than my existing database since I am not getting any data from there.
EDIT2: resolved by spring.jpa.hibernate.ddl-auto=update