org.hibernate.LazyInitializationException:无法初始化代理 - 没有Session | DAO

时间:2013-06-24 18:42:05

标签: spring hibernate lazy-loading

我搜索过这个问题太多,但我无法解决。我得到org.hibernate.LazyInitializationException: could not initialize proxy - no Session 异常,即使我试图初始化firma.getSektor()。 我在Sektor.java或Firma.java中看不到任何关系或注释问题。 请帮我。

编辑:我对使用Eagle Loading解决此问题没有积极的看法,因为它可能会降低性能。

例外:

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

我的代码:

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;
}
}

2 个答案:

答案 0 :(得分:2)

好的,我现在已经使用Hibernate几个月了,我现在无法测试任何东西,所以我的回答可能不会真的有用,但是嘿,也许我会给你一个提示:

上次出现此错误是因为我在交易结束后试图通过延迟加载获取数据。我有一个带有@Transactional注释的方法(所以一个事务被打开然后在方法之前和之后关闭)并且不知何故我试图在该事务方法之后加载我的数据。但它是使用Spring框架所以我不知道如何使用普通的hibernate ...要么你在代码中找到了解决方法以避免这种情况,或者你可以避免延迟加载并在@OneToMany中尝试EAGER FetchType。

我意识到这不是真的很有帮助(你可以帮助你们)我真的很抱歉,但是我无法从我之前的项目中得到一个有效的hibernate映射......

答案 1 :(得分:0)

会话配置为通过配置<tx:method name="*" />

在任何服务方法中自动启动

问题是您在服务方法之外调用Hibernate.initialize,而该服务方法不再有会话。

我的建议是在FirmaHome服务中创建一个方法来返回Vector个。例如:

    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:我不确定,但我认为通过将此方法移动到服务,您不需要调用Hibernate.initialize