-->
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.  [ 2 posts ] 
Author Message
 Post subject: Strategy for manually maintaining a relation
PostPosted: Tue Nov 03, 2009 6:08 am 
Beginner
Beginner

Joined: Fri Nov 14, 2008 7:11 am
Posts: 31
We have the following problem with manually administering a relation in our domain model.

The application knows devices and users who are responsible for the devices. In fact the domain model is much more complex, but we can focus on these domain objects for now. One user is responsible for n devices, so we have an 1:n relation. A device has the attribute deviceId (a serial number of the device).

The relevant mappings look like:

Code:
public class User {

   @ManyToOne
   public Device getDevice() {
      return device;
   }

   @CollectionOfElements(fetch = FetchType.EAGER)
   @JoinTable(name = "USER_DEVICEID", joinColumns = @JoinColumn(name = "User_id"))
   @Column(name = "deviceId")
   public Set<String> getDeviceIds() {
      return deviceIds;
   }
}

public class Device {
   private String deviceId;
}


Sometimes the application gets a request (via a Servlet) with a deviceId and has to figure out which user is responsible for the device with that deviceId. Because of some security reasons (that would be to difficult to explain here) we are not allowed to access the table Device in that situation. Therefore we cannot look it up like findDeviceByDeviceId().
We are only allowed to access the table User and the table User_DeviceId which we added to get a mapping from User to deviceId without having to use the table Device.

That works fine, but now we are responsible to maintain this "relation table" User_DeviceId whenever a device is created, updated or deleted and we are looking for the best strategy of doing that. We wrote a Hibernate Interceptor, but that doesn't seem to work, as the interceptor has to change a user object (add or remove a deviceId) whenever a device object is saved/updated/deleted.

We had implemented it like

Code:
public class MyInterceptor extends EmptyInterceptor {

   @Override
   public boolean onSave(final Object entity, final Serializable id, final Object[] state, final String[] propertyNames, final Type[] types) {
      if (entity instanceof Device) {
         return addDeviceIdToUser((Device) entity);
      } else {
         return super.onSave(entity, id, state, propertyNames, types);
      }
   }

   @Override
   public boolean onFlushDirty(final Object entity, final Serializable id, final Object[] currentState, final Object[] previousState, final String[] propertyNames, final Type[] types) {
      ...
   }

   @Override
   public void onDelete(final Object entity, final Serializable id, final Object[] state, final String[] propertyNames, final Type[] types) {
      ...
   }


   private boolean addDeviceIdToUser(final Device device) {
      final User user = device.getDeviceType().getMandator().getUser();
      user.addDeviceId(device.getDeviceId());
      return false;
   }


but that cannot work correctly. I think you should not use hibernate interceptors to change another object than "entity" and you are only allowed to make your changes to the "state" object array, aren't you?

Does anybody knows a better strategy to manually maintaining the relation from User to DeviceId when a Device is changed? Any hints would be greatly appreciated.

Thanks in advance,
Ole


Top
 Profile  
 
 Post subject: Re: Strategy for manually maintaining a relation
PostPosted: Tue Nov 03, 2009 11:36 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
how about an entity listener which waits for prePersist and preUpdate events on Device. There you could get another Session and update the user manually.

_________________
-----------------
Need advanced help? http://www.viada.eu


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