Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:3.2
Name and version of the database you are using:MYSQL 5
Dear All:
I have a bit complex structure like i have four table named as Company, Branch, Application & Users
Company Contain branches branches contains applications
users are registered for a company.. i.e..
Users-Company
------------------Branch
----------------------Application
my one table use for the mapping of all these table into one
but i only contains ids of relevant tables in that table...
my script it
Quote:
Mapping Table:
=========
CREATE TABLE `adm_up_ucba` (
`UCBA_id` int(8) unsigned NOT NULL auto_increment,
`user_id` int(8) unsigned NOT NULL,
`company_id` int(8) unsigned NOT NULL,
`branch_id` int(8) unsigned NOT NULL,
`application_id` int(8) unsigned NOT NULL,
PRIMARY KEY (`UCBA_id`),
UNIQUE KEY `user_screen_name_unq` (`user_id`,`company_id`,`branch_id`,`application_id`),
KEY `user_id` (`user_id`),
KEY `company_id` (`company_id`),
KEY `application_id` (`application_id`),
KEY `branch_id` (`branch_id`),
CONSTRAINT `adm_up_user_ibfk_1` FOREIGN KEY (`company_id`) REFERENCES `adm_up_company` (`company_id`) on delete cascade,
CONSTRAINT `adm_up_user_ibfk_2` FOREIGN KEY (`application_id`) REFERENCES `adm_up_application` (`application_id`) on delete cascade,
CONSTRAINT `adm_up_user_ibfk_3` FOREIGN KEY (`branch_id`) REFERENCES `adm_up_branch` (`branch_id`) on delete cascade,
CONSTRAINT `adm_up_user_ibfk_4` FOREIGN KEY (`user_id`) REFERENCES `adm_up_user` (`user_id`) on delete cascade
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Cpy:
====
CREATE TABLE `adm_up_company` (
`company_id` int(8) unsigned NOT NULL,
`company_name` varchar(100) NOT NULL,
`company_address` text NOT NULL,
`company_email` varchar(100) NOT NULL,
`company_fax` varchar(25) NOT NULL,
`company_phone` varchar(25) NOT NULL,
PRIMARY KEY (`company_id`),
UNIQUE KEY `cpy_name_unq` (`company_name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Module:
=====
CREATE TABLE `adm_up_module` (
`module_id` int(8) unsigned NOT NULL auto_increment,
`company_id` int(8) unsigned NOT NULL,
`branch_id` int(8) unsigned NOT NULL,
`application_id` int(8) unsigned NOT NULL,
`module_name` varchar(100) NOT NULL,
`module_description` text,
PRIMARY KEY (`module_id`),
UNIQUE KEY `mod_name_unq` (`module_name`,`company_id`,`branch_id`,`application_id`),
KEY `company_id` (`company_id`),
KEY `branch_id` (`branch_id`),
KEY `application_id` (`application_id`),
CONSTRAINT `adm_up_module_ibfk_1` FOREIGN KEY (`company_id`) REFERENCES `adm_up_company` (`company_id`) on delete cascade,
CONSTRAINT `adm_up_module_ibfk_2` FOREIGN KEY (`branch_id`) REFERENCES `adm_up_branch` (`branch_id`) on delete cascade,
CONSTRAINT `adm_up_module_ibfk_3` FOREIGN KEY (`application_id`) REFERENCES `adm_up_application` (`application_id`) on delete cascade
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Branch
=====
CREATE TABLE `adm_up_branch` (
`branch_id` int(8) unsigned NOT NULL auto_increment,
`branch_name` varchar(100) NOT NULL,
`branch_description` text,
`company_id` int(8) unsigned NOT NULL,
PRIMARY KEY (`branch_id`),
UNIQUE KEY `branch_name_unq` (`branch_name`,`company_id`),
KEY `company_id` (`company_id`),
CONSTRAINT `adm_up_branch_ibfk_1` FOREIGN KEY (`company_id`) REFERENCES `adm_up_company` (`company_id`) on delete cascade
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Application:
========
CREATE TABLE `adm_up_application` (
`application_id` int(8) unsigned NOT NULL auto_increment,
`company_id` int(8) unsigned NOT NULL,
`branch_id` int(8) unsigned NOT NULL,
`application_name` varchar(100) NOT NULL,
`application_description` text,
PRIMARY KEY (`application_id`),
UNIQUE KEY `branch_name_unq` (`application_name`,`company_id`,`branch_id`),
KEY `company_id` (`company_id`),
KEY `branch_id` (`branch_id`),
CONSTRAINT `adm_up_application_ibfk_1` FOREIGN KEY (`company_id`) REFERENCES `adm_up_company` (`company_id`) on delete cascade,
CONSTRAINT `adm_up_application_ibfk_2` FOREIGN KEY (`branch_id`) REFERENCES `adm_up_branch` (`branch_id`) on delete cascade
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
User:
====
CREATE TABLE IF NOT EXISTS adm_up_user (
user_id int(8) unsigned NOT NULL auto_increment,
user_screen_name varchar(150) NOT NULL ,
user_is_super enum('Y','N') DEFAULT 'N' ,
user_password varchar(25) NOT NULL ,
user_external_code int(8) unsigned NOT NULL ,
PRIMARY KEY (user_id),
UNIQUE KEY user_ext_code_unq (user_external_code)
);
my hibernate mapping files are like:
Quote:
<hibernate-mapping>
<class name="com.kiosk.clientname.entity.AdmUpUcba" table="adm_up_ucba" catalog="kiosk_admin">
<id name="ucbaId" type="java.lang.Integer">
<column name="UCBA_id" />
<generator class="identity" />
</id>
<many-to-one name="admUpUser" class="com.kiosk.clientname.entity.AdmUpUser" fetch="select">
<column name="user_id" not-null="true" />
</many-to-one>
<many-to-one name="admUpCompany" class="com.kiosk.clientname.entity.AdmUpCompany" fetch="select">
<column name="company_id" not-null="true" />
</many-to-one>
<many-to-one name="admUpBranch" class="com.kiosk.clientname.entity.AdmUpBranch" fetch="select">
<column name="branch_id" not-null="true" />
</many-to-one>
<many-to-one name="admUpApplication" class="com.kiosk.clientname.entity.AdmUpApplication" fetch="select">
<column name="application_id" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
Code for my hibernate POJO
Code:
public class AdmUpUcba implements java.io.Serializable {
// Fields
private Integer ucbaId;
private AdmUpUser admUpUser;
private AdmUpCompany admUpCompany;
private AdmUpBranch admUpBranch;
private AdmUpApplication admUpApplication;
// Constructors
/** default constructor */
public AdmUpUcba() {
}
/** full constructor */
public AdmUpUcba(AdmUpUser admUpUser, AdmUpCompany admUpCompany,
AdmUpBranch admUpBranch, AdmUpApplication admUpApplication) {
this.admUpUser = admUpUser;
this.admUpCompany = admUpCompany;
this.admUpBranch = admUpBranch;
this.admUpApplication = admUpApplication;
}
// Property accessors
public Integer getUcbaId() {
return this.ucbaId;
}
public void setUcbaId(Integer ucbaId) {
this.ucbaId = ucbaId;
}
public AdmUpUser getAdmUpUser() {
return this.admUpUser;
}
public void setAdmUpUser(AdmUpUser admUpUser) {
this.admUpUser = admUpUser;
}
public AdmUpCompany getAdmUpCompany() {
return this.admUpCompany;
}
public void setAdmUpCompany(AdmUpCompany admUpCompany) {
this.admUpCompany = admUpCompany;
}
public AdmUpBranch getAdmUpBranch() {
return this.admUpBranch;
}
public void setAdmUpBranch(AdmUpBranch admUpBranch) {
this.admUpBranch = admUpBranch;
}
public AdmUpApplication getAdmUpApplication() {
return this.admUpApplication;
}
public void setAdmUpApplication(AdmUpApplication admUpApplication) {
this.admUpApplication = admUpApplication;
}
}
now if i want to select all the applications related to the user then how can i make it....
Code:
public List<AdmUpUcba> searchUserRelatedData(String strUserID){
Criteria criteria = getSession().createCriteria( getPersistentClass() );
----What to write here to get the names of all companies , branches, applications related to a user provided by a parameter------
return criteria.list();
}
Urgent Reply will be surely appreciated...
Thanks in advance....[/b]