-->
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: ManyToOne on existing entity
PostPosted: Thu Nov 11, 2010 7:57 am 
Newbie

Joined: Fri Jan 26, 2007 9:58 am
Posts: 18
Location: Dublin
Hi All,

Have a static table of content types with data similar to below:

content_type, description
application/XML, XML Document
image/jpeg, JPeg Image

Described below as:

Code:
@Entity
@org.hibernate.annotations.Entity(mutable = false)
@org.hibernate.annotations.AccessType("field")
@Table(name = "CONTENT_TYPE")
public class ContentType {

   @Id
   @Column(name = "content_type", updatable = false, insertable = false)
   private String contentType;

   @Column(name = "description", nullable = false, updatable = false, insertable = false)
   private String description;

   ContentType () {
   }

   public ContentType (final String contentType) {
      super();
      this.contentType = contentType;
   }
        /* etc */

I have another table that needs to link to this one via a OneToOne/ManyToOne association.
Code:
public class Document {
   /**
    * @return the contentType
    */
   @ManyToOne(fetch = FetchType.EAGER, optional = true)
   @JoinColumn(name = "content_type")
   @org.hibernate.annotations.Fetch(FetchMode.SELECT)
   public ontentType getContentType() {
      return contentType;
   }
}


I want to be able to create a Document with a content type like this:

Code:

                final Document doc = new Document ();
      doc.setName("AI 1");
      doc.setContentType(new ContentType("application/xml"));
           documentDao.saveOrUpdate(ai);



This all works fine for creation, but if I then try and get the description of the content type in the same transaction it's returning null, I'm assuming it's because the "new ContentType("application/xml")" is now the cached representation of the ContentType. The only way I can then get the description field to become populated is to use:

Code:
      sessionFactory.getCurrentSession().flush();
      sessionFactory.getCurrentSession().evict(doc);



Which seems like a lot of hard work, I also know that using sessionFactory.getCurrentSession().load instead of new ContentType will fix this but neither solution is ideal.

I suspect this is a result of the hibernate session cache and can't be bypassed but I'd assumed this would be a fairly standard thing to do and am surprised it's not working using the lazy loading of Hibernate.

Many thanks for any help,

Chris.


Top
 Profile  
 
 Post subject: Re: ManyToOne on existing entity
PostPosted: Thu Nov 18, 2010 11:32 am 
Newbie

Joined: Fri Jan 26, 2007 9:58 am
Posts: 18
Location: Dublin
Anyone have any ideas on this? didn't think it was something that unusual?

Cheers!

Chris.


Top
 Profile  
 
 Post subject: Re: ManyToOne on existing entity
PostPosted: Thu Nov 18, 2010 2:38 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
session.refresh(contentType) ?

but personally I would prefer session.load()/get() instead of new ContentType(), because you may run into problems if you have to create multiple documents with the same ContentType in the same session. For a given session, Hibernate can only have a single object representing the same entity so you can't just do new ContentType() multiple times.


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.