-->
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: Custom Tuplizer or use Transient annontation
PostPosted: Tue Aug 30, 2011 4:13 pm 
Beginner
Beginner

Joined: Sun Oct 09, 2005 3:21 pm
Posts: 40
Using Hibernate 3.6

Have 2 entities Element and Relation. In the Element mapping (XML mapping):
Code:
<list name="sourceRelations" lazy="true" inverse="true" ...>
     <key column="SOURCEID" on-delete="noaction" not-null="true" />
     <index column="RELATIONID" />
     <one-to-many class="com.klistret.cmdb.ci.pojo.Relation" />
</list>


and another List property called "destinationRelations". Element thus has a collection where it is the owner of a Relation and a collection where it is the target of a Relation. The Relation has:
Code:
<many-to-one lazy="false" name="source" class="com.klistret.cmdb.ci.pojo.Element" foreign-key="FK_REL_SOURCE" index="SOURCE_REL_FK">
     <column name="SOURCEID" sql-type="INTEGER" not-null="true" />
</many-to-one>

<many-to-one lazy="false" name="destination" class="com.klistret.cmdb.ci.pojo.Element" foreign-key="FK_REL_DEST" index="DEST_REL_FK">
     <column name="DESTINATIONID" sql-type="INTEGER" not-null="true" />
</many-to-one>


I do not want these properties to be editable or a part of the proxied object marshalled outside the session. Not sure what is easiest? To try using annotations and the Transient annotation to mark the sourceRelations/destinationRelations as transient (quoting the docs "a method annotated as @Transient, and will be ignored by the entity manager"). Haven't tested since I am using JAXB generated POJOs. Or writing a custom tuplizer and removing these properties from the ProxyFactory after the PF is created.

Basically the size of the Relation collections for an Element can be really big. Only reason they are mapped is to allow Criterias to make joins between Elements and Relations. Otherwise I never want a caller to be able to get/set these properties on an Element.

Any ideas? Assuming that if I am not using Hibernate annotations then the javax.persistent.Transient annotation won't be processed (ie. using mapping XML configuration means Transient annotation in the underlying entity classes won't be read).


Top
 Profile  
 
 Post subject: Re: Custom Tuplizer or use Transient annontation
PostPosted: Sun Sep 04, 2011 7:40 am 
Beginner
Beginner

Joined: Sun Oct 09, 2005 3:21 pm
Posts: 40
The Transient annotation (without heavy testing) appears to be recognized only with JPA/Hibernate annotations loaded through the AnnotationSessionFactoryBean via Spring. Works great eliminating the collection properties (source/destination lists) from the entity manager but it also wipes them entirely from the Hibernate metadata which breaks the whole idea of still being able to write queries against these properties.

Haven't written a tuplizer yet. Wondering if anybody else has faced the same issue?


Top
 Profile  
 
 Post subject: Written a collection-type and gave a stab at a Tuplizer
PostPosted: Wed Nov 09, 2011 12:40 pm 
Beginner
Beginner

Joined: Sun Oct 09, 2005 3:21 pm
Posts: 40
After reading through http://www.theserverside.com/news/1363571/Remote-Lazy-Loading-in-Hibernate I wrote a collection-type extension specific for lists:
Code:
public class MyCollectionType implements UserCollectionType {
  public PersistentCollection instantiate(SessionImplementor session,
  CollectionPersister persister) throws HibernateException {
    return new MyPersistentList(session);
  }

  public PersistentCollection wrap(SessionImplementor session,
         Object collection) {
    return new MyPersistentList(session, (List<?>) collection);
  }

  ....

  public Object replaceElements(Object original, Object target,
         CollectionPersister persister, Object owner,
         @SuppressWarnings("rawtypes") Map copyCache,
         SessionImplementor session) throws HibernateException {
    return original;
  }

  public Object instantiate(int anticipatedSize) {
    return new ArrayList<Object>();
  }
}

public class MyPersistentList implements PersistentCollection, List {
  ...

  public MyPersistentList (SessionImplementor session) {
   this.session = session;
  }

  public MyPersistentList (SessionImplementor session, List<?> list) {
    this(session);
    this.list = new ArrayList<Object>();
  }

  ...

  public Object getValue() {
    return new ArrayList<Object>();
  }

  public void setSnapshot(Serializable key, String role, Serializable snapshot) {
    this.key = key;
    this.role = role;
    this.storedSnapshot = snapshot;
  }

  public boolean isDirty() {
    return false;
  }

  ....
}


There are bunch of methods I see getting called that I have overrided like unsetSession (same code as AbstractPersistentCollection), wasInitialized (returns false always), toArray (empty ArrayList like getValue), and clearDirty which does nothing in my code. Whether or not certain methods are called depends of course on the action (get, update, select, delete so forth). Have most of the CRUD flows and the code works. But it feels like putting a shoe on the wrong foot. And my code doesn't fit into annotations (there is no annotation for collection-type, addition is still pending as of 3.4 Hibernate Core) and the implementation of the PersistentCollection basically just removes the "dirty" controls.

I tried building a Tuplizer but the coding needed isn't straight forward for me. Building a HibernateProxy isn't problem it is using Javassisst to "ignore" method calls on the lists. Would have been cool if the JavassistLazyInitializer had a way to plug in over method filters like the one for "finalizer".

Suggestions?


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.