-->
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: Mapping several "unrelated" classes to single tabl
PostPosted: Tue Aug 14, 2007 5:06 pm 
Newbie

Joined: Tue Jul 13, 2004 10:00 pm
Posts: 3
I'm trying to simulate 'views' of a single table (row) with several different objects, but am unclear on the possibility and implications of doing so.

Here is an example:

Code:
@Entity
@Table(name="device")
public class DeviceBase
{
    @Id
    @Column(name="device_id")
    private int deviceId;

    @Column(name="ip_address")
    private String ipAddress;

    // more attributes...
}

@Entity
@Table(name="device")
public class DeviceStatus
{
    @Id
    @Column(name="device_id")
    private int deviceId;

    @Column(name="status")
    private int status;
}

@Entity
@Table(name="device")
public class DeviceInfo
{
    @Id
    @Column(name="device_id")
    private int deviceId;

    @Column(name="ipAddress")
    private String ipAddress;

    @Column(name="vendor")
    private String vendor;

    @Column(name="model")
    private String model;
}


Several things to note are, 1) the common "deviceId" across all three classes, and 2) the visibility of "ipAddress" from both DeviceBase and DeviceInfo.

First, will Hibernate allow me to do this? Next, if so are there implications for cache coherency if updates are performed through these different objects? For example, if I use the DeviceBase object above to update the ipAddress attribute, will queries for a DeviceInfo object with the same deviceId (they are the same row) receive a possibly stale object due to caching? Is caching my only concern?

Inheritance doesn't make sense in this case because they are not separate kinds of objects (i.e. a discriminator would interfere with seeing the same row through different classes). For performance reasons we would like to avoid using separate tables and joins, hence our consideration of this approach.

Any help/insight is appreciated.

Thanks,
Brett


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 15, 2007 8:47 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Sounds to me like you should have 1 class: Device with all the fields. Or can some devices have a status but not a vendor and model?

What are you trying to achieve by having separate classes? After you've loaded a device you could narrow the API using interfaces.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 5:21 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
It does appear that you _can_do this in hibernate. With the session cache, if you update, say, the ipAddress through DeviceInfo then load a DeviceBase for the same object in the same session then you'll get the old ipAddr in DeviceBase. However, if you flush the changes to DeviceInfo before loading DeviceBase then you'll see the updated ipAddress.


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.