Hello, thank you for your response.
My generated code is:
DAO:Code:
public class ArtistDAOBase implements DaoElement
{
private Transaction tx;
private final SessionFactory sessionFactory = getSessionFactory();
public SessionFactory getSessionFactory()
{
logInfo.info("[Class Artist] | getSessionFactory() : Getting new session factory");
try
{
Configuration configuration = new Configuration();
configuration.configure().setProperty("hibernate.show_sql", "false");
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
logDebug.debug("[Class Artist] | getSessionFactory() : New session factory created successfully");
return (configuration.buildSessionFactory(serviceRegistry));
}
catch (Exception e)
{
logError.error("[Class Artist] | getSessionFactory() : Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException("Could not locate SessionFactory in JNDI");
}
}
protected void startOperation()
{
tx = sessionFactory.getCurrentSession().beginTransaction();
logDebug.debug("[Class Artist] | startOperation() : New transaction started from current session.");
}
/**
* Ends a transaction
*/
public void endOperation()
{
tx.commit();
logDebug.debug("[Class Artist] | endOperation() : Transaction commited, session ended.");
}
public List<Artist> findAll(int maxResults, int firstRow)
{
logInfo.info("[Class Artist] | findAll(int maxResults, int firstRow) : Getting all Artist instances");
try
{
startOperation();
Query query = sessionFactory.getCurrentSession().createQuery("from " + "Artist");
query.setMaxResults(maxResults);
query.setFirstResult(firstRow);
List<Artist> ret = (List<Artist>)query.list();
endOperation();
logDebug.debug("[Class Artist] | findAll(int maxResults, int firstRow) : Get all Artist instances success");
return ret;
}
catch (RuntimeException re)
{
logError.error("[Class Artist] | findAll(int maxResults, int firstRow) : Get all Artist instances exception failure", re);
throw re;
}
}
}
Bean Artist:Code:
import java.util.HashSet;
import java.util.Set;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonElement;
public class Artist implements java.io.Serializable, DboBean
{
private Long id;
private String name;
private Set<Item> items = new HashSet<Item>(0);
public Artist() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set<Item> getItems() {
return this.items;
}
public void setItems(Set<Item> items) {
this.items = items;
}
}
An artist is bound to one or more items, that's why the artist bean has an attribute items of type set.
Bean Item:Code:
public class Item implements java.io.Serializable, DboBean
{
private Long id;
private Artist artist;
private int kind;
private String name;
public Item() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public Artist getArtist() {
return this.artist;
}
public void setArtist(Artist artist) {
this.artist = artist;
}
public int getKind() {
return this.kind;
}
public void setKind(int kind) {
this.kind = kind;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
When I try to fetch an item from an artist I always get a Lazy initialization error since my session has been closed by the DAO in the method endoperation() with tx.commit. My sessions are always bound to transactions. I have generated this code with Hibernate tools and I am not sure if it is correct. My question is, would it be better to use opensession() instead of getcurrentsession()? Is it a good practice to manage sessions and transactions from the DAO or is it better to create a session manager? Should I leave the session open when I start group of database operations? When and where should I close the session? In my case I would like to encapsulate all the session and transaction management in my infrastructure layer, so I would no have to manage it in my domain layer, though this approach would limit the funtionality of my layer.
Many thanks.
Thank you for your help.