2

I searched for this problem too much, but I couldn't solve. I'm getting org.hibernate.LazyInitializationException: could not initialize proxy - no Session exception, even though I tried to initialize firma.getSektor(). I couldn't see any relationship or annotation problem in Sektor.java or Firma.java. Please help me.

Edit: I don't have a positive look on using Eagle Loading to solve this problem because of it might decrease peformance.

Exception:

Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
at org.hibernate.Hibernate.initialize(Hibernate.java:414)
at CRMFunctions.CRMFunctions.TableDoldur(CRMFunctions.java:804)
at app.FirmaYonetim.Firmalar.<init>(Firmalar.java:138)
... 43 more

My Code:

public static ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
public static FirmaHome FIRMA_HOME = (FirmaHome) context.getBean("firmaHome");


Vector<Vector<?>> firmaData = new Vector<Vector<?>>();
        List<Firma> firmas = FIRMA_HOME.findAll();
        if (firmas.size() > 0) {
            for (Firma firma : firmas) {
                Vector vector = new Vector();
                vector.add(firma.getFirmaId());
                vector.add(firma.getFirmaUnvan());
                vector.add(firma.getFirmaVergiDairesi());
                vector.add(firma.getFirmaVergiNo());
                Hibernate.initialize(firma.getSektor());
                vector.add(firma.getSektor().getSektorAd());
                firmaData.add(vector);
            }   
        }               
        return new CRMTable(firmaData, Arrays.asList("Firma ID", "Unvan", "Vergi Dairesi", "Vergi No" ,"Sektor"), 1, new Boolean[] { false, false,
            false, false, false });

application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id="ilHome" class="dto.IlHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="anlasmaHome" class="dto.AnlasmaHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="bankaHome" class="dto.BankaHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="depoUrunHome" class="dto.DepoUrunHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="duyuruHome" class="dto.DuyuruHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="duyuruAliciHome" class="dto.DuyuruAliciHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="firmaHome" class="dto.FirmaHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="gonderiHome" class="dto.GonderiHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="gonderiDurumHome" class="dto.GonderiDurumHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="hastalikRaporHome" class="dto.HastalikRaporHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="ilceHome" class="dto.IlceHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="kampanyaHome" class="dto.KampanyaHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="kaynakHome" class="dto.KaynakHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="mahalleHome" class="dto.MahalleHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="markaHome" class="dto.MarkaHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="meslekHome" class="dto.MeslekHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="musteriHome" class="dto.MusteriHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="musteriAdresHome" class="dto.MusteriAdresHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="musteriKaraListeHome" class="dto.MusteriKaraListeHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="musteriMailHome" class="dto.MusteriMailHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="musteriTelefonHome" class="dto.MusteriTelefonHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="musteriWebHome" class="dto.MusteriWebHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="musteriNotHome" class="dto.MusteriNotHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="musteriSikayetHome" class="dto.MusteriSikayetHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="musteriTakipHome" class="dto.MusteriTakipHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="personelHome" class="dto.PersonelHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="personelAdresHome" class="dto.PersonelAdresHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="personelBankaHesapHome" class="dto.PersonelBankaHesapHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="personelEskiFirmaHome" class="dto.PersonelEskiFirmaHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="personelHastalikHome" class="dto.PersonelHastalikHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="personelIzinHome" class="dto.PersonelIzinHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="personelKaraListeHome" class="dto.PersonelKaraListeHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="personelMailHome" class="dto.PersonelMailHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="personelTelefonHome" class="dto.PersonelTelefonHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="PersonelSozlesmeHome" class="dto.PersonelSozlesmeHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="personelWebHome" class="dto.PersonelWebHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="personelYoklamaHome" class="dto.PersonelYoklamaHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="postaKoduHome" class="dto.PostaKoduHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="satisHome" class="dto.SatisHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="sektorHome" class="dto.SektorHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="semtHome" class="dto.SemtHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="subeHome" class="dto.SubeHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="subeAdresHome" class="dto.SubeAdresHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="subeMailHome" class="dto.SubeMailHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="subeTelefonHome" class="dto.SubeTelefonHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="subeWebHome" class="dto.SubeWebHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>


<bean id="ulkeHome" class="dto.UlkeHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>


<bean id="urunHome" class="dto.UrunHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="urunBarkodHome" class="dto.UrunBarkodHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="urunGrupHome" class="dto.UrunGrupHome">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">
        <value>hibernate.cfg.xml</value>
    </property>
</bean>

<aop:config>
    <aop:pointcut id="serviceOperation" expression="execution(* dto..*Home.*(..))" />
    <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice" />
</aop:config>

<tx:advice id="txAdvice">
    <tx:attributes>
        <tx:method name="get*" read-only="true" />
        <tx:method name="*" />
    </tx:attributes>
</tx:advice>
<!-- enable the configuration of transactional behavior based on annotations 
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />-->

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
    <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
    <property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="hibernate.connection.characterEncoding">utf8</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">****</property>
    <property name="hibernate.connection.url">jdbc:mysql://192.168.1.110:3306/HalfaCrmDb</property>
    <property name="hibernate.connection.username">***</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    <property name="hibernate.search.autoregister_listeners">false</property>
    <property name="hibernate.show_sql">true</property>          
    <mapping class="dto.Urun" resource="dto/Urun.hbm.xml"/>
    <mapping class="dto.PersonelKaraListe" resource="dto/PersonelKaraListe.hbm.xml" />
    <mapping class="dto.PersonelMail" resource="dto/PersonelMail.hbm.xml" />
    <mapping class="dto.DuyuruAlici" resource="dto/DuyuruAlici.hbm.xml" />
    <mapping class="dto.Musteri" resource="dto/Musteri.hbm.xml" />
    <mapping class="dto.Semt" resource="dto/Semt.hbm.xml" />
    <mapping class="dto.Ulke" resource="dto/Ulke.hbm.xml" />
    <mapping class="dto.MusteriTakip" resource="dto/MusteriTakip.hbm.xml" />
    <mapping class="dto.SubeMail" resource="dto/SubeMail.hbm.xml" />
    <mapping class="dto.MusteriTelefon" resource="dto/MusteriTelefon.hbm.xml" />
    <mapping class="dto.DepoUrun" resource="dto/DepoUrun.hbm.xml" />
    <mapping class="dto.Il" resource="dto/Il.hbm.xml" />
    <mapping class="dto.Kampanya" resource="dto/Kampanya.hbm.xml" />
    <mapping class="dto.PersonelWeb" resource="dto/PersonelWeb.hbm.xml" />
    <mapping class="dto.MusteriKaraListe" resource="dto/MusteriKaraListe.hbm.xml" />
    <mapping class="dto.PostaKodu" resource="dto/PostaKodu.hbm.xml" />
    <mapping class="dto.Marka" resource="dto/Marka.hbm.xml" />
    <mapping class="dto.UrunGrup" resource="dto/UrunGrup.hbm.xml" />
    <mapping class="dto.PersonelTelefon" resource="dto/PersonelTelefon.hbm.xml" />
    <mapping class="dto.Mahalle" resource="dto/Mahalle.hbm.xml" />
    <mapping class="dto.Duyuru" resource="dto/Duyuru.hbm.xml" />
    <mapping class="dto.Firma" resource="dto/Firma.hbm.xml" />
    <mapping class="dto.PersonelYoklama" resource="dto/PersonelYoklama.hbm.xml" />
    <mapping class="dto.SatisUrun" resource="dto/SatisUrun.hbm.xml" />
    <mapping class="dto.Kaynak" resource="dto/Kaynak.hbm.xml" />
    <mapping class="dto.Anlasma" resource="dto/Anlasma.hbm.xml" />
    <mapping class="dto.MusteriNot" resource="dto/MusteriNot.hbm.xml" />
    <mapping class="dto.Ilce" resource="dto/Ilce.hbm.xml" />
    <mapping class="dto.PersonelBankaHesap" resource="dto/PersonelBankaHesap.hbm.xml" />
    <mapping class="dto.PersonelHastalik" resource="dto/PersonelHastalik.hbm.xml" />
    <mapping class="dto.MusteriSikayet" resource="dto/MusteriSikayet.hbm.xml" />
    <mapping class="dto.Gonderi" resource="dto/Gonderi.hbm.xml" />
    <mapping class="dto.PersonelAdres" resource="dto/PersonelAdres.hbm.xml" />
    <mapping class="dto.SubeTelefon" resource="dto/SubeTelefon.hbm.xml" />
    <mapping class="dto.MusteriMail" resource="dto/MusteriMail.hbm.xml" />
    <mapping class="dto.GonderiDurum" resource="dto/GonderiDurum.hbm.xml" />
    <mapping class="dto.HastalikRapor" resource="dto/HastalikRapor.hbm.xml" />
    <mapping class="dto.UrunBarkod" resource="dto/UrunBarkod.hbm.xml" />
    <mapping class="dto.Meslek" resource="dto/Meslek.hbm.xml" />
    <mapping class="dto.Personel" resource="dto/Personel.hbm.xml" />
    <mapping class="dto.Sektor" resource="dto/Sektor.hbm.xml" />
    <mapping class="dto.PersonelSozlesme" resource="dto/PersonelSozlesme.hbm.xml" />
    <mapping class="dto.MusteriAdres" resource="dto/MusteriAdres.hbm.xml" />
    <mapping class="dto.SubeAdres" resource="dto/SubeAdres.hbm.xml" />
    <mapping class="dto.SubeWeb" resource="dto/SubeWeb.hbm.xml" />
    <mapping class="dto.PersonelEskiFirma" resource="dto/PersonelEskiFirma.hbm.xml" />
    <mapping class="dto.Satis" resource="dto/Satis.hbm.xml" />
    <mapping class="dto.Banka" resource="dto/Banka.hbm.xml" />
    <mapping class="dto.PersonelIzin" resource="dto/PersonelIzin.hbm.xml" />
    <mapping class="dto.MusteriWeb" resource="dto/MusteriWeb.hbm.xml" />
    <mapping class="dto.Sube" resource="dto/Sube.hbm.xml" />
</session-factory>

Firma.hbm.xml

<hibernate-mapping>
    <class name="dto.Firma" table="Firma" catalog="HalfaCrmDb">
        <id name="firmaId" type="java.lang.Integer">
            <column name="firmaId" />
            <generator class="identity" />
        </id>
        <many-to-one name="sektor" class="dto.Sektor" fetch="select">
            <column name="firmaSektorId">
                <comment>Sektor TBL'den ID gelecek</comment>
            </column>
        </many-to-one>
        <property name="firmaUnvan" type="string">
            <column name="firmaUnvan" />
        </property>
        <property name="firmaVergiDairesi" type="string">
            <column name="firmaVergiDairesi" />
        </property>
        <property name="firmaVergiNo" type="string">
            <column name="firmaVergiNo" length="11" />
        </property>
        <set name="subes" table="Sube" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="subeFirmaId">
                    <comment>Firma TBL'den ID gelecek</comment>
                </column>
            </key>
            <one-to-many class="dto.Sube" />
        </set>
        <set name="anlasmas" table="Anlasma" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="anlasmaFirmaId">
                    <comment>Firma TBL'den ID gelecek</comment>
                </column>
            </key>
            <one-to-many class="dto.Anlasma" />
        </set>
        <set name="markas" table="Marka" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="markaFirmaId">
                    <comment>Firma TBL'den ID Gelecek</comment>
                </column>
            </key>
            <one-to-many class="dto.Marka" />
        </set>
    </class>
</hibernate-mapping>

Sektor.hbm.xml

<hibernate-mapping>
<class name="dto.Sektor" table="Sektor" catalog="HalfaCrmDb">
    <id name="sektorId" type="java.lang.Integer">
        <column name="sektorId" />
        <generator class="identity" />
    </id>
    <property name="sektorAd" type="string">
        <column name="sektorAd" length="50" />
    </property>
    <set name="mesleks" table="Meslek" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="meslekSektorId">
                <comment>Sektor TBL'den ID gelecek</comment>
            </column>
        </key>
        <one-to-many class="dto.Meslek" />
    </set>
    <set name="firmas" table="Firma" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="firmaSektorId">
                <comment>Sektor TBL'den ID gelecek</comment>
            </column>
        </key>
        <one-to-many class="dto.Firma" />
    </set>
</class>

Sektor.java

@Entity
@Table(name = "Sektor", catalog = "HalfaCrmDb")
public class Sektor implements java.io.Serializable {

private static final long serialVersionUID = 1370608783666448790L;
private Integer sektorId;
private String sektorAd;
private Set<Meslek> mesleks = new HashSet<Meslek>(0);
private Set<Firma> firmas = new HashSet<Firma>(0);

public Sektor() {
}

public Sektor(String sektorAd, Set<Meslek> mesleks, Set<Firma> firmas) {
    this.sektorAd = sektorAd;
    this.mesleks = mesleks;
    this.firmas = firmas;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "sektorId", unique = true, nullable = false)
public Integer getSektorId() {
    return this.sektorId;
}

public void setSektorId(Integer sektorId) {
    this.sektorId = sektorId;
}

@Column(name = "sektorAd", length = 50)
public String getSektorAd() {
    return this.sektorAd;
}

public void setSektorAd(String sektorAd) {
    this.sektorAd = sektorAd;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "sektor")
public Set<Meslek> getMesleks() {
    return this.mesleks;
}

public void setMesleks(Set<Meslek> mesleks) {
    this.mesleks = mesleks;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "sektor")
public Set<Firma> getFirmas() {
    return this.firmas;
}

public void setFirmas(Set<Firma> firmas) {
    this.firmas = firmas;
}
}

Firma.java

    @Entity
    @Table(name = "Firma", catalog = "HalfaCrmDb")
public class Firma implements java.io.Serializable {        
    private static final long serialVersionUID = -4310046345444125265L;
    private Integer firmaId;
    private Sektor sektor;
    private String firmaUnvan;
    private String firmaVergiDairesi;
    private String firmaVergiNo;
    private Set<Sube> subes = new HashSet<Sube>(0);
    private Set<Anlasma> anlasmas = new HashSet<Anlasma>(0);
    private Set<Marka> markas = new HashSet<Marka>(0);

    public Firma() {
    }

    public Firma(Sektor sektor, String firmaUnvan, String firmaVergiDairesi,
            String firmaVergiNo, Set<Sube> subes, Set<Anlasma> anlasmas, Set<Marka> markas) {
        this.sektor = sektor;
        this.firmaUnvan = firmaUnvan;
        this.firmaVergiDairesi = firmaVergiDairesi;
        this.firmaVergiNo = firmaVergiNo;
        this.subes = subes;
        this.anlasmas = anlasmas;
        this.markas = markas;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "firmaId", unique = true, nullable = false)
    public Integer getFirmaId() {
        return this.firmaId;
    }

    public void setFirmaId(Integer firmaId) {
        this.firmaId = firmaId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "firmaSektorId")
    public Sektor getSektor() {
        return this.sektor;
    }

    public void setSektor(Sektor sektor) {
        this.sektor = sektor;
    }

    @Column(name = "firmaUnvan")
    public String getFirmaUnvan() {
        return this.firmaUnvan;
    }

    public void setFirmaUnvan(String firmaUnvan) {
        this.firmaUnvan = firmaUnvan;
    }

    @Column(name = "firmaVergiDairesi")
    public String getFirmaVergiDairesi() {
        return this.firmaVergiDairesi;
    }

    public void setFirmaVergiDairesi(String firmaVergiDairesi) {
        this.firmaVergiDairesi = firmaVergiDairesi;
    }

    @Column(name = "firmaVergiNo", length = 11)
    public String getFirmaVergiNo() {
        return this.firmaVergiNo;
    }

    public void setFirmaVergiNo(String firmaVergiNo) {
        this.firmaVergiNo = firmaVergiNo;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "firma")
    public Set<Sube> getSubes() {
        return this.subes;
    }

    public void setSubes(Set<Sube> subes) {
        this.subes = subes;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "firma")
    public Set<Anlasma> getAnlasmas() {
        return this.anlasmas;
    }

    public void setAnlasmas(Set<Anlasma> anlasmas) {
        this.anlasmas = anlasmas;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "firma")
    public Set<Marka> getMarkas() {
        return this.markas;
    }

    public void setMarkas(Set<Marka> markas) {
        this.markas = markas;
}

GenericDao.java

public interface GenericDao<E, PK extends Serializable> 
{
PK save(E newInstance);

void update(E transientObject);

void saveOrUpdate(E transientObject);

void delete(E persistentObject);

E findById(PK id);

List<E> findAll();

List<E> findAllByProperty(String propertyName, Object value);
}

GenericDaoImpl.java

    public abstract class GenericDaoImpl<E, PK extends Serializable> extends HibernateDaoSupport implements GenericDao<E, PK>
{


@SuppressWarnings("unchecked")
public PK save(E newInstance) {
    return (PK) getHibernateTemplate().save(newInstance);
}

public E findById(PK id) {
    return (E) getHibernateTemplate().get(getEntityClass(), id);
}

@SuppressWarnings("unchecked")
public List<E> findAll() {
    return getHibernateTemplate().findByCriteria(createDetachedCriteria());
}

@SuppressWarnings("unchecked")
public List<E> findAllByProperty(String propertyName, Object value) {
    DetachedCriteria criteria = createDetachedCriteria();
    criteria.add(Restrictions.eq(propertyName, value));
    return getHibernateTemplate().findByCriteria(criteria);
}

@SuppressWarnings("unchecked")
public List<E> findByExample(E object) {
    List<E> resultList = getHibernateTemplate().findByExample(object, 0, 1);
    return resultList;
}

@SuppressWarnings("unchecked")
public List<E> findByExample(E object, int firstResult, int maxResults) {
    List<E> resultList = getHibernateTemplate().findByExample(object,
            firstResult, maxResults);
    return resultList;
}

public void update(E transientObject) {
    getHibernateTemplate().update(transientObject);
}

public void saveOrUpdate(E transientObject) {
    getHibernateTemplate().saveOrUpdate(transientObject);
}

public void delete(E persistentObject) {
    getHibernateTemplate().delete(persistentObject);
}

protected abstract Class<E> getEntityClass();

protected DetachedCriteria createDetachedCriteria() {
    return DetachedCriteria.forClass(getEntityClass());
}
}

FirmaHome.java

public class FirmaHome extends GenericDaoImpl<Firma, Integer> {

@Override
protected Class<Firma> getEntityClass() {
    return Firma.class;
}
}
4
  • Is there anybody to help me? Commented Jun 24, 2013 at 19:16
  • can you edit the question and post the code of FIRMA_HOME.findAll()? Commented Jun 24, 2013 at 19:41
  • I did it. Also I posted 'application.xml' and 'hibernate.cfg.xml'. Commented Jun 25, 2013 at 8:25
  • GenericDao, GenericaoImpl and FırmaHome codes are posted. I think there is no absent now. Please help me. Commented Jun 25, 2013 at 8:40

2 Answers 2

2

Ok I didn't use Hibernate for quite a few months now and I can't test anything right now, so my answer will probably not be really helpful but hey maybe I will give you a hint :

Last time I had this error it was because I was trying to get data with lazy loading AFTER the transaction was closed. I had a method with the @Transactional annotation (so a transaction was opened then closed before and after the method) and somehow I was trying to load my data after that transactional method. But it was with Spring framework so I have no idea how works with a plain hibernate... Either you find a way around in your code to avoid this, or you can just avoid lazy loading and try an EAGER FetchType in your @OneToMany.

I realize this isn't really helpful (it's ok you can downvote guys) I'm really sorry but I just can't get my hand on a working hibernate mapping from my previous projects...

Sign up to request clarification or add additional context in comments.

13 Comments

Where should I put @Transactional annotation?
Sorry I should have specified it earlier : I was using Hibernate (implementing JPA) with the Spring framework (this annotation is from Spring). I have never encountered this "could not initialize proxy" error with a good old hibernate mapping file so I can only give you the theory. But the fact remains : you are trying to load a lazy method after your transaction has been closed.
Can you tell me how to load the lazy method before transaction closed?
First could you tell us which method does this exactly ? The exception doesn't show a specific line. If you don't know which one you will have to debug it step by step.
CRMFunctions.java Line: 804 -> Hibernate.initialize(firma.getSektor()); Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167) at org.hibernate.Hibernate.initialize(Hibernate.java:414) at CRMFunctions.CRMFunctions.TableDoldur(CRMFunctions.java:804) at app.FirmaYonetim.Firmalar.<init>(Firmalar.java:138) ... 43 more
|
0

The session is configured to be started automatically inside any service method by the configuration <tx:method name="*" />

The issue is that you are invoking the Hibernate.initialize outside a service method which doesn't have a session anymore.

My suggestion for you is to create a method inside the FirmaHome service to return the Vector of firmas. Example:

    public Vector<Vector<?>> loadFirmas(){ 
            Vector<Vector<?>> firmaData = new Vector<Vector<?>>();
            List<Firma> firmas = FIRMA_HOME.findAll();
            if (firmas.size() > 0) {
                for (Firma firma : firmas) {
                    Vector vector = new Vector();
                    vector.add(firma.getFirmaId());
                    vector.add(firma.getFirmaUnvan());
                    vector.add(firma.getFirmaVergiDairesi());
                    vector.add(firma.getFirmaVergiNo());
                    Hibernate.initialize(firma.getSektor());
                    vector.add(firma.getSektor().getSektorAd());
                    firmaData.add(vector);
                }   
            }     
    }

ps: I am not sure, but I think that by moving this method to a service you wont need to invoke the Hibernate.initialize

1 Comment

Now, I'm traying to iplement google generic dao. I gave up using previous old dao. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.