-->
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.  [ 21 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: How to handle, more than one mapping files for a POJO class
PostPosted: Tue Aug 16, 2005 12:32 am 
Beginner
Beginner

Joined: Mon Jul 25, 2005 12:35 am
Posts: 24
Location: Sri Lanka
Hi,

Info:

I'm developing a web application which having more than one mapping files (xxx.hbm.xml , yyy.hbm.xml , etc...) for a perticular POJO class (say SystemUser).

All the mapping files are in the class path and listed in the hibernate.cfg.xml.

In the MySQL database having different table sets corresponding to different mapping files.

Problem:

I want to retrive data from a table corresponds to xxx.hbm.xml mapping file only.

If I write a query as follows, then Hibernate is going to retrive data from every database table which corresponds to that POJO class(i.e. SystemUser).

List list = session.createCriteria(SystemUser.class)
.add( Expression.eq("id", userId) )
.list();

But my requirement is, get the data only from a table corresponds to xxx.hbm.xml.

How can I modify the above code to fulll fill my requirement?

I would realy appreciate for any help.

Thank you.
Thilina.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 2:38 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
Try this. http://www.hibernate.org/hib_docs/v3/re ... ogrammatic


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 3:30 am 
Beginner
Beginner

Joined: Mon Jul 25, 2005 12:35 am
Posts: 24
Location: Sri Lanka
Hi,
I have tried as follows,

Configuration cfg = new Configuration()
.addResource("xxx.hbm.xml")
.addResource("yyy.hbm.xml");

SystemUser class

public class SystemUser {
private String id = "";
private String password = "";
private String name = "";
private String status = "";
private Set roles = new HashSet();

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public Set getRoles() {
return roles;
}

public void setRoles(Set roles) {
this.roles = roles;
}
}

xxx.hbm.xml mapping file

<hibernate-mapping package="beyondm.alerta.hibernate.persistent.system">

<class name="SystemUser" table="XXX_SystemUsers">
<id name="id" column="UserId" length="50"/>
<property name="password" column="Password" length="200" not-null="true" />
<property name="name" column="Name" length="200" />
<property name="status" column="Status" length="10" />
</class>
</hibernate-mapping>


yyy.hbm.xml mapping file

<hibernate-mapping package="beyondm.alerta.hibernate.persistent.system">

<class name="SystemUser" table="YYY_SystemUsers">
<id name="id" column="UserId" length="50"/>
<property name="password" column="Password" length="200" not-null="true" />
<property name="name" column="Name" length="200" />
<property name="status" column="Status" length="10" />
</class>
</hibernate-mapping>

Then, If I excecute following.......
List list = session.createCriteria(SystemUser.class)
.add( Expression.eq("id", userId) )
.list();

I can see the exceptions......

[java] 13:23:42,631 ERROR Configuration:365 - Could not compile the mapping document
[java] org.hibernate.MappingException: duplicate import: beyondm.alerta.hibernate.persistent.system.SystemUser
[java] at org.hibernate.cfg.Mappings.addImport(Mappings.java:105)
[java] at org.hibernate.cfg.HbmBinder.bindPersistentClassCommonValues(HbmBinder.java:541)
[java] at org.hibernate.cfg.HbmBinder.bindClass(HbmBinder.java:488)
[java] at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:234)
[java] at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:152)
[java] at org.hibernate.cfg.Configuration.add(Configuration.java:362)
[java] at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:400)
[java] at org.hibernate.cfg.Configuration.addResource(Configuration.java:449)



What would be the reason?

Thank you.
Thilina

[/b]


Top
 Profile  
 
 Post subject: Re: How to handle, more than one mapping files for a POJO cl
PostPosted: Tue Aug 16, 2005 4:30 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
If you
anjithalb wrote:
want to retrive data from a table corresponds to xxx.hbm.xml mapping file only


then don't pass yyy.hbm.xml to addResource .


Top
 Profile  
 
 Post subject: Why do you need to map to TWO or more tables???
PostPosted: Tue Aug 16, 2005 4:41 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:58 am
Posts: 40
Location: Singapore
Correct me if I m wrong. Why do you want Hibernate manage a single class (POJO) properties to multiple table. Remember hibernate takes class name, not table name for all its persistence related activities, though at backend it would issue proper SQL.

For eg, session.find(User.class,uid) will return WHAT???? For your mapping, Hibernate would definitely get confused as where to copy the data from. ie which table will it use for data retrieval???

Try to change your object design. It will solve many hours of unnecessary complexity as well as your energy :)

Regards,
Aru K


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 4:42 am 
Beginner
Beginner

Joined: Mon Jul 25, 2005 12:35 am
Posts: 24
Location: Sri Lanka
Hi,
Thats correct.
But my problem is some thing different. In different situations I want to access both tables(i.e Tables created in xxx.hbm.xml and yyy.hbm.xml). In perticular time I want to access tables created in xxx.hbm.xml and another time I want to access tables created in yyy.hbm.xml in same application.

Then I have to add both resources to the configuration object otherwise I'm not be able to query tables created in other mapping file (ie yyy.hbm.xml).

But If I add both resources, then I'm getting the above mentioned exceptions...

Any sugessions??

Thank you.


Top
 Profile  
 
 Post subject: Read my prev post plz
PostPosted: Tue Aug 16, 2005 4:49 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:58 am
Posts: 40
Location: Singapore
Hope you havent read my prev. post.

Problem is with your design, not with Hibernate.
If it is absolutely necessary that you need to have provision to store a single object (' s properties) into two different tables, try combining both tables into one, by having a special field 'TableName/Type'.

In your biz logic, set appropriate value for this property in your POJO and save. Thats it.


Or, if possible, give us your table structures.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 4:51 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
use Hibernate 3 and entity-name support.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 5:07 am 
Beginner
Beginner

Joined: Mon Jul 25, 2005 12:35 am
Posts: 24
Location: Sri Lanka
Hi,

My previous comment for dennisbyrne's reply.

Hi Aru K

My application having a ASP (Application service provider) model. This sysetm having 2 modules ie. admin modules and the merchant module (for both modules having different GUIs). Administrator of the admin module can be able to create merchants. In this case we design the system so that, having seperate batabase tale sets for different merchants.

For examlple say we have 2 merchants, McD and SIA and having 2 DB table sets.
For McD have tables such as McD_Merchantoperator, McD_MerchantTranscation, etc..

For SIA have tables such as SIA_Merchantoperator, SIA_MerchantTranscation, etc..

Coloums and contents of the set of table are same but only the table names are different.

For this perpose I'm dynamically creating McD.hbm.xml and SIA.hbm.xml files and populate the database tables. Thats why I need 2 or more mapping files, hence it having 2 table sets for a single POJO class.

Note:
1) These merchant specific tables may contains huge amount of entries.
2) If we use Single database table set for all the merchants, then the number of entries in the tables become huge hence the qurying speed become less. If we use seperate database table sets for different merchants, then the quering speed become high and hence the application getting speed.


I think you may understand my question?
What would you think about it?
any sugessions?

Thank you.
Thilina.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 5:54 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:58 am
Posts: 40
Location: Singapore
For the reasons you stated, yes, your design is correct.

Why dont you have have seperate set of classes much like what you have for DB?

It wont take much time for you to have another set of classes as you can simply copy all your codes into two seperate folders say X and Y.

All you may need to do is changing the package name in your hibernate mapping file.

In your code (Biz and/or service Obj code), you can simply make use of appropriate class.

One obvious disadvantage is having same set of code. But as you know well, your class files wont consume much resource.

Give it a thought!!!

Regards,
Aru K


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 6:01 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:58 am
Posts: 40
Location: Singapore
For the following line

<---
For this perpose I'm dynamically creating McD.hbm.xml and SIA.hbm.xml files and populate the database tables. Thats why I need 2 or more mapping files, hence it having 2 table sets for a single POJO class.

--->

Remember, we configure SessionFactory only once in an application. Though your application supports different types of clients, you would need to get services from single (Web/Appln) server where your code resides.

During this phase, as you know well, it reads all hbm files and constructs factory for all. Indeed, it is during this time (when you try to build session factory by calling configure().buildSessionFactory()), you would get the error message from Hibernate.

I dont know if there is a way to get around in Hibernate 3.

As you construct hbm files dynamically, keeping two different sets of same code (might be bit un-convincing one) in two diff. package would solve the problem.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 6:06 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:58 am
Posts: 40
Location: Singapore
I think I got it very wrong......

You are going to have 'n' sets of tables for n number of Merchants. Right????

Let me think abt it !!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 6:07 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
solutions:

Either one of the following will work:

- dont do it - have separate classes for separate tables
- dont load them into the same configuration
- if none of above works, use entity-name in H3

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 6:17 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:58 am
Posts: 40
Location: Singapore
I think you need to have n number of configurations (eventually n number of SessionFactory(ies)), as Max has hinted!!!

Try a simple example before implementing!!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 6:45 am 
Beginner
Beginner

Joined: Mon Jul 25, 2005 12:35 am
Posts: 24
Location: Sri Lanka
Hi,

arumugam_kasi and max,
Thanks a lot for your comments regarding this matter and I really appreciate it.

I use 2 merchants (McD and SIA) as an example, but may have any number of merchants.
So...

---------- dont do it - have separate classes for separate tables
Having seperate classes is imposible because application may have any number of merchants and ignored this approach.

----------- dont load them into the same configuration
If I load all mapping files to same configuratin, then it will gives exceptions (MappingException) then.. this approach also ignored.

-------- if none of above works, use entity-name in H3
I'm working with H3 and now I'm trying with entity-name in H3. Hope it will work.

Thank you.
Thilina.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 21 posts ]  Go to page 1, 2  Next

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.