问题
i have an application thats build on Spring boot, using JPA repositories on HSQL database.
Problem is that while application is running, I create an entity,and it's persisted correctly to database(can be seen in database manager). But after application shutdown from eclipse, all data is removed;
Saving is performed like this
@Service
public class NotificationService {
@Autowired
private NotificationRepository notificationRepository;
public void notifyRefreshArticles(){
Notification notification = new Notification();
notification.setCreatedAt(LocalDateTime.now());
notification.setNotificationSeverity(NotificationSeverity.NORMAL);
notification.setNotificationType(NotificationType.REFRESH_ARTICLES);
notificationRepository.save(notification);
}
}
I pretty sure its configuration issue,but with spring boot basically only configuration that i have is this configuration file.
spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
spring.datasource.url=jdbc:hsqldb:hsql://localhost/rr_app_database
spring.datasource.username=XXXXXX
spring.datasource.password=XXXXXX
spring.datasource.show-sql=true
回答1:
Do you have hbm2ddl text somewhere in your configuration properties. It should be set to update
or none
, apparently you might have create-drop
.
回答2:
Specify a local filename in application.properties data source URL:
spring.datasource.url=jdbc:hsqldb:file:/home/username/testedb
You can remove the spring.datasource.driver-class-name
property as Spring Boot detects it by URL property.
回答3:
- If you insert the data within a
@Test
, it's rolled back by default. - Your database might get dropped, if you restart the application, like LearningPhase suggested.
- The
insert
is never really committed - because it's outside of an transaction or because anException
is thrown and not handeled within the transaction.
回答4:
Check the properties files if there exists a configuration line as below
spring.jpa.hibernate.ddl-auto=create
Just remove it or change to
spring.jpa.hibernate.ddl-auto=update
来源:https://stackoverflow.com/questions/36605104/spring-jpa-data-removed-after-shutdown-of-application