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: The Sessions's EntityPersister & IdentifierGenerator
PostPosted: Thu Aug 20, 2009 6:45 pm 
Newbie

Joined: Thu Jul 23, 2009 11:48 am
Posts: 9
I'm building a Hibernate (3.4.0.GA) web-app using Mystic Paste as a template (the source is available at http://kenai.com/projects/mystic-apps ). The Mystic Paste objects save fine, but my objects do not. I've discovered that the problem occurs in org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(). The problem is at lines 129-131, which say
Code:
129. else if ( generatedId == IdentifierGeneratorFactory.POST_INSERT_INDICATOR ) {
130.   return performSave( entity, null, persister, true, anything, source, requiresImmediateIdAccess );
131. }
The Mystic Paste objects have generatedId == IdentifierGeneratorFactory.POST_INSERT_INDICATOR, but my objects just have a generatedId of some Long sequence value. The generatedId variable comes from lines 121-122, which read
Code:
121. EntityPersister persister = source.getEntityPersister( entityName, entity );
122. Serializable generatedId = persister.getIdentifierGenerator().generate( source, entity );
The "source" variable here refers to the session and the "entity" variable is the object to be saved. When I step through the code, both my object and the MysticPaste object return a SingleTableEntityPersister at line 121. However, at line 122 the Mystic Paste object returns an IdentityGenerator while my object returns a SequenceGenerator. This seems to be the problem, since the IdentityGenerator's generate() function returns POST_INSERT_INDICATOR, while the SequenceGenerator's generate() just returns a Long.

So my question is, where are these Generators set? And how can I get the session for my object to have an IdentityGenerator here? The session factory definitions for the Mystic Paste object and for my object are below. As far as I can tell, the only difference between the two is that the Mystic Paste object is using an HSQL database while mine is using an Oracle database.
Code:
<!-- Mystic Paste Hibernate session factory -->
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>

                <prop key="hibernate.connection.pool_size">10</prop>
                <prop key="hibernate.jdbc.batch_size">1000</prop>
                <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
            </props>
        </property>
    </bean>
   
    <!-- My Hibernate session factory -->
    <bean id="sessionFactoryOracle"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSourceOracle"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>

                <prop key="hibernate.connection.pool_size">10</prop>
                <prop key="hibernate.jdbc.batch_size">1000</prop>
                <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>

            </props>
        </property>
    </bean>


Top
 Profile  
 
 Post subject: Re: The Sessions's EntityPersister & IdentifierGenerator
PostPosted: Thu Aug 20, 2009 7:43 pm 
Newbie

Joined: Thu Jul 23, 2009 11:48 am
Posts: 9
Looking deeper at Hibernate's source code, I see that the IdentifierGenerator does not come from the session, but from the EntityMetamodel for the object being persisted. So perhaps the issue comes from how the objects are mapped. Here's the code for both the Mystic Paste object (PasteItem.java) and my object (Referral.java)

Code:
/**
* @author <a href="mailto:gcastro@mysticcoders.com">Guillermo Castro</a>
* @version $Revision$ $Date$
*/
@Entity
@Table(name = "PASTE_ITEMS")
@NamedQueries({@NamedQuery(name = "item.getById",
                query = "from PasteItem item where item.id = :id"),
        @NamedQuery(name = "item.find",
                query = "from PasteItem item where item.isPrivate <> true AND item.abuseFlag <> true and item.content is not null order by item.timestamp desc"),
        @NamedQuery(name = "item.findThreaded",
                query = "from PasteItem item where item.isPrivate <> true AND item.abuseFlag <> true and item.content is not null and item.parent is null order by item.timestamp desc"),
        @NamedQuery(name = "item.findByLanguage",
                query = "from PasteItem item where item.isPrivate <> true AND item.abuseFlag <> true and item.content is not null and item.type = :type order by item.timestamp desc"),
        @NamedQuery(name = "item.findByLanguageThreaded",
                query = "from PasteItem item where item.isPrivate <> true AND item.abuseFlag <> true and item.content is not null and item.parent is null and item.type = :type order by item.timestamp desc"),
        @NamedQuery(name = "item.findByToken",
                query = "from PasteItem item where item.privateToken = :token"),
        @NamedQuery(name = "item.findByUser",
                query = "from PasteItem item where item.isPrivate <> true AND item.abuseFlag <> true and item.content is not null and item.userToken = :token"),
        @NamedQuery(name = "item.count",
                query = "select count(item) from PasteItem item where item.isPrivate <> true AND item.abuseFlag <> true")
})

public class PasteItem implements Serializable {
    private static final long serialVersionUID = -6467870857777145137L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ITEM_ID")
    protected long id;

    @Lob
    @Column(name = "CONTENT")
    protected String content;

    @Lob
    @Column(name = "HTML_CONTENT")
    protected String formatedContent;

    @Basic
    @Column(name = "LANG_TYPE_CD")
    protected String type;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATE_TS")
    protected Date timestamp;

    @Basic
    @Column(name = "USER_TOKEN")
    protected String userToken;


    @Basic
    @Column(name = "ABUSE_FLAG")
    protected boolean abuseFlag;

//    @Basic
//    @Column(name = "CLIENT_TOKEN")
//    protected String clientToken;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CLIENT_ID")
    protected Client client;

    @Basic
    @Column(name = "PRIVATE_FLG")
    protected boolean isPrivate;

    @Basic
    @Column(name = "PRIVATE_TOKEN", unique = true, updatable = false)
    protected String privateToken;

    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @JoinColumn(name = "PARENT_ITEM_ID", nullable = true)
    protected PasteItem parent;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
    protected List<PasteItem> children;

    // Lots of unannotated getters and setters...
}


Code:
@Entity
@Table(name = "zjp_referrals")
public class Referral implements Serializable {
   
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name="referral_id")
   private long id;
   
   @ManyToOne
   @JoinColumn(name="jpu_jobp_user_id")
   private BasicUser user;
   
   @ManyToOne
   @JoinColumn(name="job_job_id")
   private Job job;
   
   Referral() {
   
   }
   
   public Referral(BasicUser user, Job job) {
      this.user = user;
      this.job = job;
   }

    // unannotated getters and setters...
}


Top
 Profile  
 
 Post subject: Re: The Sessions's EntityPersister & IdentifierGenerator
PostPosted: Thu Aug 20, 2009 9:05 pm 
Newbie

Joined: Thu Jul 23, 2009 11:48 am
Posts: 9
Never mind, the answer was very simple. I had forgotten to use @Transactional.


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.