Does JPA/Hibernate save even when not calling persist












8















em.getTransaction().begin();

StringData sd = em.find(StringData.class, key);
System.out.println("Old value: " + sd.getData());
sd.setData(newValue);
// em.persist(sd);

em.getTransaction().commit();


As you can see, I'm not calling persist, it's commented out, because I'm dry running this code first. However, as it turns out it's not so very dry. Upon inspecting the database, I see the data is changed (fortunately it's a test database).



Apparently my understanding of Hibernate/JPA is flawed. Isn't calling persist always required to change data? And if not, what are the rules on when something is saved?










share|improve this question























  • but you are calling commit?

    – Sergey Benner
    Feb 8 '12 at 16:20
















8















em.getTransaction().begin();

StringData sd = em.find(StringData.class, key);
System.out.println("Old value: " + sd.getData());
sd.setData(newValue);
// em.persist(sd);

em.getTransaction().commit();


As you can see, I'm not calling persist, it's commented out, because I'm dry running this code first. However, as it turns out it's not so very dry. Upon inspecting the database, I see the data is changed (fortunately it's a test database).



Apparently my understanding of Hibernate/JPA is flawed. Isn't calling persist always required to change data? And if not, what are the rules on when something is saved?










share|improve this question























  • but you are calling commit?

    – Sergey Benner
    Feb 8 '12 at 16:20














8












8








8








em.getTransaction().begin();

StringData sd = em.find(StringData.class, key);
System.out.println("Old value: " + sd.getData());
sd.setData(newValue);
// em.persist(sd);

em.getTransaction().commit();


As you can see, I'm not calling persist, it's commented out, because I'm dry running this code first. However, as it turns out it's not so very dry. Upon inspecting the database, I see the data is changed (fortunately it's a test database).



Apparently my understanding of Hibernate/JPA is flawed. Isn't calling persist always required to change data? And if not, what are the rules on when something is saved?










share|improve this question














em.getTransaction().begin();

StringData sd = em.find(StringData.class, key);
System.out.println("Old value: " + sd.getData());
sd.setData(newValue);
// em.persist(sd);

em.getTransaction().commit();


As you can see, I'm not calling persist, it's commented out, because I'm dry running this code first. However, as it turns out it's not so very dry. Upon inspecting the database, I see the data is changed (fortunately it's a test database).



Apparently my understanding of Hibernate/JPA is flawed. Isn't calling persist always required to change data? And if not, what are the rules on when something is saved?







java hibernate jpa






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 8 '12 at 16:15









Bart van HeukelomBart van Heukelom

20.8k47154270




20.8k47154270













  • but you are calling commit?

    – Sergey Benner
    Feb 8 '12 at 16:20



















  • but you are calling commit?

    – Sergey Benner
    Feb 8 '12 at 16:20

















but you are calling commit?

– Sergey Benner
Feb 8 '12 at 16:20





but you are calling commit?

– Sergey Benner
Feb 8 '12 at 16:20












2 Answers
2






active

oldest

votes


















10














Yes, managed entities are saved when a flush (flush are also done with a commit) is done if any change is detected, it's called dirty checking.






share|improve this answer
























  • Good to know. Can that be relied upon, or is a persist call still wise?

    – Bart van Heukelom
    Feb 8 '12 at 16:25











  • @Bart Only managed entities are saved this way, managed means that the entity manager used to load those entities is not closed yet, so be careful. And persist works to create a new entity. If you use it in an already existing entity (like here) it will throw an exception. Take a look at the API: link

    – Pablo
    Feb 8 '12 at 16:32













  • But I use persist on loaded entities all the time, without exceptions.

    – Bart van Heukelom
    Feb 8 '12 at 16:43











  • @Bart Strange, in the API for persist: "EntityExistsException - if the entity already exists. (If the entity already exists, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.) "

    – Pablo
    Feb 8 '12 at 16:47








  • 1





    @Bart OK, it seems that the API is not very accurate, look at the accepted answer: link

    – Pablo
    Feb 8 '12 at 16:53





















3














StringData sd = em.find(StringData.class, key);


That line of code retrieves the StringData instance sd from the em session, any changes you make will be saved on flush (when transactions ends) because the object instance is associated with the em session (ie managed).



You could detach it, or return it from the method. Outside of the transaction it is not associated with em session and changes will not be persisted until it is re-attached via merge.






share|improve this answer
























  • The important part is not that the transaction is commited, the important part is that the entity manager is closed. If the transaction is commited but the entity manager is not closed, the entity is still associated with the entity manager.

    – Pablo
    Feb 8 '12 at 16:38











  • @Pablo So what happens if the entity is changed outside the transaction, but within the entity manager?

    – Bart van Heukelom
    Feb 8 '12 at 17:28











  • @Bart The entity manager will try to save it the next time that a flush() or commit() is called.

    – Pablo
    Feb 8 '12 at 17:50











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%2f9197160%2fdoes-jpa-hibernate-save-even-when-not-calling-persist%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









10














Yes, managed entities are saved when a flush (flush are also done with a commit) is done if any change is detected, it's called dirty checking.






share|improve this answer
























  • Good to know. Can that be relied upon, or is a persist call still wise?

    – Bart van Heukelom
    Feb 8 '12 at 16:25











  • @Bart Only managed entities are saved this way, managed means that the entity manager used to load those entities is not closed yet, so be careful. And persist works to create a new entity. If you use it in an already existing entity (like here) it will throw an exception. Take a look at the API: link

    – Pablo
    Feb 8 '12 at 16:32













  • But I use persist on loaded entities all the time, without exceptions.

    – Bart van Heukelom
    Feb 8 '12 at 16:43











  • @Bart Strange, in the API for persist: "EntityExistsException - if the entity already exists. (If the entity already exists, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.) "

    – Pablo
    Feb 8 '12 at 16:47








  • 1





    @Bart OK, it seems that the API is not very accurate, look at the accepted answer: link

    – Pablo
    Feb 8 '12 at 16:53


















10














Yes, managed entities are saved when a flush (flush are also done with a commit) is done if any change is detected, it's called dirty checking.






share|improve this answer
























  • Good to know. Can that be relied upon, or is a persist call still wise?

    – Bart van Heukelom
    Feb 8 '12 at 16:25











  • @Bart Only managed entities are saved this way, managed means that the entity manager used to load those entities is not closed yet, so be careful. And persist works to create a new entity. If you use it in an already existing entity (like here) it will throw an exception. Take a look at the API: link

    – Pablo
    Feb 8 '12 at 16:32













  • But I use persist on loaded entities all the time, without exceptions.

    – Bart van Heukelom
    Feb 8 '12 at 16:43











  • @Bart Strange, in the API for persist: "EntityExistsException - if the entity already exists. (If the entity already exists, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.) "

    – Pablo
    Feb 8 '12 at 16:47








  • 1





    @Bart OK, it seems that the API is not very accurate, look at the accepted answer: link

    – Pablo
    Feb 8 '12 at 16:53
















10












10








10







Yes, managed entities are saved when a flush (flush are also done with a commit) is done if any change is detected, it's called dirty checking.






share|improve this answer













Yes, managed entities are saved when a flush (flush are also done with a commit) is done if any change is detected, it's called dirty checking.







share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 8 '12 at 16:19









PabloPablo

3,39622443




3,39622443













  • Good to know. Can that be relied upon, or is a persist call still wise?

    – Bart van Heukelom
    Feb 8 '12 at 16:25











  • @Bart Only managed entities are saved this way, managed means that the entity manager used to load those entities is not closed yet, so be careful. And persist works to create a new entity. If you use it in an already existing entity (like here) it will throw an exception. Take a look at the API: link

    – Pablo
    Feb 8 '12 at 16:32













  • But I use persist on loaded entities all the time, without exceptions.

    – Bart van Heukelom
    Feb 8 '12 at 16:43











  • @Bart Strange, in the API for persist: "EntityExistsException - if the entity already exists. (If the entity already exists, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.) "

    – Pablo
    Feb 8 '12 at 16:47








  • 1





    @Bart OK, it seems that the API is not very accurate, look at the accepted answer: link

    – Pablo
    Feb 8 '12 at 16:53





















  • Good to know. Can that be relied upon, or is a persist call still wise?

    – Bart van Heukelom
    Feb 8 '12 at 16:25











  • @Bart Only managed entities are saved this way, managed means that the entity manager used to load those entities is not closed yet, so be careful. And persist works to create a new entity. If you use it in an already existing entity (like here) it will throw an exception. Take a look at the API: link

    – Pablo
    Feb 8 '12 at 16:32













  • But I use persist on loaded entities all the time, without exceptions.

    – Bart van Heukelom
    Feb 8 '12 at 16:43











  • @Bart Strange, in the API for persist: "EntityExistsException - if the entity already exists. (If the entity already exists, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.) "

    – Pablo
    Feb 8 '12 at 16:47








  • 1





    @Bart OK, it seems that the API is not very accurate, look at the accepted answer: link

    – Pablo
    Feb 8 '12 at 16:53



















Good to know. Can that be relied upon, or is a persist call still wise?

– Bart van Heukelom
Feb 8 '12 at 16:25





Good to know. Can that be relied upon, or is a persist call still wise?

– Bart van Heukelom
Feb 8 '12 at 16:25













@Bart Only managed entities are saved this way, managed means that the entity manager used to load those entities is not closed yet, so be careful. And persist works to create a new entity. If you use it in an already existing entity (like here) it will throw an exception. Take a look at the API: link

– Pablo
Feb 8 '12 at 16:32







@Bart Only managed entities are saved this way, managed means that the entity manager used to load those entities is not closed yet, so be careful. And persist works to create a new entity. If you use it in an already existing entity (like here) it will throw an exception. Take a look at the API: link

– Pablo
Feb 8 '12 at 16:32















But I use persist on loaded entities all the time, without exceptions.

– Bart van Heukelom
Feb 8 '12 at 16:43





But I use persist on loaded entities all the time, without exceptions.

– Bart van Heukelom
Feb 8 '12 at 16:43













@Bart Strange, in the API for persist: "EntityExistsException - if the entity already exists. (If the entity already exists, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.) "

– Pablo
Feb 8 '12 at 16:47







@Bart Strange, in the API for persist: "EntityExistsException - if the entity already exists. (If the entity already exists, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.) "

– Pablo
Feb 8 '12 at 16:47






1




1





@Bart OK, it seems that the API is not very accurate, look at the accepted answer: link

– Pablo
Feb 8 '12 at 16:53







@Bart OK, it seems that the API is not very accurate, look at the accepted answer: link

– Pablo
Feb 8 '12 at 16:53















3














StringData sd = em.find(StringData.class, key);


That line of code retrieves the StringData instance sd from the em session, any changes you make will be saved on flush (when transactions ends) because the object instance is associated with the em session (ie managed).



You could detach it, or return it from the method. Outside of the transaction it is not associated with em session and changes will not be persisted until it is re-attached via merge.






share|improve this answer
























  • The important part is not that the transaction is commited, the important part is that the entity manager is closed. If the transaction is commited but the entity manager is not closed, the entity is still associated with the entity manager.

    – Pablo
    Feb 8 '12 at 16:38











  • @Pablo So what happens if the entity is changed outside the transaction, but within the entity manager?

    – Bart van Heukelom
    Feb 8 '12 at 17:28











  • @Bart The entity manager will try to save it the next time that a flush() or commit() is called.

    – Pablo
    Feb 8 '12 at 17:50
















3














StringData sd = em.find(StringData.class, key);


That line of code retrieves the StringData instance sd from the em session, any changes you make will be saved on flush (when transactions ends) because the object instance is associated with the em session (ie managed).



You could detach it, or return it from the method. Outside of the transaction it is not associated with em session and changes will not be persisted until it is re-attached via merge.






share|improve this answer
























  • The important part is not that the transaction is commited, the important part is that the entity manager is closed. If the transaction is commited but the entity manager is not closed, the entity is still associated with the entity manager.

    – Pablo
    Feb 8 '12 at 16:38











  • @Pablo So what happens if the entity is changed outside the transaction, but within the entity manager?

    – Bart van Heukelom
    Feb 8 '12 at 17:28











  • @Bart The entity manager will try to save it the next time that a flush() or commit() is called.

    – Pablo
    Feb 8 '12 at 17:50














3












3








3







StringData sd = em.find(StringData.class, key);


That line of code retrieves the StringData instance sd from the em session, any changes you make will be saved on flush (when transactions ends) because the object instance is associated with the em session (ie managed).



You could detach it, or return it from the method. Outside of the transaction it is not associated with em session and changes will not be persisted until it is re-attached via merge.






share|improve this answer













StringData sd = em.find(StringData.class, key);


That line of code retrieves the StringData instance sd from the em session, any changes you make will be saved on flush (when transactions ends) because the object instance is associated with the em session (ie managed).



You could detach it, or return it from the method. Outside of the transaction it is not associated with em session and changes will not be persisted until it is re-attached via merge.







share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 8 '12 at 16:26









NimChimpskyNimChimpsky

33.2k46168276




33.2k46168276













  • The important part is not that the transaction is commited, the important part is that the entity manager is closed. If the transaction is commited but the entity manager is not closed, the entity is still associated with the entity manager.

    – Pablo
    Feb 8 '12 at 16:38











  • @Pablo So what happens if the entity is changed outside the transaction, but within the entity manager?

    – Bart van Heukelom
    Feb 8 '12 at 17:28











  • @Bart The entity manager will try to save it the next time that a flush() or commit() is called.

    – Pablo
    Feb 8 '12 at 17:50



















  • The important part is not that the transaction is commited, the important part is that the entity manager is closed. If the transaction is commited but the entity manager is not closed, the entity is still associated with the entity manager.

    – Pablo
    Feb 8 '12 at 16:38











  • @Pablo So what happens if the entity is changed outside the transaction, but within the entity manager?

    – Bart van Heukelom
    Feb 8 '12 at 17:28











  • @Bart The entity manager will try to save it the next time that a flush() or commit() is called.

    – Pablo
    Feb 8 '12 at 17:50

















The important part is not that the transaction is commited, the important part is that the entity manager is closed. If the transaction is commited but the entity manager is not closed, the entity is still associated with the entity manager.

– Pablo
Feb 8 '12 at 16:38





The important part is not that the transaction is commited, the important part is that the entity manager is closed. If the transaction is commited but the entity manager is not closed, the entity is still associated with the entity manager.

– Pablo
Feb 8 '12 at 16:38













@Pablo So what happens if the entity is changed outside the transaction, but within the entity manager?

– Bart van Heukelom
Feb 8 '12 at 17:28





@Pablo So what happens if the entity is changed outside the transaction, but within the entity manager?

– Bart van Heukelom
Feb 8 '12 at 17:28













@Bart The entity manager will try to save it the next time that a flush() or commit() is called.

– Pablo
Feb 8 '12 at 17:50





@Bart The entity manager will try to save it the next time that a flush() or commit() is called.

– Pablo
Feb 8 '12 at 17:50


















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%2f9197160%2fdoes-jpa-hibernate-save-even-when-not-calling-persist%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

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga