क्या एक संस्करणित इकाई उदाहरण को बदलना संभव है, और फ्लश का उपयोग किए बिना बढ़ने वाले संस्करण को प्राप्त करना संभव है? क्योंकि मैंने जो पढ़ा है, उससे डरते हुए फ्लश एक अच्छा अभ्यास नहीं है क्योंकि यह प्रदर्शन या यहां तक कि डेटा भ्रष्टाचार के लिए भी बुरा प्रभाव है? इम यकीन नहीं:विलय के बिना अद्यतन और अद्यतन संस्करण प्राप्त करें?
/*
Hibernate: select receivingg0_.id as id9_14_, receivingg0_.creationDate as creation2_9_14_, ... too long
the version before modification : 16
the version after modification : 16
after merge the modification, the version is : 16
Hibernate: update ReceivingGood set creationDate=?, modificationDate=?, usercreate_id=?, usermodify_id=?, ... too long
after flushing the modification, the version is finally : 17
*/
public void modifyHeaderAndGetUpdatedVersion() {
String id = "3b373f6a-9cd1-4c9c-9d46-240de37f6b0f";
ReceivingGood receivingGood = em.find(ReceivingGood.class, id);
System.out.println("the version before modification : " + receivingGood.getVersion());
receivingGood.setTransactionNumber("NUM001xyz");
System.out.println("the version after modification : " + receivingGood.getVersion());
receivingGood = em.merge(receivingGood);
System.out.println("after merge the modification, the version is : " + receivingGood.getVersion());
em.flush();
System.out.println("after flushing the modification, the version is finally : " + receivingGood.getVersion());
}
अपने परीक्षण में, संस्करण फ्लश के बाद वृद्धि की जाती गया: डी
यहाँ एक सरल कोड है और यह भी टिप्पणी के रूप में उत्पादन है। विलय ऑपरेशन से लौटाया गया उदाहरण में वृद्धि संस्करण नहीं है।
लेकिन मेरे मामले में, मैं इकाई को डीटीओ के रूप में अपनी वेबूई में वापस करना चाहता हूं, और इकाई को डीटीओ में परिवर्तित करने से पहले संस्करण को बाद में फ्लश/प्रतिबद्ध होना चाहिए और इसे यूआई में वापस करना चाहिए प्रस्तुत किया जाना चाहिए। और फिर यूआई का नवीनतम संस्करण हो सकता है, और अगले संस्करण के लिए इस संस्करण को पास कर देगा।
क्या कोई तरीका है जहां मैं फ्लश किए बिना नवीनतम संस्करण प्राप्त कर सकता हूं?
धन्यवाद!
अद्यतन
मेरे अनुभव में, इस मैन्युअल रूप से बढ़ाने समस्या हो सकती है के रूप में नीचे इस उदाहरण से देखा जा सकता। इस उदाहरण में, हमारे पास 2 फ्लश हैं।
पहला वाला डीबी कनेक्शन में परिवर्तन सिंक्रनाइज़ करना है, ताकि एक ही कनेक्शन से संग्रहीत प्रक्रिया कॉल इकाई प्रबंधक से किए गए परिवर्तन देख सके।
दूसरा फ्लश अंतिम संस्करण प्राप्त करने के लिए बुलाया जाता है। और हम देख सकते हैं कि यह दो बार बढ़ गया है। इसलिए फ्लैशिंग के बिना मैन्युअल वृद्धि से संस्करण प्राप्त करना इस स्थिति में काम नहीं करेगा, क्योंकि हमें वास्तव में गिनना है कि कितने फ्लश किए जा रहे हैं।
/*
Hibernate: select receivingg0_.id as id9_14_, receivingg0_.creationDate as creation2_9_14_, .. too long
the version before modification : 18
the version after modification : 18
after merge the modification, the version is : 18
now flushing the modification, so that the stored procedure call from the same connection can see the changes
Hibernate: update ReceivingGood set creationDate=?, modificationDate=?, usercreate_id=?, .. too long
after flushing the modification, the version is : 19
Hibernate: update ReceivingGood set creationDate=?, modificationDate=?, usercreate_id=?, .. too long
after the second flush, the version got increased again into : 20
*/
public void modifyHeaderAndGetUpdatedVersionWith2Flushes() {
String id = "3b373f6a-9cd1-4c9c-9d46-240de37f6b0f";
ReceivingGood receivingGood = em.find(ReceivingGood.class, id);
System.out.println("the version before modification : " + receivingGood.getVersion());
//auditEntity(receivingGood, getUser("3978fee3-9690-4377-84bd-9fb05928a6fc"));
receivingGood.setTransactionNumber("NUM001xyz");
System.out.println("the version after modification : " + receivingGood.getVersion());
receivingGood = em.merge(receivingGood);
System.out.println("after merge the modification, the version is : " + receivingGood.getVersion());
System.out.println("now flushing the modification, so that the stored procedure call from the same connection can see the changes");
em.flush();
System.out.println("after flushing the modification, the version is : " + receivingGood.getVersion());
receivingGood.setTransactionNumber("NUM001abc");
em.flush();
System.out.println("after the second flush, the version got increased again into : " + receivingGood.getVersion());
}
इसका मतलब मैं सच में अंत में फ्लश पर निर्भर करने के लिए संशोधित इकाई के लिए नवीनतम संस्करण प्राप्त करने है?
अद्यतन 2
यहाँ एक सेवा विधि है कि ReceivingGood इकाई अपडेट हो जाएगा और एक डीटीओ नवीनतम संस्करण है कि लौटना चाहिए का एक सरल उदाहरण है।
@Transactional
public ReceivingGoodDTO doSomethingInTheServiceAndReturnDTO() {
// do xxx ..
// do yyy ..
dto = update(entity);
return dto; // and the transaction commits here, but dto's version isnt increased because it's not a managed entity, just a plain POJO
}
@partenon: मैं समझता हूं कि कैसे काम करता है और काम करता है, और संस्करण उनके द्वारा कैसे बढ़ाया गया है। लेकिन फिर भी मैं आपके द्वारा अनुशंसित पुस्तक को पढ़ूंगा :-) वैसे भी, मैंने मैन्युअल वृद्धि के संबंध में कुछ अतिरिक्त चिंताओं के लिए अपनी मूल पोस्ट अपडेट की है। हमेशा की तरह धन्यवाद। – bertie
मैंने अभी आपकी नई चिंताओं को हल करने के लिए उत्तर अपडेट किया है :-) – jpkrohling
@partenon: तो, मैं अंत में निष्कर्ष निकाल सकता हूं कि, सक्रिय लेनदेन के अंदर एक डीटीओ बनाने के लिए जो नवीनतम संस्करण के साथ इकाई को प्रतिबिंबित करता है, मुझे वास्तव में करना है फ्लश()। धन्यवाद ! – bertie