aspose file tools*
The moose likes Object Relational Mapping and the fly likes CollectionsInHibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "CollectionsInHibernate" Watch "CollectionsInHibernate" New topic
Author

CollectionsInHibernate

kishor kalapa
Greenhorn

Joined: Jun 05, 2008
Posts: 24
package com.coll.clients;

import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.coll.pojos.Images123;
import com.coll.pojos.Item123;

public class CollectionKing {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SessionFactory sf=new Configuration().configure().buildSessionFactory();
session s=sf.openSession();
Transaction tx=s.beginTransaction();
Images123 i=new Images123(); //1*********
Images123 i1=new Images123(); //2**********
Item123 it=new Item123();
Set set=new HashSet();
i.setItemId(1);
i.setFileName("fooimage1.jpg");
i1.setFileName("fooimages2.jpg");
set.add(i);
set.add(i1);
it.setImages(set);
it.setItemName("car");
it.setItemDescription("Car is very good");
it.setItemId(1);
s.save(it);
tx.commit();


}

}
--------------------------------------------------------------------------
Images123:
package com.coll.pojos;

public class Images123 {
private int itemId;
private String fileName;

public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}

------------------------------------------------------------------------
Item123.java :

package com.coll.pojos;

import java.util.HashSet;
import java.util.Set;

public class Item123 {
private int itemId;
private String itemName;
private String itemDescription;
private Set images=new HashSet();
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemDescription() {
return itemDescription;
}
public void setItemDescription(String itemDescription) {
this.itemDescription = itemDescription;
}
public Set getImages() {
return images;
}
public void setImages(Set images) {
this.images = images;
}

}}
------------------------------------------------------------------
primarycollection-hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.coll.pojos">
<class name="Item123" table="item">
<id name="itemId" column="item_id">
</id>
<property name="itemName" column="name"></property>
<property name="itemDescription" column="description"></property>
<set name="images" table="item_image">
<key column="ITEM_ID"></key>
<element type="string" column="FILENAME" not-null="true"></element>
</set>
</class>
</hibernate-mapping>
-------------------------------------------------------------
cfg file:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">RaviDriver</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping resource="com/coll/pojos/primarycollection-hbm.xml" />

</session-factory>

</hibernate-configuration>
--------------------------------------------------------------
when I run the CollectionKing.java I am getting ClasscastException on image123(as element) insertion into images(set),if I am replace set element as string program runnig fine.
Can any one mention changes required to run CollectionKing.java for set element as Images123.
 
Don't get me started about those stupid light bulbs.
 
subject: CollectionsInHibernate
 
Similar Threads
Hibernate Issue
Hibernate exception for one-to-one mapping
CascadeType.PERSIST not working
Mapping Error Hibernate
Exception while inserting a row using Hibernate