• Post Reply Bookmark Topic Watch Topic
  • New Topic

CollectionsInHibernate  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!