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.