Hibernate, updating an object containing a collection without loading the collection - java

I have a Project that contains a Set of Columns that contains a Set of Tasks that contains a Set of TaskAssigness.
I'm trying to update a column title without having to load its set of tasks, but I get the following error :
[AssertionFailure] - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: collection [com.example.tasks.Task.taskAssignees] was not processed by flush()
I have the following hbm.xml :
Project.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.exemple.tasks.project.Project" table="P_TASKS_PROJECT">
<cache usage="read-write" />
<id name="rowId" column="rowId">
<generator class="native"/>
</id>
...
<set name="columns" table="P_TASK_PROJECT_COLUMN" inverse="true" cascade="all" sort="com.exemple.tasks.project.ColumnIndexComparator">
<cache usage="read-write" />
<key column="projectId" not-null="true" />
<one-to-many class="com.exemple.tasks.project.Column" />
</set>
</class>
</hibernate-mapping>
Column.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.example.tasks.project.Column" table="P_TASK_PROJECT_COLUMN">
<cache usage="read-write" />
<id name="rowId" column="rowId">
<generator class="native"/>
</id>
...
<property name="title" />
<many-to-one name="project" class="com.example.tasks.project.Project">
<column name="projectId" not-null="false" />
</many-to-one>
<set name="tasks" table="P_TASKS_JOIN_COLUMNS">
<cache usage="read-write" />
<key column="columnId" not-null="true" />
<many-to-many column="taskId" class="com.example.tasks.Task" unique="false" />
</set>
</class>
</hibernate-mapping>
Task.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.example.tasks.Task" table="P_TASK">
<cache usage="read-write" />
<id name="rowId" column="rowId">
<generator class="native"/>
</id>
...
<set name="taskAssignees" table="P_TASK_ASSIGNEE" cascade="all-delete-orphan">
<cache usage="read-write" />
<key column="taskId" not-null="true" />
<one-to-many class="com.example.tasks.TaskAssignee" />
</set>
</class>
</hibernate-mapping>
TaskAsignee.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.example.tasks.TaskAssignee" table="P_TASK_ASSIGNEE">
<cache usage="read-write" />
<id name="rowId" column="rowId">
<generator class="native"/>
</id>
...
<many-to-one name="task" class="com.example.tasks.Task" insert="false" update="false">
<column name="taskId" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
I'm trying to update the title of a column this way :
//Updating column title
Column column = project.getColumn(index); // return a column from the project. Project contains a SortedSet of Column
column.setTitle("NewTitle");
session.saveOrUpdate(project);
This works fine if the column does not contains any tasks.
If I try to update a column containing tasks I get the following error message:
[AssertionFailure] - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: collection [com.example.tasks.Task.taskAssignees] was not processed by flush()
If i get tasks and tasks assigness before updating the project, I have no errors :
Set<Task> tasks = col.getTasks();
if (tasks != null) {
for (Task task : tasks) {
task.getTaskAssignees();
}
}
session.saveOrUpdate(project);
Is there a way to change my column title, then update the project without having to get tasks and task assignees ? Can I tell hibernate just to update the column and not try to go further.

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>

Hibernate Many-to-Many doesn't work

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

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