-->
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.  [ 6 posts ] 
Author Message
 Post subject: Can I map more than one class to the same DB tabl
PostPosted: Wed Aug 10, 2005 5:30 pm 
Newbie

Joined: Fri Jul 29, 2005 11:35 am
Posts: 1
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.0

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:MySQL 3.1.8

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

In the Hibernate documentation it is implied that a table will be mapped to by only one class. Is it possible to have one table mapped to by more than one class? If so are there any locking implications?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 6:30 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
Yep, can map a table to more than one class using entity-name.

example

<class name="Foo" entity-name="Foo" table="FOO">
<id name="id"/>
<property name="name"/>
<property name="notes"/>
</class>

<class name="Foo" entity-name="FooSummary">
<id name="id"/>
<property name="name"/>
</class>

Note that for relationships you will need to specify entity-name in addition to class name.


Top
 Profile  
 
 Post subject: Re: Can I map more than one class to the same DB tabl
PostPosted: Wed Aug 10, 2005 7:41 pm 
Beginner
Beginner

Joined: Thu Feb 17, 2005 9:20 pm
Posts: 36
Location: Vancouver, WA
bridget.mertens wrote:
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.0

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:MySQL 3.1.8

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

In the Hibernate documentation it is implied that a table will be mapped to by only one class. Is it possible to have one table mapped to by more than one class? If so are there any locking implications?


This is simplified mapping from our current project it is working just fine. About locking, so far we had no issues, problem or any kind of restrictions and etc ....

Map1:
Code:
<class name="Account" table="account">
        <id name="accountId" column="ACCOUNT_ID" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <property name="balance" column="BALANCE" />
</class>



Map2:
Code:


<class name="SearchAccount" table="account">

  <id name="accountId" column="ACCOUNT_ID" type="java.lang.Integer">
  <generator class="native"/>
</id>

  <property name="balance" column="BALANCE" />

  <one-to-one name="account" class="AjDataAccount" />

<set name="customers" lazy="true" order-by="ACCOUNT_ID">
  <key column="ACCOUNT_ID" />
  <one-to-many class="customer" />
</set>

</class>



We used such an approach in few areas of our app. Actually everywhere where that was used one class was user for read-only data other for all of the rest. We have some cases where we had implemented more then two classes mapped on the same table for different buisiness logic to improve performance. Be careful to use that it can cause code maintenance issues later.

_________________
Vasyl Zhabko


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 12:00 am 
Newbie

Joined: Thu Jun 09, 2005 11:11 pm
Posts: 5
Be carefull with last mapping. There are some problems if you try to save both objects with same id in one transaction using merge().


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 10:38 am 
Beginner
Beginner

Joined: Thu Feb 17, 2005 9:20 pm
Posts: 36
Location: Vancouver, WA
melkiy wrote:
Be carefull with last mapping. There are some problems if you try to save both objects with same id in one transaction using merge().


As I mentioned above we are using second class only in read-only mode.

_________________
Vasyl Zhabko


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 11:21 am 
Regular
Regular

Joined: Tue Mar 01, 2005 2:35 pm
Posts: 60
Basilio wrote:
melkiy wrote:
Be carefull with last mapping. There are some problems if you try to save both objects with same id in one transaction using merge().


As I mentioned above we are using second class only in read-only mode.


Then you should set mutable="false".


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