2

我对这个问题搜索了太多,但我无法解决。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 });

应用程序.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>

休眠.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>

固件.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

2 回答 2

2

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

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

我意识到这并没有真正的帮助(没关系,你可以投反对票)我真的很抱歉,但我无法从我以前的项目中获得有效的休眠映射......

于 2013-06-25T09:23:20.667 回答
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

于 2013-06-25T16:36:44.703 回答