Saving Collections in Hibernate with Example

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;"><b style&equals;"color&colon; &num;351c75&semi; font-weight&colon; normal&semi;">Now we’ll learn about the Saving Collections in Hibernate with Example&period;<&sol;b><&sol;p>&NewLine;<div style&equals;"color&colon; &num;073763&semi;">In the <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;value-types-and-embedding-objects&period;html">previous tutorial<&sol;a>&comma; we learned about Embedding value type object&period;<&sol;p>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<&sol;div>&NewLine;<div style&equals;"color&colon; &num;073763&semi;">Now what will be doing when there are a lot of embeddable objects instead of the single embeddable objects&comma; In previous we have used two embeddable objects in the <b>UserDetails <&sol;b>class&period;<&sol;div>&NewLine;<div style&equals;"color&colon; &num;073763&semi;"><&sol;div>&NewLine;<div style&equals;"color&colon; &num;073763&semi;">Suppose we want to keep tracks all addresses of a user&comma; so think about one minute how to get&period;<&sol;div>&NewLine;<p>Lets we will use the Collection for that&period;<&sol;p>&NewLine;<p>1&period; In this code snip&comma; User has one address<&sol;p>&NewLine;<div style&equals;"color&colon; blue&semi;">class UserDetails&lbrace;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    &&num;8212&semi;&&num;8211&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    private int userId&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    &&num;8212&semi;&&num;8211&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    private Address address&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    &&num;8212&semi;&&num;8211&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">&rcub;<&sol;div>&NewLine;<p>2&period; In this code snip User has two addresses<&sol;p>&NewLine;<div style&equals;"color&colon; blue&semi;">class UserDetails&lbrace;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    &&num;8212&semi;&&num;8211&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    private int userId&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    &&num;8212&semi;&&num;8211&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    private Address address1&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    private Address address2&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    &&num;8212&semi;&&num;8211&semi;<&sol;div>&NewLine;<p>&rcub;<&sol;p>&NewLine;<p>3&period; In this code snip User has more than two addresses<&sol;p>&NewLine;<div style&equals;"color&colon; blue&semi;">class UserDetails&lbrace;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    &&num;8212&semi;&&num;8211&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    private int userId&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    &&num;8212&semi;&&num;8211&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    private Address address1&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    private Address address2&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    &&num;8212&semi;&&num;8211&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    private Address addressN&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    &&num;8212&semi;&&num;8211&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">&rcub;<&sol;div>&NewLine;<p>In the third case how to manage the records about addresses for that user&comma; now for managing this type of problems we have to use the collection&comma; we do not need to care about how many of the addresses user has&period; Let&&num;8217&semi;s see there is the same change in the code for that&&num;8230&semi;<&sol;p>&NewLine;<div style&equals;"color&colon; blue&semi;">class UserDetails&lbrace;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    &&num;8212&semi;&&num;8211&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    private int userId&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    &&num;8212&semi;&&num;8211&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    private List&lt&semi;Address&gt&semi; addresses &equals; new ArrayList&lt&semi;Address&gt&semi;&lpar;&rpar;&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">    &&num;8212&semi;&&num;8211&semi;<&sol;div>&NewLine;<div style&equals;"color&colon; blue&semi;">&rcub;<&sol;div>&NewLine;<div style&equals;"color&colon; &num;20124d&semi;">Hibernate provides the facility to persist the collections&period; A collection can be a list&comma; set&comma; map&comma; collection&comma; sorted set&comma; sorted map&period; java&period;util&period;List&comma; java&period;util&period;Set&comma; java&period;util&period;Collection&comma; java&period;util&period;SortedSet&comma; java&period;util&period;SortedMap etc&period; are the real interface types to declared the persistent collection-valued fields&period; Hibernate injects persistent collections based on the type of interface&period; The collection instances usually behave likes the types of value behaviour&period; Instances of collections are auto persisted if a persistent object refers it and are deleted automatically if it is not referred through&period; Elements of collection may shift from one table to another when a persistent object passed the collection to another persistent object&period;<&sol;div>&NewLine;<p>We look following example of <b>Object of Entity Type&colon;<&sol;b><b style&equals;"color&colon; &num;351c75&semi; font-weight&colon; normal&semi;"> <&sol;b><br &sol;>&NewLine;<b style&equals;"color&colon; &num;351c75&semi; font-weight&colon; normal&semi;"><br &sol;>&NewLine;<&sol;b><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi; border-width&colon; thin&semi; border&colon; solid&semi;">&NewLine;<div style&equals;"color&colon; &num;274e13&semi;"><b>&commat;Entity<&sol;b><&sol;div>&NewLine;<div style&equals;"color&colon; &num;274e13&semi;"><b>&commat;Table&lpar;name&equals;&&num;8221&semi;TBL&lowbar;USER&lowbar;DETAILS<&sol;b><b>&&num;8220&semi;&rpar; <&sol;b><&sol;div>&NewLine;<p>public class UserDetails&lbrace;<br &sol;>&NewLine;<b style&equals;"color&colon; &num;274e13&semi;">&commat;Id<&sol;b><&sol;p>&NewLine;<div style&equals;"color&colon; &num;274e13&semi;"><b>     &commat;Column&lpar;name&equals;&&num;8221&semi;USER&lowbar;ID&&num;8221&semi;&comma; type&equals;&&num;8221&semi;INTEGER&&num;8221&semi;&rpar;<&sol;b><br &sol;>&NewLine;<b>     &commat;GeneratedValue&lpar;strategy&equals;GenerationType&period;AUTO&rpar; <&sol;b><&sol;div>&NewLine;<p>private long userId&semi;<br &sol;>&NewLine;<b style&equals;"color&colon; &num;274e13&semi;">&commat;Column&lpar;name&equals;&&num;8221&semi;USER&lowbar;NAME&&num;8221&semi;&comma; type&equals;&&num;8221&semi;String&&num;8221&semi;&rpar;<&sol;b><br &sol;>&NewLine;private String userName&semi;<br &sol;>&NewLine;<b style&equals;"color&colon; &num;274e13&semi;">&commat;ElementCollection<br &sol;>&NewLine;<&sol;b>private Collection&lt&semi;Address&gt&semi; lisOfAddresses &equals; new ArrayList&lt&semi;Address&gt&semi;&lpar;&rpar;&semi;<br &sol;>&NewLine;public Collection&lt&semi;Address&gt&semi; getLisOfAddresses&lpar;&rpar; &lbrace;<br &sol;>&NewLine;return lisOfAddresses&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public void setLisOfAddresses&lpar;Collection&lt&semi;Address&gt&semi; lisOfAddresses&rpar; &lbrace;<br &sol;>&NewLine;this&period;lisOfAddresses &equals; lisOfAddresses&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public int getUserId&lpar;&rpar; &lbrace;<br &sol;>&NewLine;return userId&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public void setUserId&lpar;int userId&rpar; &lbrace;<br &sol;>&NewLine;this&period;userId &equals; userId&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public String getUserName&lpar;&rpar; &lbrace;<br &sol;>&NewLine;return userName&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public void setUserName&lpar;String userName&rpar; &lbrace;<br &sol;>&NewLine;this&period;userName &equals; userName&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public String toString&lpar;&rpar;<br &sol;>&NewLine;&lbrace;<br &sol;>&NewLine;return &&num;8220&semi;&lbrack;User Name&colon; &&num;8220&semi;&plus;userName&plus;&&num;8221&semi;n Office Address&colon; &&num;8220&semi;&plus;lisOfAddresses&plus;&&num;8221&semi;&rsqb;&&num;8221&semi;&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;&rcub;<&sol;p>&NewLine;<&sol;div>&NewLine;<p><b style&equals;"color&colon; &num;274e13&semi;">&commat;ElementCollection&colon;<&sol;b><br &sol;>&NewLine;<b>Target&colon;<&sol;b><br &sol;>&NewLine;Fields &lpar;including property get methods&rpar;Defines a collection of instances of a basic type or embeddable class&period; Must be specified if the collection is to be mapped by means of a collection table&period;<br &sol;>&NewLine;<b>Address&period;java<&sol;b><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi; border-width&colon; thin&semi; border&colon; solid&semi;">package com&period;sdnext&period;hibernate&period;tutorial&period;dto&semi;<&sol;p>&NewLine;<p>import javax&period;persistence&period;Column&semi;<br &sol;>&NewLine;import javax&period;persistence&period;Embeddable&semi;<&sol;p>&NewLine;<p><b>&commat;Embeddable<&sol;b> &sol;&sol;for value object it is not is an entity object&period; Value object means does not have real meaning for self individually&period;<br &sol;>&NewLine;public class Address<br &sol;>&NewLine;&lbrace;<br &sol;>&NewLine;<b>&commat;Column&lpar;name&equals;&&num;8221&semi;STREET&lowbar;NAME&&num;8221&semi;&rpar;<&sol;b><br &sol;>&NewLine;private String street&semi;<br &sol;>&NewLine;<b>&commat;Column&lpar;name&equals;&&num;8221&semi;CITY&lowbar;NAME&&num;8221&semi;&rpar;<&sol;b><br &sol;>&NewLine;private String city&semi;<br &sol;>&NewLine;<b> &commat;Column&lpar;name&equals;&&num;8221&semi;STATE&lowbar;NAME&&num;8221&semi;&rpar;<&sol;b><br &sol;>&NewLine;private String state&semi;<br &sol;>&NewLine;<b>&commat;Column&lpar;name&equals;&&num;8221&semi;PIN&lowbar;CODE&&num;8221&semi;&rpar;<&sol;b><br &sol;>&NewLine;private String pincode&semi;<&sol;p>&NewLine;<p>public String getStreet&lpar;&rpar; &lbrace;<br &sol;>&NewLine;return street&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public void setStreet&lpar;String street&rpar; &lbrace;<br &sol;>&NewLine;this&period;street &equals; street&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public String getCity&lpar;&rpar; &lbrace;<br &sol;>&NewLine;return city&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public void setCity&lpar;String city&rpar; &lbrace;<br &sol;>&NewLine;this&period;city &equals; city&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public String getState&lpar;&rpar; &lbrace;<br &sol;>&NewLine;return state&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public void setState&lpar;String state&rpar; &lbrace;<br &sol;>&NewLine;this&period;state &equals; state&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public String getPincode&lpar;&rpar; &lbrace;<br &sol;>&NewLine;return pincode&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public void setPincode&lpar;String pincode&rpar; &lbrace;<br &sol;>&NewLine;this&period;pincode &equals; pincode&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public String toString&lpar;&rpar;<br &sol;>&NewLine;&lbrace;<br &sol;>&NewLine;return &&num;8221&semi; &lbrace;Street&colon; &&num;8220&semi;&plus;street&plus;&&num;8221&semi; City&colon; &&num;8220&semi;&plus;city&plus;&&num;8221&semi; State&colon; &&num;8220&semi;&plus;state&plus;&&num;8221&semi; Pincode&colon; &&num;8220&semi;&plus;pincode&plus;&&num;8221&semi; &rcub;&&num;8221&semi;&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;&rcub;<&sol;p>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<p><b>hibernate&period;cfg&period;xml <&sol;b>will be the same as the previous chapter&period;<b> <&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b><br &sol;>&NewLine;<b>HibernateTestDemo&period;java<&sol;b><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi; border-width&colon; thin&semi; border&colon; solid&semi;">package com&period;sdnext&period;hibernate&period;tutorial&semi;<&sol;p>&NewLine;<p>import org&period;hibernate&period;Session&semi;<br &sol;>&NewLine;import org&period;hibernate&period;SessionFactory&semi;<br &sol;>&NewLine;import org&period;hibernate&period;cfg&period;AnnotationConfiguration&semi;<&sol;p>&NewLine;<p>import com&period;sdnext&period;hibernate&period;tutorial&period;dto&period;Address&semi;<br &sol;>&NewLine;import com&period;sdnext&period;hibernate&period;tutorial&period;dto&period;UserDetails&semi;<&sol;p>&NewLine;<p>public class HibernateTestDemo &lbrace;<&sol;p>&NewLine;<p>&sol;&ast;&ast;<br &sol;>&NewLine;&ast; &commat;param args<br &sol;>&NewLine;&ast;&sol;<br &sol;>&NewLine;public static void main&lpar;String&lbrack;&rsqb; args&rpar;<br &sol;>&NewLine;&lbrace;<br &sol;>&NewLine;UserDetails user &equals; new UserDetails&lpar;&rpar;&semi;&sol;&sol;Create user object<br &sol;>&NewLine;user&period;setUserName&lpar;&&num;8220&semi;Dinesh Rajput&&num;8221&semi;&rpar;&semi; &sol;&sol;Set user name<&sol;p>&NewLine;<p>Address address1 &equals; new Address&lpar;&rpar;&semi; &sol;&sol; create first embedded object address<br &sol;>&NewLine;address1&period;setStreet&lpar;&&num;8220&semi;First Street&&num;8221&semi;&rpar;&semi;<br &sol;>&NewLine;address1&period;setCity&lpar;&&num;8220&semi;First City&&num;8221&semi;&rpar;&semi;<br &sol;>&NewLine;address1&period;setState&lpar;&&num;8220&semi;First State&&num;8221&semi;&rpar;&semi;<br &sol;>&NewLine;address1&period;setPincode&lpar;&&num;8220&semi;First Pin&&num;8221&semi;&rpar;&semi;<&sol;p>&NewLine;<p>Address address2 &equals; new Address&lpar;&rpar;&semi; &sol;&sol; create second embedded object address<br &sol;>&NewLine;address2&period;setStreet&lpar;&&num;8220&semi;Second Street&&num;8221&semi;&rpar;&semi;<br &sol;>&NewLine;address2&period;setCity&lpar;&&num;8220&semi;Second City&&num;8221&semi;&rpar;&semi;<br &sol;>&NewLine;address2&period;setState&lpar;&&num;8220&semi;Second State&&num;8221&semi;&rpar;&semi;<br &sol;>&NewLine;address2&period;setPincode&lpar;&&num;8220&semi;Second Pin&&num;8221&semi;&rpar;&semi;<br &sol;>&NewLine;&sol;&sol;adding addresses object to the list of address<br &sol;>&NewLine;user&period;getLisOfAddresses&lpar;&rpar;&period;add&lpar;address1&rpar;&semi;<br &sol;>&NewLine;user&period;getLisOfAddresses&lpar;&rpar;&period;add&lpar;address2&rpar;&semi;<&sol;p>&NewLine;<p>SessionFactory sessionFactory &equals; new AnnotationConfiguration&lpar;&rpar;&period;configure&lpar;&rpar;&period;buildSessionFactory&lpar;&rpar;&semi; &sol;&sol;create session factory object<br &sol;>&NewLine;Session session &equals; sessionFactory&period;openSession&lpar;&rpar;&semi; &sol;&sol;create session object from the session factory<br &sol;>&NewLine;session&period;beginTransaction&lpar;&rpar;&semi; &sol;&sol;initialize the transaction object from session<br &sol;>&NewLine;session&period;save&lpar;user&rpar;&semi; &sol;&sol; save the user<br &sol;>&NewLine;session&period;getTransaction&lpar;&rpar;&period;commit&lpar;&rpar;&semi; &sol;&sol;commit the transaction<br &sol;>&NewLine;session&period;close&lpar;&rpar;&semi; &sol;&sol;closing session<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;&rcub;<&sol;p>&NewLine;<p><b>Output&colon;<&sol;b><br &sol;>&NewLine;log4j&colon;WARN No appenders could be found for logger &lpar;org&period;hibernate&period;cfg&period;annotations&period;Version&rpar;&period;<br &sol;>&NewLine;log4j&colon;WARN Please initialize the log4j system properly&period;<br &sol;>&NewLine;Hibernate&colon; insert into TBL&lowbar;USER&lowbar;DETAILS &lpar;USER&lowbar;NAME&rpar; values &lpar;&quest;&rpar;<br &sol;>&NewLine;Hibernate&colon; insert into TBL&lowbar;USER&lowbar;DETAILS&lowbar;lisOfAddresses &lpar;TBL&lowbar;USER&lowbar;DETAILS&lowbar;USER&lowbar;ID&comma; CITY&lowbar;NAME&comma; PIN&lowbar;CODE&comma; STATE&lowbar;NAME&comma; STREET&lowbar;NAME&rpar; values &lpar;&quest;&comma; &quest;&comma; &quest;&comma; &quest;&comma; &quest;&rpar;<br &sol;>&NewLine;Hibernate&colon; insert into TBL&lowbar;USER&lowbar;DETAILS&lowbar;lisOfAddresses &lpar;TBL&lowbar;USER&lowbar;DETAILS&lowbar;USER&lowbar;ID&comma; CITY&lowbar;NAME&comma; PIN&lowbar;CODE&comma; STATE&lowbar;NAME&comma; STREET&lowbar;NAME&rpar; values &lpar;&quest;&comma; &quest;&comma; &quest;&comma; &quest;&comma; &quest;&rpar;<&sol;p>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;outputAddress&period;jpg" width&equals;"640" height&equals;"74" border&equals;"0" &sol;><&sol;div>&NewLine;<div style&equals;"color&colon; &num;0c343d&semi;">Look carefully to the output line&comma; there are two tables here in the database&comma; first for the UserDetails entity &&num;8220&semi;<b>TBL&lowbar;USER&lowbar;DETAILS<&sol;b>&&num;8221&semi; and second for the embeddable object Address name is &&num;8220&semi;<b>TBL&lowbar;USER&lowbar;DETAILS&lowbar;lisOfAddresses <&sol;b>&&num;8221&semi; &lpar;this is default name of table for address Entity Table Name&lowbar;field name of the list of the embeddable object in the entity class&rpar;&period;<&sol;div>&NewLine;<div style&equals;"color&colon; &num;0c343d&semi;"><&sol;div>&NewLine;<div style&equals;"color&colon; &num;0c343d&semi;">Here see that there is one table for the address object created separately for the collection&period;<&sol;div>&NewLine;<div style&equals;"color&colon; &num;0c343d&semi;">1&period; First Table <b>TBL&lowbar;USER&lowbar;DETAILS<&sol;b><&sol;div>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;tableAddress&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><&sol;div>&NewLine;<div style&equals;"color&colon; &num;0c343d&semi;"> 2&period; Second Table <b>TBL&lowbar;USER&lowbar;DETAILS&lowbar;lisOfAddresses <&sol;b><&sol;div>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;tableAddress2&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<p>In the second table first column&comma; <b style&equals;"color&colon; &num;0c343d&semi;">TBL&lowbar;USERS&lowbar;DETAILS&lowbar;USER&lowbar;ID<&sol;b> has the <b style&equals;"color&colon; &num;0c343d&semi;">user id is<&sol;b> foreign key for this table and primary key of the  <b style&equals;"color&colon; &num;0c343d&semi;">TBL&lowbar;USER&lowbar;DETAILS <&sol;b>table&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;tableAddress2-1&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b><br &sol;>&NewLine;<&sol;b> <b>In <&sol;b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;configuring-collections-and-adding-keys&period;html">Next Chapter<&sol;a>&comma; you will learn more about the <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;configuring-collections-and-adding-keys&period;html">configuration of the collection and adding keys<&sol;a>&period;<&sol;p>&NewLine;<p><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;value-types-and-embedding-objects&period;html">Previous Chapter 13<&sol;a>&lt&semi;&lt&semi;    &gt&semi;&gt&semi;N<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;configuring-collections-and-adding-keys&period;html">ext Chapter15<&sol;a><&sol;b><&sol;p>&NewLine;<&sol;div>&NewLine;

View Comments

  • hi dinesh there is a probleme while running above programm or query it shows error @EmbeddedCollection please tell me why it occurs or which type of plugnin i have to use

  • Hi Dinesh
    I got an error when i run this prog and error is ............
    Initial SessionFactory creation failed.org.hibernate.MappingException: Could not determine type for: java.util.Collection, for columns: [org.hibernate.mapping.Column(lisOfAddresses)]

  • Hi Dinesh
    I got same error as mentioned above :
    Exception in thread "main" org.hibernate.MappingException: Could not determine type for: java.util.Collection, for columns: [org.hibernate.mapping.Column(lisOfAddresses)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
    at org.hibernate.mapping.Property.isValid(Property.java:185)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:440)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1102)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
    at pack.TestDemo.main(TestDemo.java:35)

    Please help me out to resolve this

  • HI i faced the same problem org.hibernate.MappingException: Could not determine type for: java.util.Collection, for columns:

    But i fixed this by adding @OneToMany annotations on addressList field

  • Hi too got Mapping Exception as others and it got fixed by adding @OneToMany annotations as suggested by asad .Thank you

  • hello Sir,
    I wanted to save list of entity in single DB connection.
    how can i do ?
    tx in advance