Hi,
thank in advance for your attention ^^
I have this mapped superclass, DataImpl
Code:
@MappedSuperclass
@Proxy(lazy=true) //useless
public abstract class DataImpl extends WebsiteModelAbstractIdentifiable implements Data {
private Map<String,Object> key2data;
public DataImpl() {
init();
}
...
@CollectionOfElements(targetElement=ByteArrayBlobType.class)
@Cascade(value = {CascadeType.ALL,CascadeType.DELETE_ORPHAN})
@JoinTable(
name="data",
joinColumns = @JoinColumn(name="entityid")
)
@MapKey(
columns = {@Column(name="keytodata")}
)
@Column(name="data")
@LazyCollection(LazyCollectionOption.TRUE)
@Fetch(FetchMode.SUBSELECT)
private Map<String, Byte[]> getSerializedMap(){
Map <String, Byte[]> result = new HashMap<String, Byte[]>();
if(key2data != null ) {
for( String key : key2data.keySet()){
result.put(key, HibernateUtils.objectToByte( key2data.get(key) ));
}
}
return result;
}
private void setSerializedMap(Map<String,Byte[]> serializedMap){
this.key2data = new HashMap<String, Object>(serializedMap.size());
for( String key : serializedMap.keySet()){
key2data.put(key, HibernateUtils.byteToObject( serializedMap.get(key) ));
}
}
...
}
As you can see, I have mapped a Map<String,Object> into a Map<String,Byte[]>. In this way I can store some usefull Java Object into the db ( in table data with a INT entityId, String keytodata, BLOB data ).
Every class of my application extend DataImpl and everything works fine.
My only problem is the lazyness of the CollectionOfElements.
I have two class who extends DataImpl, PageClassImpl and PageImpl
PAGECLASSIMPL
Code:
@Entity
@Table(name="pageclass")
@Proxy(lazy=true)
public class PageClassImpl extends DataImpl implements PageClass, Identifiable {
private Set<Page> pages;
....
protected PageClassImpl() {
init();
}
....
@OneToMany(targetEntity=PageImpl.class)
@JoinTable(
name="pageclasspage",
joinColumns=@JoinColumn(name="pageclass"),
inverseJoinColumns=@JoinColumn(name="page")
)
@LazyCollection(LazyCollectionOption.TRUE)
@Fetch(FetchMode.SELECT)
private Set<Page> getPages() {
return pages;
}
....
}
PAGEIMPL
Code:
@Entity
@Table(name="page")
@Proxy(lazy=true)
public class PageImpl extends DataImpl implements Page, Data {
protected URI pageUri;
protected Set<RequestCollection> requestCollectionSet;
protected PageImpl(){};
//MGC
public PageImpl(int id, URI uri, Set<RequestCollection> rc) {
super(id);
init(uri, rc);
}
...
}
In few word i have a crawler. everytime i found a new htmlpage, I save the html content in this way
page.setData("htmlpage", HTMLCONTENT);
When I save the page with session.save(page) this is the correct result
Code:
...
Hibernate: insert into page (uri, id) values (?, ?)
Hibernate: insert into data (entityid, keytodata, data) values (?, ?, ?)
...
Every page belongs to one pageclass. So when I ask to hibernate to retrieve the pageclass pages with pageclass.getPages() this is the result
Code:
Hibernate: select pages0_.pageclass as pageclass1_, pages0_.page as page1_, pageimpl1_.id as id2_0_, pageimpl1_.uri as uri2_0_ from pageclasspage pages0_ left outer join page pageimpl1_ on pages0_.page=pageimpl1_.id where pages0_.pageclass=?
Hibernate: select serialized0_.entityid as entityid0_, serialized0_.data as data0_, serialized0_.keytodata as keytodata0_ from data serialized0_ where serialized0_.entityid=?
Hibernate: select serialized0_.entityid as entityid0_, serialized0_.data as data0_, serialized0_.keytodata as keytodata0_ from data serialized0_ where serialized0_.entityid=?
Hibernate: select serialized0_.entityid as entityid0_, serialized0_.data as data0_, serialized0_.keytodata as keytodata0_ from data serialized0_ where serialized0_.entityid=?
Hibernate: select serialized0_.entityid as entityid0_, serialized0_.data as data0_, serialized0_.keytodata as keytodata0_ from data serialized0_ where serialized0_.entityid=?
Hibernate: select serialized0_.entityid as entityid0_, serialized0_.data as data0_, serialized0_.keytodata as keytodata0_ from data serialized0_ where serialized0_.entityid=?
Hibernate: select serialized0_.entityid as entityid0_, serialized0_.data as data0_, serialized0_.keytodata as keytodata0_ from data serialized0_ where serialized0_.entityid=?
...
for every page i stored
As you can see, hibernate retrieve every page of the pageclass, but also the page htmlcontent that i've never asked. Something is wrong with the lazyness of the @CollectionofElements in DataImpl.