Persistent collections by R4R Team

Hibernate need the persistent collection of valued fields which declared as an interface type. We can take a simple example to understand this:

public class WareHouse (any class name will be declared)
    {
        private String serialNumber;
        private Set parts = new HashSet();
        public Set getParts() { return parts; }
        void setParts(Set parts) { this.parts = parts; }
        public String getSerialNumber() { return serialNumber; }
        void setSerialNumber(String serialnumber) { serialNumber = serialnumber;}
    }

For this we need the java.util.Set interface, java.util.Collection, java.util.List, java.util.Map, java.util.SortedSet, java.util.SortedMap or we can implement of org.hibernate.usertype.UserCollectionType.

We have to know that which instance we intialized that mean how that instance is initilized with an instance of HashSet. In other word we can say that it would be the best way for initilizing the collection valued properties of newly instantiated (non-persistent) instances. When we use the persist() method for the persistend, Hibernate actually replace the HashSet with an instance of Hibernate's own implementation of Set. We always know the some base error which are given below:

Cat cat = new DomesticCat();
Cat kitten = new DomesticCat();
.....
Set kittens = new HashSet();
kittens.add(kitten);
cat.setKittens(kittens);
session.persist(cat);
kittens = cat.getKittens(); //Here kittens collection is a Set
(HashSet) cat.getKittens(); // Error!

In hibernate persistent collection injected by some points like HashMap, HashSet, TreeMap, TreeSet or ArrayList, depending on the interface type.

We sees that the Collection instance have the usual behavior of value types. They are automatically persisted when referenced by the persistent object and are automatically deleted when unfreferenced. If a collection is passed from one persistent object to another, its elements might be moved from one table to another. Two entities cannot share a reference to the same collection instance. Due to the underlying relational model, collection-valued properties do not support null value semantics. Hibernate does not distinguish between a null collection reference and an empty collection.
Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!