-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Hibernate Reading Data Problems...
PostPosted: Sun Sep 28, 2008 1:34 pm 
Newbie

Joined: Sun Sep 28, 2008 1:18 pm
Posts: 4
Location: Pakistan
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]

_________________
Wasif A Kirmani-Parisa


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 29, 2008 4:35 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
First, create a query on AdmUpUcba.
Then, create a subcriteria on user

Code:
Criteria crit2=criteria.createCriteria("admUpUser", "user");


Then, restrict by the user property you want (name?)

Code:
crit2.add(Restrictions.eq("name", yourParameterValue ));


This would return you only valid users, witht the upper "Adm ..." thing unfiltered. So, before asking for the results, apply a "transformation"

Code:
  crit2.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);


Finally, ask for the results (of Adm ... things), and ask for specific properties of if

Code:
List<AdmUpUcba> admList=crit2.list();
for (AdmUpUcba adm: admList){
  logger.info("the user is " + adm.getUser().getName());
  logger.info("the company is " + adm.AdmUpCompany .getName());
  ...
}

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 05, 2008 10:35 pm 
Newbie

Joined: Sun Sep 28, 2008 1:18 pm
Posts: 4
Location: Pakistan
Thanks....But if i do solve it via projection then will it effect on the performance...

_________________
Wasif A Kirmani-Parisa


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.