Hibernate without primary keys generated by db? - java

I'm building a data warehouse and want to use InfiniDB as the storage engine. However, it doesn't allow primary keys or foreign key constraints (or any constraints for that matter).
Hibernate complains "The database returned no natively generated identity value" when I perform an insert.
Each table is relational, and contains a unique integer column that was previously used as the primary key - I want to keep that, but just not have the constraint in the db that the column is the primary key.
I'm assuming the problem is that Hibernate expects the db to return a generated key. Is it possible to override this behaviour so I can set the primary key field's value myself and keep Hibernate happy?
-- edit --
Two of the mappings are as follows:
<?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 Jun 1, 2010 2:49:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.example.project.Visitor" table="visitor" catalog="orwell">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<property name="firstSeen" type="timestamp">
<column name="first_seen" length="19" />
</property>
<property name="lastSeen" type="timestamp">
<column name="last_seen" length="19" />
</property>
<property name="sessionId" type="string">
<column name="session_id" length="26" unique="true" />
</property>
<property name="userId" type="java.lang.Long">
<column name="user_id" />
</property>
<set name="visits" inverse="true">
<key>
<column name="visitor_id" />
</key>
<one-to-many class="com.example.project.Visit" />
</set>
</class>
</hibernate-mapping>
and:
<?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 Jun 1, 2010 2:49:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.example.project.Visit" table="visit" catalog="orwell">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<many-to-one name="visitor" class="com.example.project.Visitor" fetch="join" cascade="all">
<column name="visitor_id" />
</many-to-one>
<property name="visitId" type="string">
<column name="visit_id" length="20" unique="true" />
</property>
<property name="startTime" type="timestamp">
<column name="start_time" length="19" />
</property>
<property name="endTime" type="timestamp">
<column name="end_time" length="19" />
</property>
<property name="userAgent" type="string">
<column name="user_agent" length="65535" />
</property>
<set name="pageViews" inverse="true">
<key>
<column name="visit_id" />
</key>
<one-to-many class="com.example.project.PageView" />
</set>
</class>
</hibernate-mapping>

As you noted in a comment, there're many id generators you can use. E.g., you many find 'increment' convenient. Complete overview
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-id
And hibernate won't care about your db restrictions (both primary keys and foreign keys). In fact, hibernate has no way of knowing about certain db restriction until it's violated. And if restriction doesn't exist, it can never be violated :)

You can remove the <generator class="identity" /> element and set the id manually before saving the object.

Related

Repeated column in mapping for entity, oneToMany relationship

I am trying to do a one-to-many relationship between two tables. I have a table called users that has many user_history entries. I defined these tables in mysql and I generated the entities with intellij hibrernate support. The problem is that when i want to insert something in the databse I am receiving the following error Repeated column in mapping for entity: com.UserHistoryEntity
User:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.UsersEntity" table="users" schema="" catalog="protein_tracker">
<id name="id">
<column name="id" sql-type="int" not-null="true"/>
</id>
<property name="name">
<column name="name" sql-type="varchar" length="45" not-null="true"/>
</property>
<property name="total">
<column name="total" sql-type="int" not-null="true"/>
</property>
<property name="goal">
<column name="goal" sql-type="int" not-null="true"/>
</property>
<set name="userHistoriesById" inverse="true">
<key>
<column name="id_user" not-null="true"/>
</key>
<one-to-many not-found="ignore" class="com.UserHistoryEntity"/>
</set>
</class>
</hibernate-mapping>
UserHistory:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.UserHistoryEntity" table="user_history" schema="" catalog="protein_tracker">
<id name="id">
<column name="id" sql-type="int" not-null="true"/>
</id>
<property name="entryDate">
<column name="entry_date" sql-type="datetime" not-null="true"/>
</property>
<property name="entry">
<column name="entry" sql-type="varchar" length="45" not-null="true"/>
</property>
<property name="idUser">
<column name="id_user" sql-type="int" not-null="true"/>
</property>
<many-to-one name="usersByIdUser" class="com.UsersEntity">
<column name="id_user" not-null="true"/>
</many-to-one>
</class>
</hibernate-mapping>
How can I fix this issue?
Mark your Many-To-One mapping for usersByIdUser to insertable = false and updatable = false. This allows the relationship to only be managed by a single property, idUser that you have mapped.

Hibernate java web application

I am working on a java JSP webapplication that uses the hibernate framework.
I am a total beginner with JSP/hibernate, and I can't seem to get hibernate to work.
I have followed this tutorial: https://netbeans.org/kb/docs/web/hibernate-webapp.html
It all worked. I used xampp with phpmyadmin, and I could execute HQL query's through the hibernate.cfg.xml file.
Then I was trying the same thing with the database I am using for the web application. Followed all steps and went through all the wizards. But I can't execute HQL query's.
the following error is given:
org.hibernate.MappingException: An association from the table campingsperfestival refers to an unmapped class: festivalOverview.Campings
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1252)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1170)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:324)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
I followed the Hibernate Mapping Files and POJOs from Database wizard, and it generated a .java and .hbm.xml file for all tables in the database except for one table: the 'campingsperfestival' table. I've done the wizard again a few times and started it all over but it still doesn't generate the .java and .hbm.xml file for the 'campingsperfestival' table.
The 'campingsperfestival' table is a table with 2 id's that both have a foreign key. There are festivals and campings that both have ID's, the 'campingsperfestival' matches those 2 id's in one table.
Campings.hbm.xml:
<?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 20-apr-2013 12:04:37 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="festivalOverview.Campings" table="campings" catalog="groep11_festivals">
<id name="campId" type="java.lang.Integer">
<column name="camp_id" />
<generator class="identity" />
</id>
<property name="campAdres" type="string">
<column name="camp_adres" not-null="true" />
</property>
<property name="campCap" type="int">
<column name="camp_cap" not-null="true" />
</property>
<set name="festivalses" inverse="true" table="campingsperfestival">
<key>
<column name="camp_id" not-null="true" />
</key>
<many-to-many entity-name="festivalOverview.Festivals">
<column name="fest_id" not-null="true" />
</many-to-many>
</set>
<set name="facpercamps" inverse="true">
<key>
<column name="camp_id" not-null="true" />
</key>
<one-to-many class="festivalOverview.Facpercamp" />
</set>
</class>
</hibernate-mapping>
Festivals.hbm.xml:
<?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 20-apr-2013 12:04:37 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="festivalOverview.Festivals" table="festivals" catalog="groep11_festivals">
<id name="festId" type="java.lang.Integer">
<column name="fest_id" />
<generator class="identity" />
</id>
<property name="festNaam" type="string">
<column name="fest_naam" length="100" not-null="true" />
</property>
<property name="festLocatie" type="string">
<column name="fest_locatie" length="200" not-null="true" />
</property>
<property name="festDatum" type="date">
<column name="fest_datum" length="10" not-null="true" />
</property>
<property name="festDuur" type="byte">
<column name="fest_duur" not-null="true" />
</property>
<set name="ticketses" inverse="true">
<key>
<column name="fest_id" not-null="true" />
</key>
<one-to-many class="festivalOverview.Tickets" />
</set>
<set name="bandsperfestivals" inverse="true">
<key>
<column name="fest_id" not-null="true" />
</key>
<one-to-many class="festivalOverview.Bandsperfestival" />
</set>
<set name="campingses" inverse="false" table="campingsperfestival">
<key>
<column name="fest_id" not-null="true" />
</key>
<many-to-many entity-name="festivalOverview.Campings">
<column name="camp_id" not-null="true" />
</many-to-many>
</set>
<set name="tickettypesperfestivals" inverse="true">
<key>
<column name="fest_id" not-null="true" />
</key>
<one-to-many class="festivalOverview.Tickettypesperfestival" />
</set>
</class>
</hibernate-mapping>
I'm only a beginner with hibernate and really don't know how to solve this problem. Any help is greatly appreciated!
I assume campingsperfestival is a join table between two classes, Campings and Festival? Have you defined both these classes and their mapping?
The error you have there is saying it can't create campingsperfestival because it is referring to Campings, which is not defined as as hibernate class. So make sure Campings is defined and you have the mapping correct.
If you are still unclear we may be able to help more if you show the java/mappings you have for campings and festival.
As an aside, if this is a new project you are embarking on, I would really recommend using annotation based hibernate classes. You may also find it a more productive learning experience to create your hibernate entity classes yourself rather than using netbeasns - but that's down to personal preference.

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 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