Hibernate Many-to-Many doesn't work - java

I try to understand hibernate but it is very hard.
I have a problem now that i don't really understand. Its about the many-to-many relations in my mapping file. If I save an object it wont save the "many" in my DB but it wont save it. I think it is in my mapping but i don't see it.
It's about flight and staff on the plane. When I save the plane the staff members must be save with it. But that wont happen.
Here is my mapping of both:
The Flight mapping:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 28-sep-2012 11:49:37 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="model.Flight" table="flights" catalog="flyaway_db">
<id name="number" type="int">
<column name="FlightNumber" />
<generator class="assigned" />
</id>
<set name="staffs" table="flightstaff" cascade="save-update">
<key>
<column name="FlightNumber" not-null="true" />
</key>
<many-to-many class="model.Staff">
<column name="StaffNumber" length="5" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
The staff mapping:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 28-sep-2012 11:49:37 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="model.Staff" table="staff" catalog="flyaway_db">
<id name="staffNumber" type="string">
<column name="StaffNumber" length="5" />
<generator class="assigned" />
</id>
<set name="flightses" table="flightstaff" cascade="save-update">
<key>
<column name="StaffNumber" length="5" not-null="true" />
</key>
<many-to-many class="model.Flight">
<column name="FlightNumber" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>

You forgot the inverse="true" on one side of the bidirectional relationship.

Sorry for all the question but it was the model that wasn't correct! and i think Pache answer also helped!
Thanks all

Related

Hibernate One To Many Relation Hbm Xml Error

I want to create relation between Definition (DEF) and Details (DET) table by foreign keys in Hibernate. In this case, one definition has multiple details.
Definition table has ID column as PK
Details table has ID column as PK and DEFINITION_ID as FK
When I connect these inside of hbm.xml I am getting an error such as;
ERROR
Caused by: org.hibernate.MappingException: Repeated column in mapping
for entity: com.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDet
column: ID (should be mapped with insert="false" update="false")
DEFINITION HBM XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDef"
table="CMS_EXEMPT_RESTRICT_DEF">
<tuplizer class="com.ykb.hmn.inf.core.datamodel.IntfEntityTuplizer"
entity-mode="pojo" />
<id name="oid" type="java.lang.Long">
<column name="ID" />
<generator class="sequence">
<param name="sequence">SEQ_CMS_EXEMPT_RESTRICT_DEF</param>
</generator>
</id>
.
.
.
<bag name="cmsExemptRestrictDetails" table="CMS_EXEMPT_RESTRICT_DET"
inverse="true" lazy="true" fetch="select">
<key>
<column name="DEFINITION_ID" not-null="true" />
</key>
<one-to-many
class="com.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDet" />
</bag>
</class>
</hibernate-mapping>
DETAIL HBM XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDet"
table="CMS_EXEMPT_RESTRICT_DET">
<tuplizer class="com.ykb.hmn.inf.core.datamodel.IntfEntityTuplizer"
entity-mode="pojo" />
<id name="oid" type="java.lang.Long">
<column name="ID" />
<generator class="sequence">
<param name="sequence">SEQ_CMS_EXEMPT_RESTRICT_DET</param>
</generator>
</id>
<many-to-one name="cmsExemptRestrictDefinition" class="com.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDef" fetch="select">
<column name="ID" not-null="true" />
</many-to-one>
<property name="definitionId" type="java.lang.Long">
<column name="DEFINITION_ID" />
</property>
</class>
</hibernate-mapping>
First - Do I need to define both many to one and one to many relation in each hbm xmls?
Second - Do I need to rename column ID of Detail something like DETAIL_ID?
Thanks.
The property mapping Definition_id is incorrect. The foreign key should not be mapped. Remove it and change the column name in manytoone mapping to definition_id:
Change the detail hbm xml to:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDet"
table="CMS_EXEMPT_RESTRICT_DET">
<tuplizer class="com.ykb.hmn.inf.core.datamodel.IntfEntityTuplizer"
entity-mode="pojo" />
<id name="oid" type="java.lang.Long">
<column name="ID" />
<generator class="sequence">
<param name="sequence">SEQ_CMS_EXEMPT_RESTRICT_DET</param>
</generator>
</id>
<many-to-one name="cmsExemptRestrictDefinition" class="com.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDef" fetch="select">
<column name="DEFINITION_ID" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>

How do I cascade saves through a many to many relationship with hibernate?

I have a many to many (with a join table) relationship in my database that I need to model in hibernate.
I have POJO classes for all three tables in the database as the join table contains more than just the mapping information.
My problem occurs on save() of class A where class A is told to cascade to class AB (the join table) which is told to cascade to class B, but when it gets to the point where it is saving AB the foriegn key reference to table A is null.
At the end of the day I want to be able to call save() on an instance of the TableA class and have it save all three tables.
Table Diagram
TABLE A Mapping
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Aug 27, 2012 2:16:37 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.example.dbtables.TableA" table="Table A">
<id name="id" type="int">
<column name="id" />
<generator class="sequence">
<param name="sequence">table_a_seq</param>
</generator>
</id>
<natural-id>
<property name="data1" type="int">
<column name="DATA1" not-null="true" />
</property>
<property name="data2" type="string">
<column name="DATA2" length="16" not-null="true" />
</property>
</natural-id>
<set name="tableAB" cascade="save-update" table="TableAB" inverse="true" lazy="true" fetch="select">
<key>
<column name="Table_AB_ID" not-null="true" />
</key>
<one-to-many class="com.example.dbtables.TableAB" />
</set>
</class>
</hibernate-mapping>
TABLE AB Mapping
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Aug 27, 2012 2:16:37 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.example.dbtables.TableAB" table="Table AB">
<id name="id" type="int">
<column name="id" />
<generator class="sequence">
<param name="sequence">table_ab_seq</param>
</generator>
</id>
<natural-id>
<many-to-one name="tableA" class="com.example.dbtables.TableA" fetch="select">
<column name="Table_A_ID" not-null="true" />
</many-to-one>
<many-to-one name="tableB" cascade="save-update" class="com.example.dbtables.TableB" fetch="select">
<column name="Table_B_ID" not-null="true" />
</many-to-one>
<property name="data1" type="int">
<column name="DATA1" not-null="true" />
</property>
<property name="data2" type="int">
<column name="DATA2" not-null="true" />
</property>
</natural-id>
</class>
</hibernate-mapping>
TABLE B Mapping
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Aug 27, 2012 2:16:37 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.examples.dbtables.TableB" table="Table B">
<id name="id" type="int">
<column name="id" />
<generator class="sequence">
<param name="sequence">table_b_seq</param>
</generator>
</id>
<natural-id>
<property name="data1" type="string">
<column name="DATA1" length="16" />
</property>
<property name="data2" type="java.lang.Integer">
<column name="DATA2" />
</property>
</natural-id>
<set name="TableAB" table="Table AB" inverse="true" lazy="true" fetch="select">
<key>
<column name="Table_AB_ID" not-null="true" />
</key>
<one-to-many class="com.example.dbtables.TableAB" />
</set>
</class>
</hibernate-mapping>
Thanks for any help you can give. Let me know if you need clarification or anymore information.
EDIT: My problem was that the references from AB -> to A were not getting correctly populated. This issue has been resolved thanks to everyone who helped.
A a = new A();
B b = new B();
AB ab = new AB();
ab.setA(a);
ab.setB(b);
a.setAB(ab);
b.setAB(ab);
save(a);
This example not working? Did you get some exception?
EDIT
Look into this Hibernate doc Chapter 5. Basic O/R Mapping to chapter 5.1.4.1. Generator
PostgreSQL doesn't support identity generation type.
Use sequence generation instead of identity, or something else
The problem was that the back references between AB -> A were not getting correctly populated in my code. Thanks to Ilya for the help.

Hibernate One-To-Many Could Not Initialise Collection

I have two database tables, User and PageComment. Using Hibernate, I'm trying to store a Set of PageComment objects in the User comment (comments made to that user), by using one-to-many relationship in the hbm XML files.
The problem is, I seem to be able to retrieve the set from a User object, but as soon as I try to access any of the objects stored within the set, or even access on of the methods included in the set class (i.e. size()), the JVM throws "org.hibernate.exception.GenericJDBCException: could not initialize a collection". I'm at a loss on this one.
HBM For User Table:
<?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>
<class name="User" table="user">
<id name="username" column="Username" type="string">
<generator class="assigned"></generator>
</id>
<property name="password" column="Password" type="string"></property>
<property name="firstname" column="Firstname" type="string"></property>
<property name="surname" column="Surname" type="string"></property>
<property name="email" column="Email" type="string"></property>
<property name="admin" column="Admin" type="integer"></property>
<set name="commentsMadeTo" inverse="true">
<key column="userMadeTo"/>
<one-to-many class="PageComment"/>
</set>
</class>
</hibernate-mapping>
HBM For PageComment:
<?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>
<class name="PageComment" table="PageComment">
<composite-id>
<key-property name="userMadeBy" column="UserMadeBy" type="string" />
<key-property name="userMadeTo" column="UserMadeTo" type="string" />
<key-property name="time" column="Time" type="integer" />
<generator class="assigned"></generator>
</composite-id>
<property name="commentText" column="CommentText" type="string"></property>
<many-to-one name="userMadeTo" column="Username" not-null="true" class="User" />
</class>
</hibernate-mapping>
I'm trying to test the mapping with this method:
Session session = sessionFactory.openSession();
User theUser = (User)session.createQuery("FROM User WHERE Username='Samat'").uniqueResult();
System.out.println("Trying to print out all comments made to 'Samat'");
Set<PageComment> theComments = theUser.getCommentsMadeTo();
for(PageComment p: theComments){
System.out.println(p.getAllData());
}
I spot that there are some problems in the relationship mapping
HBM For User Table:
<set name="commentsMadeTo" inverse="true">
<key column="XXXXXXXX"/>
<one-to-many class="PageComment"/>
</set>
HBM For PageComment:
<many-to-one name="userMadeTo" column="XXXXXXX" not-null="true" class="User" />
The value XXXXX represents the column name on the "many" side (i.e PageComment table in your case) that associates to its "One" side. It should have the same value in both mapping hbm.
Try to change the hbm for user table to:
<set name="commentsMadeTo" inverse="true">
<key column="Username"/>
<one-to-many class="PageComment"/>
</set>

Hibernate Problem Not Saving to database

I am getting a hibernate problem. I am trying with no success. I have a schema where there are Category,Attribute,AttributeOption. e.g. a category can be a 'computer', and its respective attribute can be 'RAM','Hard Disk' and e.g. 'RAM' can have attribute option '512MB','1024MB'
When I try the above schema with hibernate everything is OK.
Now I have one other requirement. A 'Category' can have many sub categories. e.g. a 'Computer' can have 'laptop' or 'Notebook' as sub categories. Now these subcategories are of the class Category themselves
then I get this scheme:
1. Category ------>Category
A category can contain many sub categories
e.g. A computer can be a notebook or laptop
2. Category ------>Attribure
A category can have many attribute
A notebook can have RAM , Hard Disk, Screen Size
3. Attribute ------>AttributeOption
An Attribute can have many attribute options
e.g. RAM can be 512 MB, 1024 MB
These are my classes without their getters and setters
Class Category:
public class Category implements IsSerializable
{
private long CategoryId;
private String CategoryName;
private Set <Category> SubCategory=new HashSet <Category> ();
private Set <Attribute> AllAttributes= new HashSet <Attribute>();
}
Category Mapping File:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 16, 2010 8:07:32 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
<class name="com.BiddingSystem.Models.Category" table="CATEGORY">
<id name="CategoryId" type="long">
<column name="CATEGORYID" />
<generator class="native" />
</id>
<property name="CategoryName" type="java.lang.String">
<column name="CATEGORYNAME" />
</property>
<many-to-one name="ParentCategory" class="com.BiddingSystem.Models.Category">
<column name="PARENTCATEGORYID" />
</many-to-one>
<set name="SubCategory" inverse="true" lazy="true" cascade="all" fetch="join">
<key>
<column name="PARENTCATEGORYID" />
</key>
<one-to-many class="com.BiddingSystem.Models.Category" />
</set>
<set name="AllAttributes" table="ATTRIBUTE" inverse="false" lazy="true">
<key>
<column name="CATEGORYID" />
</key>
<one-to-many class="com.BiddingSystem.Models.Attribute" />
</set>
</class>
</hibernate-mapping>
Class Attribute:
public class Attribute
{
private long AttributeId;
private String AttributeName;
private Set <AttributeOption> Options= new HashSet <AttributeOption>();
}
Attribute Mapping File:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 16, 2010 5:25:09 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
<class name="com.BiddingSystem.Models.Attribute" table="ATTRIBUTE">
<id name="AttributeId" type="long">
<column name="ATTRIBUTEID" />
<generator class="native" />
</id>
<property name="AttributeName" type="java.lang.String">
<column name="ATTRIBUTENAME" />
</property>
<set name="Options" table="ATTRIBUTEOPTION" inverse="false" cascade="all">
<key>
<column name="ATTRIBUTEID" />
</key>
<one-to-many class="com.BiddingSystem.Models.AttributeOption" />
</set>
</class>
</hibernate-mapping>
Class AttributeOption:
public class AttributeOption
{
private long AttributeOptionId;
private String Option;
private String SQLValue;
}
Attribute Mapping File:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 16, 2010 5:25:09 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
<class name="com.BiddingSystem.Models.AttributeOption" table="ATTRIBUTEOPTION">
<id name="AttributeOptionId" type="long">
<column name="ATTRIBUTEOPTIONID" />
<generator class="native" />
</id>
<property name="Option" type="java.lang.String">
<column name="OPTION" />
</property>
<property name="SQLValue" type="java.lang.String">
<column name="SQLVALUE" />
</property>
</class>
</hibernate-mapping>
I am trying the following. I am not getting any error but It is not saving the 'Laptop' which is a category of 'computer' else everything is being saved. So i think the problem is with this part from the category mapping file:
<set name="SubCategory" table="CATEGORY" cascade="all">
<key>
<column name="CATEGORYID" />
</key>
<one-to-many class="com.BiddingSystem.Models.Category" />
</set>
This is part of my program
Category C=new Category();
C.setCategoryName("Computer");
AttributeOption R512= new AttributeOption();R512.setOption("512");R512.setSQLValue("512");
AttributeOption R1024= new AttributeOption();R1024.setOption("1024");R1024.setSQLValue("1024");
Category C0= new Category();
C0.setCategoryName("Laptop");
C.getSubCategory().add(C0);
Attribute RAM= new Attribute();
RAM.setAttributeName("RAM");
RAM.getOptions().add(R512);RAM.getOptions().add(R1024);
C.getAllAttributes().add(RAM);
Transaction tx = null;
try
{
tx=session.beginTransaction();
tx.begin();
session.saveOrUpdate(C);
tx.commit();
return true;
}
catch (Exception e)
{
tx.rollback();
e.printStackTrace();
return false;
}
Something seems wrong with this:
C0.getSubCategory().add(C0);
Shouldn't it be:
C.getSubCategory().add(C0);
Hummm, I see some place for improvement in this code :
First, you should use the persist() method to persist your entities.
Second, to persist a whole object graph, you must either a) persist the sub-objects manually, then the "main" object ; or b) use the Cascade parameter on your relationships so that persisting the "main" object also triggers the persistence of the related sub-objects.
You can find more info on cascading here :
http://docs.jboss.org/hibernate/stable/core/reference/en/html/example-parentchild.html#example-parentchild-cascades
Ok, I made it to work
The key to it is the category mapping file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 16, 2010 8:37:02 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
<class name="com.BiddingSystem.Models.Category" table="CATEGORY">
<id name="CategoryId" type="long">
<column name="CATEGORYID" />
<generator class="native" />
</id>
<property name="CategoryName" type="java.lang.String">
<column name="CATEGORYNAME" />
</property>
<many-to-one name="ParentCategory" class="com.BiddingSystem.Models.Category">
<column name="PARENT_CATEGORY_ID" />
</many-to-one>
<set name="SubCategory" lazy="false" cascade="all-delete-orphan" inverse="true">
<key>
<column name="PARENT_CATEGORY_ID" />
</key>
<one-to-many class="com.BiddingSystem.Models.Category" />
</set>
<set name="AllAttributes" table="ATTRIBUTE" inverse="false" lazy="true">
<key>
<column name="CATEGORYID" />
</key>
<one-to-many class="com.BiddingSystem.Models.Attribute" />
</set>
</class>
</hibernate-mapping>

Hibernate Loading not loading the whole tree

I am trying to make hibernate load my whole tree with no success.
I have a schema like this
A Category can have many attribute and an attribute can have many options
Category
--->Attribute1
--->AttributeOption1
--->AttributeOption3
--->Attribute2
--->AttributeOption1
--->AttributeOption1
but when I am using hibernate, it is not loading the whole tree:
my mapping file are:
Attribute Mapping FIle:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 16, 2010 5:25:09 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
<class name="com.BiddingSystem.Models.Attribute" table="ATTRIBUTE">
<id name="AttributeId" type="long">
<column name="ATTRIBUTEID" />
<generator class="native" />
</id>
<property name="AttributeName" type="java.lang.String">
<column name="ATTRIBUTENAME" />
</property>
<set name="Options" table="ATTRIBUTEOPTION" inverse="false" cascade="all" lazy="true">
<key>
<column name="ATTRIBUTEID" />
</key>
<one-to-many class="com.BiddingSystem.Models.AttributeOption" />
</set>
</class>
</hibernate-mapping>
Attribute Option Mapping File:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 16, 2010 5:25:09 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
<class name="com.BiddingSystem.Models.AttributeOption" table="ATTRIBUTEOPTION">
<id name="AttributeOptionId" type="long">
<column name="ATTRIBUTEOPTIONID" />
<generator class="native" />
</id>
<property name="Option" type="java.lang.String">
<column name="OPTION" />
</property>
<property name="SQLValue" type="java.lang.String">
<column name="SQLVALUE" />
</property>
</class>
</hibernate-mapping>
Category Mapping File:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 16, 2010 8:37:02 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
<class name="com.BiddingSystem.Models.Category" table="CATEGORY">
<id name="CategoryId" type="long">
<column name="CATEGORYID" />
<generator class="native" />
</id>
<property name="CategoryName" type="java.lang.String">
<column name="CATEGORYNAME" />
</property>
<many-to-one name="ParentCategory" class="com.BiddingSystem.Models.Category">
<column name="PARENT_CATEGORY_ID" />
</many-to-one>
<set name="SubCategory" lazy="true" cascade="all-delete-orphan" inverse="true">
<key>
<column name="PARENT_CATEGORY_ID" />
</key>
<one-to-many class="com.BiddingSystem.Models.Category" />
</set>
<set name="AllAttributes" table="ATTRIBUTE" inverse="false" lazy="true" cascade="all">
<key>
<column name="CATEGORYID" />
</key>
<one-to-many class="com.BiddingSystem.Models.Attribute" />
</set>
</class>
</hibernate-mapping>
The function that loads the parent category are:
public List <Category> getCategory(long CategoryId)
{
Session session = gileadHibernateUtil.getSessionFactory().openSession();
List <Category> AllCategory= new LinkedList<Category> ();
String SQL="from Category where parent_category_id is NULL";
Query query = session.createQuery(SQL);
return query.list();
}
Make all your associations lazy=false; by default the lazy = true
I believe there is an other mistake:
String SQL="from Category where parent_category_id is NULL";
Query query = session.createQuery(SQL);
org.hibernate.Session.createQuery(String queryString) requires a HQL Query String, but not a SQL query! And from Category where parent_category_id is NULL seems to be a SQL query, because parent_category_id is a column name but not a property / field name.

Categories

Resources