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.  [ 5 posts ] 
Author Message
 Post subject: 1 Mapping file and 1 Java class for more than one table
PostPosted: Fri Sep 19, 2008 9:16 am 
Newbie

Joined: Thu Sep 18, 2008 10:46 am
Posts: 3
Hi all, is it possible to use a mapping file (and the related Java class) to access more than one table? For instance, is it possible to dynamically change the "class" attribute in the mapping file?

Thanks,
Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 19, 2008 12:02 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 11:52 am
Posts: 43
I take it that you don't mean a Java class that is mapped to a join of two tables, which is quite possible. If you mean changing the mapping at runtime, that might be possible. Explain you situation a bit more.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 22, 2008 1:25 am 
Newbie

Joined: Thu Sep 18, 2008 10:46 am
Posts: 3
lkline wrote:
I take it that you don't mean a Java class that is mapped to a join of two tables, which is quite possible. If you mean changing the mapping at runtime, that might be possible. Explain you situation a bit more.


I have more or less 350 different tables... but all of them have a column named ID. I'm developing a tool for determining the most appropriate ID for each table according to some predefined rules.

For instance, I don't want to have 350 identical mapping files and as many identical Java classes just because I'm accessing 350 tables. One mapping file and one Java class should be fine for such a task.

Talk 2 u soon,
Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 22, 2008 12:57 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 11:52 am
Posts: 43
Just because all of the tables have a column named ID does not mean one mapping file can be used for all of them. I assume each table has a slightly different set of columns, otherwise why not put all the data in one table.

If you want to use Hibernate in Java, and you will access more than one of the tables you will need to create a mapping for each table that you want to access.

For instance, let's say you have 5 tables, A,B,C,D,and E. Each one has a column called ID and also has other columns, let's say A has a1, a2, a3; B has b1, b2, b3, etc. Now if you want to access table A from Java you need to create a mapping file (or use annotations) that map the columns in A to instance variables in a Java class. Same for B, C, D, and E. Each table will need its own mapping file and corresponding Java class.

There are some tools that might allow you to reverse engineer your database schema into a set of mapping files and Java objects. I haven't used them.

Feel free to give more details if what I said does not really capture your issue.


Top
 Profile  
 
 Post subject: Would entity-name work?
PostPosted: Mon Sep 22, 2008 1:12 pm 
Newbie

Joined: Mon Sep 22, 2008 9:31 am
Posts: 9
I take it you would be determining at runtime within the JRE which mapping to call right? If that is the case could you use entity-name's?

For Example:
Code:
<class entity-name="CaseA" table="TableA">
...
</class>

<class entity-name="CaseB" table="TableB">
...
</class>


And your Java code:
Code:
String entityMapping = null;
ClassToPersist ctp = new ClassToPersist();

...
//Business code to determine what entityMapping should be set to (i.e. CaseA or CaseB)
...

session.startTransaction();
session.save(entityMapping, ctp);
...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.