-->
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.  [ 1 post ] 
Author Message
 Post subject: How to override LAZY loading after session was closed?
PostPosted: Tue Dec 23, 2014 6:01 am 
Newbie

Joined: Tue Dec 23, 2014 5:12 am
Posts: 1
I work with hibernate and try to optimize loading foreign entities annotated with

Code:
@ManyToOne(fetch = FetchType.LAZY)


I don't want to retrieve foreign entity during hibernate query and use LAZY fetch type. Later(after session already closed), I want to get that foreign entity but use tool that different from hibernate (another cached DAO (GuavaCache) that already stores foreign entity).

Of couse, immediately I have got an LazyInitializationException.

I can't replace @ManyToOne annotation with @Transient because of toooooo much legacy HQL code witch does not works after deleting @MabyToOne.

Somewere somebody adviced to make getter method final and do not access to entity field straightly, but just use getter. Here is an example :

Code:
    private int foreignId;

@Basic
@Column(name = "foregn_id")
public int getForeignId() { return foreignId;}
public void setForeignId(int id) { this.foreignId = id; }


// private DBForeignEntity foreignEntity;  no more sense to have this field

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "foregn_id", referencedColumnName = "id", nullable = false, insertable = false, updatable = false)
public final DBJavaModulesEntity getProtocolJavaModules() {
    // return foreignEntity; deprecated usage! Hibernate will create proxy object and throw LazyInitException after session was closed
    return getFromCache(getForeignId());
}

public void setForeign(DBForeignEntity foreignEntity) {
    // this.foreignEntity = foreignEntity;   no more sence for having setter at all
}


this ugly solution exludes any abilities to persist nested entities, because of no setter for foreign entity anymore!

Is there another solution to deprecate Hibernate to create a proxy object for my entity? How to avoid LazyInitializationException if session was closed? Is there any bad consequences of non-proxing in this case?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.