रिमूव्ह() पद्धतीने काढत आहे

शेवटी, ऑब्जेक्ट हटवण्याकडे पाहू. तत्वतः, डेटाबेसमधून ऑब्जेक्ट्स हटविणे खूप सोपे आहे, परंतु ते म्हणतात त्याप्रमाणे, त्यात बारकावे आहेत. आणि अशा सहा बारकावे आहेत:

  • रिमूव्ह() पद्धतीने काढत आहे
  • कंपनीसाठी काढणे
  • अनाथ करून काढणे
  • JPQL सह हटवा
  • NativeQuery द्वारे हटवणे
  • softDeleted()

आणि आम्ही सर्वात स्पष्ट उपायाने सुरुवात करू - remove() पद्धतीला कॉल करणे .

User user = new User();
user.setName("Kolyan");
session.persist(user);  // add an object to the database
session.flush();
session.clear();  // close the session

user = (User) session.find(User.class, user.getId() ); //receive the object from the database
session.remove(user);
session.flush();
session.clear();  // close the session

//here the object is actually deleted.

फ्लश() पद्धत कॉल केल्यानंतर किंवा व्यवहार बंद केल्यानंतर डेटाबेसमधील वास्तविक ऑपरेशन केले जाईल .

कॅस्केडिंग हटवा

तुम्हाला आठवत असेल जेव्हा आम्ही SQL चा अभ्यास केला होता, तेव्हा अवलंबून टेबल्स कंस्ट्रायंटने लिहिता येतात. आणि त्यापैकी एक असा गेला:

CONSTRAINT ONDELETE REMOVE

त्याचा अर्थ असा होता की जर आपल्याकडे लहान घटकांचा समावेश असलेले सारणी असेल, तर जेव्हा पालक घटक वाटप केले जातात, तेव्हा त्याची सर्व मुले हटविली जाणे आवश्यक आहे.

समजा आम्ही वापरकर्त्याची वैयक्तिक माहिती कुठेतरी साठवून ठेवली आणि डेटाबेसमध्ये CONSTRAINT सेट केले जेणेकरून वापरकर्ता हटवला जाईल तेव्हा हा डेटा देखील हटवला जाईल. मग आपल्याला फक्त मूळ ऑब्जेक्ट हटवण्याची आवश्यकता आहे आणि सर्व चाइल्ड ऑब्जेक्ट्स बेस स्तरावर हटवले जातील:

User user = new User();
UserPrivateInfo info = new UserPrivateInfo();
user.setPrivateInfo(info);
session.persist(user);  //add the object to the database, the info object will also be saved to the database
session.flush();
session.clear();  // close the session

user = (User) session.find(User.class, user.getId() ); //receive the object from the database
session.remove(user);
session.flush();
session.clear();  // close the session

// here the user and info objects are actually removed from the database.

अनाथ करून काढणे

अनाथ काढणे नावाचा काढण्याचा आणखी एक प्रकार आहे. हे काहीसे मागील आवृत्तीसारखेच आहे. मूल अस्तित्व हटवले जाते जेव्हा त्याचे पालक घटकाशी नाते तुटते. या प्रकरणात, मूळ घटक सहसा हटविला जात नाही.

समजा आमच्याकडे एक वापरकर्ता आहे आणि त्याच्याकडे पोस्टची यादी आहे:

User user = new User();
UserMessage message = new UserMessage();
user.getMessageList().add(message);
session.persist(user);  //add the object to the database, the message object will also be saved to the database
session.flush();
session.clear();  // close the session

user = (User) session.find(User.class, user.getId() ); //receive the object from the database
UserMessage message2 = user.getMessageList().get(0); //get the user's message
user.getMessageList().remove(message2);  //remove the message from the list
session.flush();
session.clear();  // close the session

// here the message2 object is actually removed from the database

एक महत्त्वाची सूक्ष्मता देखील आहे, जर आम्हाला हायबरनेटने हे वर्तन अंमलात आणायचे असेल, तर भाष्य वापरून दोन घटकांना जोडताना ते स्पष्टपणे निर्दिष्ट केले पाहिजे:

@Entity
public class User {

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    private List<UserMessage> messageList = new ArrayList<UserMessage>();

}

JPQL द्वारे हटवा

ऑब्जेक्ट हटवण्याचा आणखी एक मनोरंजक मार्ग म्हणजे HQL (किंवा JPQL) क्वेरी लिहिणे. शेवटी executeUpdate() पद्धतीला कॉल करायला विसरू नका , अन्यथा हायबरनेट केवळ-वाचनीय व्यवहार तयार करेल आणि तुम्हाला कोणतेही हटवले जाणार नाही.

उदाहरण:

User user = new User();
session.persist(user);  // add an object to the database
session.flush();
session.clear();  // close the session

session.createQuery("delete from User where id = :id")
   .setParameter("id", user.getId())
   .executeUpdate();

डेटाबेस बदलल्याने अस्तित्वातील अस्तित्व वस्तू कोणत्याही प्रकारे बदलणार नाहीत.

NativeQuery द्वारे हटवणे

त्याचप्रमाणे, तुम्ही NativeQuery हटवू शकता आणि कॉल करू शकता.

उदाहरण:

User user = new User();
session.persist(user);  // add an object to the database
session.flush();
session.clear();  // close the session

session.createNativeQuery("DELETE FROM user WHERE id = :id")
   .setParameter("id", user.getId())
   .executeUpdate();

डेटाबेसमधील बदल कोणत्याही प्रकारे विद्यमान घटक वस्तूंवर परिणाम करणार नाही.

मऊ हटवा

कधीकधी, डेटाबेसमधील डेटा हटविण्याऐवजी, तो फक्त हटविला म्हणून चिन्हांकित करणे सोयीचे असते. असा डेटा नंतर विविध परिस्थितींमध्ये सहभागी होऊ शकतो. प्रथम, असे हटविणे सहजपणे उलट करता येते - रेषा पुन्हा थेट म्हणून चिन्हांकित केल्या जाऊ शकतात.

दुसरे म्हणजे, अशा रिमोट डेटाचे "संग्रहण" करणे उपयुक्त आहे, कारण अशी प्रकरणे आहेत जेव्हा सर्व्हरचे वर्तन कायद्याद्वारे नियंत्रित केले जाते आणि यासारखे. तथापि, जर तुम्ही तुमचा डेटा हटवला म्हणून चिन्हांकित केले, तरच तुम्हाला ते हटवले गेले आहे हे समजेल. हायबरनेट अजूनही हा डेटा शोधेल आणि क्रमवारी लावताना देखील वापरेल.

म्हणून, हायबरनेटचे निर्माते एक विशेष भाष्य घेऊन आले ज्याद्वारे वस्तू जिवंत म्हणून चिन्हांकित करणे शक्य होईल. उदाहरण:

@Entity
@Where(clause = "DELETED = 0") //in all WHEREs "AND DELETED = 0" will be added
public class User {
	// mapping fields

	@Column(name = "DELETED") // if the value in the DELETED column == 0, then the record is alive, if 1 - dead
	private Integer deleted = 0;

	//getters and setters

    public void softDeleted() {
    	this.deleted = 1; //mark the post as dead
    }
}

एखादी वस्तू हटवली म्हणून चिन्हांकित करण्यासाठी, तुम्हाला त्यावर फक्त softDeleted() पद्धत कॉल करणे आवश्यक आहे :

User user = new User();
session.persist(user);  // add an object to the database
session.flush();
session.clear();  // close the session

user = (User) session.find(User.class, user.getId() ); //receive the object from the database
user.softDeleted(); // mark the object as deleted
session.flush();
session.clear();  // close the session

//this object will no longer reside via Hibernate