Spring Data not committing to DB





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I'm wondering what the best practice is to translate from DTO to DataBase through Spring Data ?



Here is what I've tried so far :



    @Transactional
public void saveTemplatesFromDTOList(List<TemplateDTO> templateDTOList) {

for (TemplateDTO templateDTO : templateDTOList) {

em.merge(templateDTO);

}
}


However, this doesn't work. Any other way I've tried with Spring Data repository save method don't work. Notably, I've tried to fetch the entity back from DB through its id, and then update it, and save, but this doesn't seem to commit.



Thanks much.



=============



Hello Nathan. Thanks. It doesn't update in the logs either. No UPDATE instruction just SELECT



here are the application.properties settings :



#==== connect to calanco ======#
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=xxx
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.use-new-id-generator-mapping=true

#temporary settings
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.type=trace
spring.jpa.show-sql=true
logging.level.org.hibernate=TRACE


=============



UPDATE : Happy New Year Ken Chan ;) I did try to fetch the entity back from DB, update it, and save, but it doesn't persist into DB.



Here's the code :



@Transactional
public void saveTemplatesFromDTOList(List<TemplateDTO> templateDTOList) {
for (TemplateDTO templateDTO : templateDTOList) {
Template templateFetchFromDB = templateRepo.getOne(templateDTO.getId());

EntityToDTOConverter.fillTemplateEntityFromTemplateDTO(templateDTO, templateFetchFromDB); // does update the fields from DTO to entity fetched from DB

templateRepo.save(templateFetchFromDB);

em.persist(templateFetchFromDB); //doesn't work
em.flush();
}
}









share|improve this question




















  • 1





    are you using any ORM tool like hibernate or not. If yes then, show the configuration file also.

    – Jabongg
    Jan 3 at 13:56











  • Hello. I believe Spring Data "magic" does use Hibernate as a base. No special config just annotations (Spring Boot).

    – FrèreToc
    Jan 3 at 13:59











  • Hello Nathan. Thanks. It doesn't update in the logs either. No UPDATE instruction just SELECT

    – FrèreToc
    Jan 3 at 14:01








  • 1





    Why you do not use JpaRepository methodes ?

    – TinyOS
    Jan 3 at 14:14











  • Back after some time. Thanks TinyOS. Using Spring Data Repository already. And method "save" does a "EntityManager.persist" in the background. Honestly, don't know what solved the problem...

    – FrèreToc
    Feb 12 at 9:53


















1















I'm wondering what the best practice is to translate from DTO to DataBase through Spring Data ?



Here is what I've tried so far :



    @Transactional
public void saveTemplatesFromDTOList(List<TemplateDTO> templateDTOList) {

for (TemplateDTO templateDTO : templateDTOList) {

em.merge(templateDTO);

}
}


However, this doesn't work. Any other way I've tried with Spring Data repository save method don't work. Notably, I've tried to fetch the entity back from DB through its id, and then update it, and save, but this doesn't seem to commit.



Thanks much.



=============



Hello Nathan. Thanks. It doesn't update in the logs either. No UPDATE instruction just SELECT



here are the application.properties settings :



#==== connect to calanco ======#
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=xxx
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.use-new-id-generator-mapping=true

#temporary settings
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.type=trace
spring.jpa.show-sql=true
logging.level.org.hibernate=TRACE


=============



UPDATE : Happy New Year Ken Chan ;) I did try to fetch the entity back from DB, update it, and save, but it doesn't persist into DB.



Here's the code :



@Transactional
public void saveTemplatesFromDTOList(List<TemplateDTO> templateDTOList) {
for (TemplateDTO templateDTO : templateDTOList) {
Template templateFetchFromDB = templateRepo.getOne(templateDTO.getId());

EntityToDTOConverter.fillTemplateEntityFromTemplateDTO(templateDTO, templateFetchFromDB); // does update the fields from DTO to entity fetched from DB

templateRepo.save(templateFetchFromDB);

em.persist(templateFetchFromDB); //doesn't work
em.flush();
}
}









share|improve this question




















  • 1





    are you using any ORM tool like hibernate or not. If yes then, show the configuration file also.

    – Jabongg
    Jan 3 at 13:56











  • Hello. I believe Spring Data "magic" does use Hibernate as a base. No special config just annotations (Spring Boot).

    – FrèreToc
    Jan 3 at 13:59











  • Hello Nathan. Thanks. It doesn't update in the logs either. No UPDATE instruction just SELECT

    – FrèreToc
    Jan 3 at 14:01








  • 1





    Why you do not use JpaRepository methodes ?

    – TinyOS
    Jan 3 at 14:14











  • Back after some time. Thanks TinyOS. Using Spring Data Repository already. And method "save" does a "EntityManager.persist" in the background. Honestly, don't know what solved the problem...

    – FrèreToc
    Feb 12 at 9:53














1












1








1








I'm wondering what the best practice is to translate from DTO to DataBase through Spring Data ?



Here is what I've tried so far :



    @Transactional
public void saveTemplatesFromDTOList(List<TemplateDTO> templateDTOList) {

for (TemplateDTO templateDTO : templateDTOList) {

em.merge(templateDTO);

}
}


However, this doesn't work. Any other way I've tried with Spring Data repository save method don't work. Notably, I've tried to fetch the entity back from DB through its id, and then update it, and save, but this doesn't seem to commit.



Thanks much.



=============



Hello Nathan. Thanks. It doesn't update in the logs either. No UPDATE instruction just SELECT



here are the application.properties settings :



#==== connect to calanco ======#
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=xxx
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.use-new-id-generator-mapping=true

#temporary settings
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.type=trace
spring.jpa.show-sql=true
logging.level.org.hibernate=TRACE


=============



UPDATE : Happy New Year Ken Chan ;) I did try to fetch the entity back from DB, update it, and save, but it doesn't persist into DB.



Here's the code :



@Transactional
public void saveTemplatesFromDTOList(List<TemplateDTO> templateDTOList) {
for (TemplateDTO templateDTO : templateDTOList) {
Template templateFetchFromDB = templateRepo.getOne(templateDTO.getId());

EntityToDTOConverter.fillTemplateEntityFromTemplateDTO(templateDTO, templateFetchFromDB); // does update the fields from DTO to entity fetched from DB

templateRepo.save(templateFetchFromDB);

em.persist(templateFetchFromDB); //doesn't work
em.flush();
}
}









share|improve this question
















I'm wondering what the best practice is to translate from DTO to DataBase through Spring Data ?



Here is what I've tried so far :



    @Transactional
public void saveTemplatesFromDTOList(List<TemplateDTO> templateDTOList) {

for (TemplateDTO templateDTO : templateDTOList) {

em.merge(templateDTO);

}
}


However, this doesn't work. Any other way I've tried with Spring Data repository save method don't work. Notably, I've tried to fetch the entity back from DB through its id, and then update it, and save, but this doesn't seem to commit.



Thanks much.



=============



Hello Nathan. Thanks. It doesn't update in the logs either. No UPDATE instruction just SELECT



here are the application.properties settings :



#==== connect to calanco ======#
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=xxx
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.use-new-id-generator-mapping=true

#temporary settings
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.type=trace
spring.jpa.show-sql=true
logging.level.org.hibernate=TRACE


=============



UPDATE : Happy New Year Ken Chan ;) I did try to fetch the entity back from DB, update it, and save, but it doesn't persist into DB.



Here's the code :



@Transactional
public void saveTemplatesFromDTOList(List<TemplateDTO> templateDTOList) {
for (TemplateDTO templateDTO : templateDTOList) {
Template templateFetchFromDB = templateRepo.getOne(templateDTO.getId());

EntityToDTOConverter.fillTemplateEntityFromTemplateDTO(templateDTO, templateFetchFromDB); // does update the fields from DTO to entity fetched from DB

templateRepo.save(templateFetchFromDB);

em.persist(templateFetchFromDB); //doesn't work
em.flush();
}
}






java spring hibernate spring-boot spring-data






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 4 at 1:30









Martijn Pieters

727k14425532354




727k14425532354










asked Jan 3 at 13:54









FrèreTocFrèreToc

63




63








  • 1





    are you using any ORM tool like hibernate or not. If yes then, show the configuration file also.

    – Jabongg
    Jan 3 at 13:56











  • Hello. I believe Spring Data "magic" does use Hibernate as a base. No special config just annotations (Spring Boot).

    – FrèreToc
    Jan 3 at 13:59











  • Hello Nathan. Thanks. It doesn't update in the logs either. No UPDATE instruction just SELECT

    – FrèreToc
    Jan 3 at 14:01








  • 1





    Why you do not use JpaRepository methodes ?

    – TinyOS
    Jan 3 at 14:14











  • Back after some time. Thanks TinyOS. Using Spring Data Repository already. And method "save" does a "EntityManager.persist" in the background. Honestly, don't know what solved the problem...

    – FrèreToc
    Feb 12 at 9:53














  • 1





    are you using any ORM tool like hibernate or not. If yes then, show the configuration file also.

    – Jabongg
    Jan 3 at 13:56











  • Hello. I believe Spring Data "magic" does use Hibernate as a base. No special config just annotations (Spring Boot).

    – FrèreToc
    Jan 3 at 13:59











  • Hello Nathan. Thanks. It doesn't update in the logs either. No UPDATE instruction just SELECT

    – FrèreToc
    Jan 3 at 14:01








  • 1





    Why you do not use JpaRepository methodes ?

    – TinyOS
    Jan 3 at 14:14











  • Back after some time. Thanks TinyOS. Using Spring Data Repository already. And method "save" does a "EntityManager.persist" in the background. Honestly, don't know what solved the problem...

    – FrèreToc
    Feb 12 at 9:53








1




1





are you using any ORM tool like hibernate or not. If yes then, show the configuration file also.

– Jabongg
Jan 3 at 13:56





are you using any ORM tool like hibernate or not. If yes then, show the configuration file also.

– Jabongg
Jan 3 at 13:56













Hello. I believe Spring Data "magic" does use Hibernate as a base. No special config just annotations (Spring Boot).

– FrèreToc
Jan 3 at 13:59





Hello. I believe Spring Data "magic" does use Hibernate as a base. No special config just annotations (Spring Boot).

– FrèreToc
Jan 3 at 13:59













Hello Nathan. Thanks. It doesn't update in the logs either. No UPDATE instruction just SELECT

– FrèreToc
Jan 3 at 14:01







Hello Nathan. Thanks. It doesn't update in the logs either. No UPDATE instruction just SELECT

– FrèreToc
Jan 3 at 14:01






1




1





Why you do not use JpaRepository methodes ?

– TinyOS
Jan 3 at 14:14





Why you do not use JpaRepository methodes ?

– TinyOS
Jan 3 at 14:14













Back after some time. Thanks TinyOS. Using Spring Data Repository already. And method "save" does a "EntityManager.persist" in the background. Honestly, don't know what solved the problem...

– FrèreToc
Feb 12 at 9:53





Back after some time. Thanks TinyOS. Using Spring Data Repository already. And method "save" does a "EntityManager.persist" in the background. Honestly, don't know what solved the problem...

– FrèreToc
Feb 12 at 9:53












1 Answer
1






active

oldest

votes


















0














merge() only works for the @Entity instance . It does not work on DTO.



The correct way is to use entityManager to get the entity which require update , then update its value by invoking its methods. When the transaction commits , the changes you made in the entity will be updated to database. Please note that you don't need to call merge() for the update which I notice many boys are doing wrong. merge() is for updating the detached entities back to the database which is another story.



So, the general flow for updating one DTO is something like below (Please modify it for the list version for exercise :D)



@Transactional
public void saveTemplatesFromDTO(TemplateDTO dto) {
Template template = em.find(Template.class , dto.getTemplateId());

//Get DTO value to update template
template.setFoo(dto.getFoo());
template.setBar(dto.getBar());
//blablabla......

}





share|improve this answer
























  • Happy New Year ;) I did try to fetch the entity back from DB, update it, and save, but it doesn't persist into DB. Here's the code :

    – FrèreToc
    Jan 3 at 17:36













  • Happy New Year ~ Then most probably it is due to you do not configure transaction property.At least , you have to @EnableTransactionManagement in your spring boot configuration and define the PlatformTransactionManager......

    – Ken Chan
    Jan 3 at 17:39











  • Thanks Ken Chan for the time. Working now (edited the OQuestion). No need for configuring the transaction, it seems

    – FrèreToc
    Feb 12 at 10:04












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023687%2fspring-data-not-committing-to-db%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














merge() only works for the @Entity instance . It does not work on DTO.



The correct way is to use entityManager to get the entity which require update , then update its value by invoking its methods. When the transaction commits , the changes you made in the entity will be updated to database. Please note that you don't need to call merge() for the update which I notice many boys are doing wrong. merge() is for updating the detached entities back to the database which is another story.



So, the general flow for updating one DTO is something like below (Please modify it for the list version for exercise :D)



@Transactional
public void saveTemplatesFromDTO(TemplateDTO dto) {
Template template = em.find(Template.class , dto.getTemplateId());

//Get DTO value to update template
template.setFoo(dto.getFoo());
template.setBar(dto.getBar());
//blablabla......

}





share|improve this answer
























  • Happy New Year ;) I did try to fetch the entity back from DB, update it, and save, but it doesn't persist into DB. Here's the code :

    – FrèreToc
    Jan 3 at 17:36













  • Happy New Year ~ Then most probably it is due to you do not configure transaction property.At least , you have to @EnableTransactionManagement in your spring boot configuration and define the PlatformTransactionManager......

    – Ken Chan
    Jan 3 at 17:39











  • Thanks Ken Chan for the time. Working now (edited the OQuestion). No need for configuring the transaction, it seems

    – FrèreToc
    Feb 12 at 10:04
















0














merge() only works for the @Entity instance . It does not work on DTO.



The correct way is to use entityManager to get the entity which require update , then update its value by invoking its methods. When the transaction commits , the changes you made in the entity will be updated to database. Please note that you don't need to call merge() for the update which I notice many boys are doing wrong. merge() is for updating the detached entities back to the database which is another story.



So, the general flow for updating one DTO is something like below (Please modify it for the list version for exercise :D)



@Transactional
public void saveTemplatesFromDTO(TemplateDTO dto) {
Template template = em.find(Template.class , dto.getTemplateId());

//Get DTO value to update template
template.setFoo(dto.getFoo());
template.setBar(dto.getBar());
//blablabla......

}





share|improve this answer
























  • Happy New Year ;) I did try to fetch the entity back from DB, update it, and save, but it doesn't persist into DB. Here's the code :

    – FrèreToc
    Jan 3 at 17:36













  • Happy New Year ~ Then most probably it is due to you do not configure transaction property.At least , you have to @EnableTransactionManagement in your spring boot configuration and define the PlatformTransactionManager......

    – Ken Chan
    Jan 3 at 17:39











  • Thanks Ken Chan for the time. Working now (edited the OQuestion). No need for configuring the transaction, it seems

    – FrèreToc
    Feb 12 at 10:04














0












0








0







merge() only works for the @Entity instance . It does not work on DTO.



The correct way is to use entityManager to get the entity which require update , then update its value by invoking its methods. When the transaction commits , the changes you made in the entity will be updated to database. Please note that you don't need to call merge() for the update which I notice many boys are doing wrong. merge() is for updating the detached entities back to the database which is another story.



So, the general flow for updating one DTO is something like below (Please modify it for the list version for exercise :D)



@Transactional
public void saveTemplatesFromDTO(TemplateDTO dto) {
Template template = em.find(Template.class , dto.getTemplateId());

//Get DTO value to update template
template.setFoo(dto.getFoo());
template.setBar(dto.getBar());
//blablabla......

}





share|improve this answer













merge() only works for the @Entity instance . It does not work on DTO.



The correct way is to use entityManager to get the entity which require update , then update its value by invoking its methods. When the transaction commits , the changes you made in the entity will be updated to database. Please note that you don't need to call merge() for the update which I notice many boys are doing wrong. merge() is for updating the detached entities back to the database which is another story.



So, the general flow for updating one DTO is something like below (Please modify it for the list version for exercise :D)



@Transactional
public void saveTemplatesFromDTO(TemplateDTO dto) {
Template template = em.find(Template.class , dto.getTemplateId());

//Get DTO value to update template
template.setFoo(dto.getFoo());
template.setBar(dto.getBar());
//blablabla......

}






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 14:39









Ken ChanKen Chan

41.6k1596114




41.6k1596114













  • Happy New Year ;) I did try to fetch the entity back from DB, update it, and save, but it doesn't persist into DB. Here's the code :

    – FrèreToc
    Jan 3 at 17:36













  • Happy New Year ~ Then most probably it is due to you do not configure transaction property.At least , you have to @EnableTransactionManagement in your spring boot configuration and define the PlatformTransactionManager......

    – Ken Chan
    Jan 3 at 17:39











  • Thanks Ken Chan for the time. Working now (edited the OQuestion). No need for configuring the transaction, it seems

    – FrèreToc
    Feb 12 at 10:04



















  • Happy New Year ;) I did try to fetch the entity back from DB, update it, and save, but it doesn't persist into DB. Here's the code :

    – FrèreToc
    Jan 3 at 17:36













  • Happy New Year ~ Then most probably it is due to you do not configure transaction property.At least , you have to @EnableTransactionManagement in your spring boot configuration and define the PlatformTransactionManager......

    – Ken Chan
    Jan 3 at 17:39











  • Thanks Ken Chan for the time. Working now (edited the OQuestion). No need for configuring the transaction, it seems

    – FrèreToc
    Feb 12 at 10:04

















Happy New Year ;) I did try to fetch the entity back from DB, update it, and save, but it doesn't persist into DB. Here's the code :

– FrèreToc
Jan 3 at 17:36







Happy New Year ;) I did try to fetch the entity back from DB, update it, and save, but it doesn't persist into DB. Here's the code :

– FrèreToc
Jan 3 at 17:36















Happy New Year ~ Then most probably it is due to you do not configure transaction property.At least , you have to @EnableTransactionManagement in your spring boot configuration and define the PlatformTransactionManager......

– Ken Chan
Jan 3 at 17:39





Happy New Year ~ Then most probably it is due to you do not configure transaction property.At least , you have to @EnableTransactionManagement in your spring boot configuration and define the PlatformTransactionManager......

– Ken Chan
Jan 3 at 17:39













Thanks Ken Chan for the time. Working now (edited the OQuestion). No need for configuring the transaction, it seems

– FrèreToc
Feb 12 at 10:04





Thanks Ken Chan for the time. Working now (edited the OQuestion). No need for configuring the transaction, it seems

– FrèreToc
Feb 12 at 10:04




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023687%2fspring-data-not-committing-to-db%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

ts Property 'filter' does not exist on type '{}'

mat-slide-toggle shouldn't change it's state when I click cancel in confirmation window