Hi
I'm new to Hibernate and are having some problem. I have been looking for a solution all over this webpage but I guess I'm really stupied (because the solution is so easy) or blind *smile* So now I hop anyone of you all can help me.
The problem is that I have two mapping files working towards the same table but with different scope (that is in what information the object contains).
The session.load always return one of the object types but never the other one. If I remove all the calles to one of the object the other is returned.
Well let me show you the exception (i.e. the debug log), the mapping and finally the code.
Ahh we are talking about Hibernate 2.1.3 and a Oracle 8.1.7 DB
Thank you for your time and help!
Cheers Christian
(Hibernate Debug log)
Code:
09:01:25,911 INFO Environment:462 - Hibernate 2.1.3
09:01:25,931 INFO Environment:496 - loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver, hibernate.cglib.use_reflection_optimizer=true, hibernate.cache.provider_class=net.sf.ehcache.hibernate.Provider, hibernate.cache.use_query_cache=true, hibernate.max_fetch_depth=1, hibernate.dialect=net.sf.hibernate.dialect.OracleDialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.jdbc.batch_size=0, hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N', hibernate.proxool.pool_alias=pool1, hibernate.connection.username=utv, hibernate.cache.region_prefix=hibernate.test, hibernate.connection.url=jdbc:oracle:thin:@felix.lg.posten.se:1521:dev817, hibernate.show_sql=true, hibernate.connection.password=utv, hibernate.connection.pool_size=1}
09:01:25,991 INFO Environment:518 - using java.io streams to persist binary types
09:01:26,001 INFO Environment:519 - using CGLIB reflection optimizer
09:01:26,011 INFO Configuration:872 - configuring from resource: /hibernate.cfg.xml
09:01:26,061 INFO Configuration:844 - Configuration resource: /hibernate.cfg.xml
09:01:26,772 INFO Configuration:328 - Mapping resource: hbm-xml-Filer/Land.hbm.xml
09:01:27,022 INFO Binder:229 - Mapping class: se.posten.logistik.applikation.hibernate.Land -> land
09:01:27,163 INFO Configuration:328 - Mapping resource: hbm-xml-Filer/Keyword.hbm.xml
09:01:27,223 INFO Binder:229 - Mapping class: se.posten.logistik.applikation.hibernate.Keyword -> keywords
09:01:27,223 INFO Configuration:328 - Mapping resource: hbm-xml-Filer/KundTest.hbm.xml
09:01:27,303 INFO Binder:229 - Mapping class: se.posten.logistik.applikation.hibernate.KundTest -> LGAB_KUND
09:01:27,603 INFO Configuration:328 - Mapping resource: hbm-xml-Filer/Kund.hbm.xml
09:01:27,723 INFO Binder:229 - Mapping class: se.posten.logistik.applikation.hibernate.Kund -> LGAB_KUND
09:01:27,733 INFO Configuration:328 - Mapping resource: hbm-xml-Filer/Adress.hbm.xml
09:01:27,794 INFO Binder:229 - Mapping class: se.posten.logistik.applikation.hibernate.Adress -> LGAB_ADRESS
09:01:27,794 INFO Configuration:328 - Mapping resource: hbm-xml-Filer/AdressTest.hbm.xml
09:01:27,874 INFO Binder:229 - Mapping class: se.posten.logistik.applikation.hibernate.AdressTest -> LGAB_ADRESS
09:01:27,974 INFO Configuration:1030 - Configured SessionFactory: null
09:01:27,974 INFO Configuration:613 - processing one-to-many association mappings
09:01:27,984 INFO Binder:1168 - Mapping collection: se.posten.logistik.applikation.hibernate.AdressTest.MyKund -> LGAB_KUND
09:01:27,984 INFO Configuration:622 - processing one-to-one association property references
09:01:27,984 INFO Configuration:647 - processing foreign key constraints
09:01:28,044 INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.OracleDialect
09:01:28,054 INFO SettingsFactory:58 - Maximim outer join fetch depth: 1
09:01:28,054 INFO SettingsFactory:62 - Use outer join fetching: true
09:01:28,064 INFO DriverManagerConnectionProvider:42 - Using Hibernate built-in connection pool (not for production use!)
09:01:28,064 INFO DriverManagerConnectionProvider:43 - Hibernate connection pool size: 1
09:01:28,064 INFO DriverManagerConnectionProvider:77 - using driver: oracle.jdbc.driver.OracleDriver at URL: jdbc:oracle:thin:@felix.lg.posten.se:1521:dev817
09:01:28,064 INFO DriverManagerConnectionProvider:78 - connection properties: {user=utv, password=utv}
09:01:28,074 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
09:01:34,113 INFO SettingsFactory:102 - Use scrollable result sets: true
09:01:34,113 INFO SettingsFactory:105 - Use JDBC3 getGeneratedKeys(): false
09:01:34,113 INFO SettingsFactory:108 - Optimize cache for minimal puts: false
09:01:34,113 INFO SettingsFactory:114 - echoing all SQL to stdout
09:01:34,113 INFO SettingsFactory:117 - Query language substitutions: {no='N', true=1, yes='Y', false=0}
09:01:34,153 INFO SettingsFactory:128 - cache provider: net.sf.ehcache.hibernate.Provider
09:01:34,153 INFO Configuration:1093 - instantiating and configuring caches
09:01:34,203 INFO SessionFactoryImpl:119 - building session factory
09:01:34,824 INFO SessionFactoryObjectFactory:82 - no JNDI name configured
09:01:34,834 INFO UpdateTimestampsCache:35 - starting update timestamps cache at region: net.sf.hibernate.cache.UpdateTimestampsCache
09:01:34,864 WARN Configurator:123 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/java/hibernate-2.1/lib/ehcache-0.7.jar!/ehcache-failsafe.xml
09:01:34,884 WARN Plugin:95 - Could not find configuration for net.sf.hibernate.cache.UpdateTimestampsCache. Configuring using the defaultCache settings.
09:01:34,924 INFO QueryCache:39 - starting query cache at region: net.sf.hibernate.cache.QueryCache
09:01:34,924 WARN Plugin:95 - Could not find configuration for net.sf.hibernate.cache.QueryCache. Configuring using the defaultCache settings.
09:01:35,024 DEBUG SQL:237 - select kund0_.KUNDNR as KUNDNR0_, kund0_.KUNDNAMN as KUNDNAMN0_, kund0_.KONCERN as KONCERN0_, kund0_.KUNDNR_GL as KUNDNR_GL0_, kund0_.ORGNR as ORGNR0_, kund0_.KREDSPARRJN as KREDSPAR8_0_, kund0_.FAKTSPECUTS as FAKTSPEC9_0_, kund0_.INTRID as INTRID0_, kund0_.KUNDREFKRAV as KUNDREF10_0_, kund0_.KUNDINTJN as KUNDINTJN0_, kund0_.BETVILLKOR as BETVILLKOR0_, kund0_.FAKTSELKOD as FAKTSELKOD0_, kund0_.KREDITGRANS as KREDITG14_0_, kund0_.KREDSPARRTXT as KREDSPA15_0_, kund0_.SAKERHETSTYP as SAKERHE16_0_, kund0_.SAKERHETSBEL as SAKERHE17_0_, kund0_.FAKTAVGKOD as FAKTAVGKOD0_, kund0_.DATUMKREDGR as DATUMKR19_0_, kund0_.DATUMKREDSP as DATUMKR20_0_, kund0_.AUTOGIRO as AUTOGIRO0_, kund0_.KREDTILLKOD as KREDTIL22_0_, kund0_.DATUMKUNDREG as DATUMKU23_0_, kund0_.DATUMKUNDAND as DATUMKU24_0_, kund0_.DATUMKUNDBRT as DATUMKU25_0_, kund0_.BETKUNDNR as BETKUNDNR0_, kund0_.SARFAKTJN as SARFAKTJN0_, kund0_.KLKOD as KLKOD0_, kund0_.SNIKOD as SNIKOD0_, kund0_.ANTANSTKOD as ANTANSTKOD0_, kund0_.OMSKOD as OMSKOD0_, kund0_.PARID as PARID0_, kund0_.KRAVKOD as KRAVKOD0_, kund0_.KONCERNNR as KONCERNNR0_, kund0_.KREDAVTJN as KREDAVTJN0_, kund0_.VATNR as VATNR0_, kund0_.FSPECINTKOD as FSPECIN37_0_, kund0_.KUNDKATKOD as KUNDKATKOD0_, kund0_.DATAKORNKOD as DATAKOR39_0_, kund0_.UTSKICKJN as UTSKICKJN0_, kund0_.KUNDNAMN_V as KUNDNAMN_V0_, kund0_.FOLJSEDJN as FOLJSEDJN0_, kund0_.TAXERINGSBARJN as TAXERING4_0_, kund0_.SAPKUNDNR as SAPKUNDNR0_ from LGAB_KUND kund0_ where kund0_.KUNDNR=?
Hibernate: select kund0_.KUNDNR as KUNDNR0_, kund0_.KUNDNAMN as KUNDNAMN0_, kund0_.KONCERN as KONCERN0_, kund0_.KUNDNR_GL as KUNDNR_GL0_, kund0_.ORGNR as ORGNR0_, kund0_.KREDSPARRJN as KREDSPAR8_0_, kund0_.FAKTSPECUTS as FAKTSPEC9_0_, kund0_.INTRID as INTRID0_, kund0_.KUNDREFKRAV as KUNDREF10_0_, kund0_.KUNDINTJN as KUNDINTJN0_, kund0_.BETVILLKOR as BETVILLKOR0_, kund0_.FAKTSELKOD as FAKTSELKOD0_, kund0_.KREDITGRANS as KREDITG14_0_, kund0_.KREDSPARRTXT as KREDSPA15_0_, kund0_.SAKERHETSTYP as SAKERHE16_0_, kund0_.SAKERHETSBEL as SAKERHE17_0_, kund0_.FAKTAVGKOD as FAKTAVGKOD0_, kund0_.DATUMKREDGR as DATUMKR19_0_, kund0_.DATUMKREDSP as DATUMKR20_0_, kund0_.AUTOGIRO as AUTOGIRO0_, kund0_.KREDTILLKOD as KREDTIL22_0_, kund0_.DATUMKUNDREG as DATUMKU23_0_, kund0_.DATUMKUNDAND as DATUMKU24_0_, kund0_.DATUMKUNDBRT as DATUMKU25_0_, kund0_.BETKUNDNR as BETKUNDNR0_, kund0_.SARFAKTJN as SARFAKTJN0_, kund0_.KLKOD as KLKOD0_, kund0_.SNIKOD as SNIKOD0_, kund0_.ANTANSTKOD as ANTANSTKOD0_, kund0_.OMSKOD as OMSKOD0_, kund0_.PARID as PARID0_, kund0_.KRAVKOD as KRAVKOD0_, kund0_.KONCERNNR as KONCERNNR0_, kund0_.KREDAVTJN as KREDAVTJN0_, kund0_.VATNR as VATNR0_, kund0_.FSPECINTKOD as FSPECIN37_0_, kund0_.KUNDKATKOD as KUNDKATKOD0_, kund0_.DATAKORNKOD as DATAKOR39_0_, kund0_.UTSKICKJN as UTSKICKJN0_, kund0_.KUNDNAMN_V as KUNDNAMN_V0_, kund0_.FOLJSEDJN as FOLJSEDJN0_, kund0_.TAXERINGSBARJN as TAXERING4_0_, kund0_.SAPKUNDNR as SAPKUNDNR0_ from LGAB_KUND kund0_ where kund0_.KUNDNR=?
Kund: VA TEKNIK
IntrId: 168424
09:01:36,666 DEBUG SQL:237 - select adress0_.INTRID as INTRID0_, adress0_.ADRTYP as ADRTYP0_, adress0_.DATUMFOM as DATUMFOM0_, adress0_.DATUMTOM as DATUMTOM0_, adress0_.ATTENTION as ATTENTION0_, adress0_.COADR as COADR0_, adress0_.GATABOX as GATABOX0_, adress0_.POSTNR as POSTNR0_, adress0_.POSTORT as POSTORT0_, adress0_.LANDKOD as LANDKOD0_, adress0_.ADRESS_UTL as ADRESS_UTL0_, adress0_.ADRID as ADRID0_ from LGAB_ADRESS adress0_ where adress0_.INTRID=?
Hibernate: select adress0_.INTRID as INTRID0_, adress0_.ADRTYP as ADRTYP0_, adress0_.DATUMFOM as DATUMFOM0_, adress0_.DATUMTOM as DATUMTOM0_, adress0_.ATTENTION as ATTENTION0_, adress0_.COADR as COADR0_, adress0_.GATABOX as GATABOX0_, adress0_.POSTNR as POSTNR0_, adress0_.POSTORT as POSTORT0_, adress0_.LANDKOD as LANDKOD0_, adress0_.ADRESS_UTL as ADRESS_UTL0_, adress0_.ADRID as ADRID0_ from LGAB_ADRESS adress0_ where adress0_.INTRID=?
se.posten.logistik.applikation.hibernate.Kund@1f2cea2[kundnr=4104476009]
java.lang.ClassCastException
at se.posten.logistik.applikation.hibernate.HibernateTestMain.loadSingleCustomerTest(HibernateTestMain.java:69)
at se.posten.logistik.applikation.hibernate.HibernateTestMain.<init>(HibernateTestMain.java:34)
Mapping file Kund.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="se.posten.logistik.applikation.hibernate">
<class name="Kund" table="LGAB_KUND" mutable="true">
<id name="kundnr" type="java.lang.String" column="KUNDNR">
<generator class="assigned"/>
</id>
<property name="kundnamn" type="java.lang.String" column="KUNDNAMN"/>
<property name="koncern" type="java.lang.String" column="KONCERN" not-null="false" unique="false"/>
<property name="kundnr_gl" type="java.lang.String" column="KUNDNR_GL" not-null="false" unique="false"/>
<property name="orgnr" type="java.lang.String" column="ORGNR" not-null="false" unique="false"/>
<property name="kredsparrjn" type="java.lang.String" column="KREDSPARRJN" not-null="false" unique="false"/>
<property name="faktspecuts" type="java.lang.String" column="FAKTSPECUTS" not-null="false" unique="false"/>
<property name="intrid" type="java.lang.Integer" column="INTRID" not-null="false" unique="false"/>
<property name="kundrefkrav" type="java.lang.String" column="KUNDREFKRAV" not-null="false" unique="false"/>
<property name="kundintjn" type="java.lang.String" column="KUNDINTJN" not-null="false" unique="false"/>
<property name="betvillkor" type="java.lang.String" column="BETVILLKOR" not-null="false" unique="false"/>
<property name="faktselkod" type="java.lang.Integer" column="FAKTSELKOD" not-null="false" unique="false"/>
<property name="kreditgrans" type="java.lang.Integer" column="KREDITGRANS" not-null="false" unique="false"/>
<property name="kredsparrtxt" type="java.lang.String" column="KREDSPARRTXT" not-null="false" unique="false"/>
<property name="sakerhetstyp" type="java.lang.Integer" column="SAKERHETSTYP" not-null="false" unique="false"/>
<property name="sakerhetsbel" type="java.lang.String" column="SAKERHETSBEL" not-null="false" unique="false"/>
<property name="faktavgkod" type="java.lang.String" column="FAKTAVGKOD" not-null="false" unique="false"/>
<property name="datumkredgr" type="java.lang.String" column="DATUMKREDGR" not-null="false" unique="false"/>
<property name="datumkredsp" type="java.lang.String" column="DATUMKREDSP" not-null="false" unique="false"/>
<property name="autogiro" type="java.lang.Integer" column="AUTOGIRO" not-null="false" unique="false"/>
<property name="kredtillkod" type="java.lang.Integer" column="KREDTILLKOD" not-null="false" unique="false"/>
<property name="datumkundreg" type="java.lang.String" column="DATUMKUNDREG" not-null="false" unique="false"/>
<property name="datumkundand" type="java.lang.String" column="DATUMKUNDAND" not-null="false" unique="false"/>
<property name="datumkundbrt" type="java.lang.String" column="DATUMKUNDBRT" not-null="false" unique="false"/>
<property name="betkundnr" type="java.lang.String" column="BETKUNDNR" not-null="false" unique="false"/>
<property name="sarfaktjn" type="java.lang.String" column="SARFAKTJN" not-null="false" unique="false"/>
<property name="klkod" type="java.lang.Integer" column="KLKOD" not-null="false" unique="false"/>
<property name="snikod" type="java.lang.String" column="SNIKOD" not-null="false" unique="false"/>
<property name="antanstkod" type="java.lang.Integer" column="ANTANSTKOD" not-null="false" unique="false"/>
<property name="omskod" type="java.lang.Integer" column="OMSKOD" not-null="false" unique="false"/>
<property name="parid" type="java.lang.Integer" column="PARID" not-null="false" unique="false"/>
<property name="kravkod" type="java.lang.String" column="KRAVKOD" not-null="false" unique="false"/>
<property name="koncernnr" type="java.lang.Integer" column="KONCERNNR" not-null="true" unique="false"/>
<property name="kredavtjn" type="java.lang.String" column="KREDAVTJN" not-null="false" unique="false"/>
<property name="vatnr" type="java.lang.String" column="VATNR" not-null="false" unique="false"/>
<property name="fspecintkod" type="java.lang.Integer" column="FSPECINTKOD" not-null="false" unique="false"/>
<property name="kundkatkod" type="java.lang.String" column="KUNDKATKOD" not-null="false" unique="false"/>
<property name="datakornkod" type="java.lang.Integer" column="DATAKORNKOD" not-null="false" unique="false"/>
<property name="utskickjn" type="java.lang.String" column="UTSKICKJN" not-null="false" unique="false"/>
<property name="kundnamn_v" type="java.lang.String" column="KUNDNAMN_V" not-null="false" unique="false"/>
<property name="foljsedjn" type="java.lang.String" column="FOLJSEDJN" not-null="false" unique="false"/>
<property name="taxeringsbarjn" type="java.lang.String" column="TAXERINGSBARJN" not-null="false" unique="false"/>
<property name="sapkundnr" type="java.lang.String" column="SAPKUNDNR" not-null="false" unique="false"/>
</class>
</hibernate-mapping>
Mapping file KundTest.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<!-- table created by: CREATE TABLE KEYWORDS ( ID IDENTITY, NAME VARCHAR(25) ); -->
<class name="se.posten.logistik.applikation.hibernate.KundTest" table="LGAB_KUND" mutable="true">
<id name="kundnrtest" type="java.lang.String" column="KUNDNR">
<generator class="assigned"/>
</id>
<property name="Intrid" type="java.lang.Integer" column="INTRID"/>
<component name="KundNamn" class="se.posten.logistik.applikation.hibernate.KundNamn">
<property name="Namn" type="java.lang.String" column="KUNDNAMN"/>
<property name="Taxeringsbar" type="java.lang.String" column="TAXERINGSBARJN"/>
</component>
</class>
</hibernate-mapping>
And finally the java code (it is Two classes)
Code:
package se.posten.logistik.applikation.hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.HashSet;
/**
* <p>Title: Hibernate test</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class HibernateTestMain
{
public HibernateTestMain()
{
try
{
System.out.println("Kund: " + loadSingleCustomer("4104476009").getKundnamn());
System.out.println("IntrId: " + loadSingleCustomer("4104476009").getIntrid());
System.out.println("Adress: " + loadSingleAdress((loadSingleCustomer("4104476009").getIntrid())).getLandkod());
System.out.println("*********************************");
KundTest kt = loadSingleCustomerTest("4104476009");
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
}
public AdressTest loadSingleAdressTest(Integer par_nIntrid)
throws HibernateException
{
return (AdressTest)HibernateUtil.currentSession().load(AdressTest.class, par_nIntrid);
}
public KundTest loadSingleCustomerTest(String par_strKundNr)
throws HibernateException
{
Object o = HibernateUtil.currentSession().load(KundTest.class, par_strKundNr);
System.err.println( o.toString());
if(o instanceof KundTest)
{
System.out.println("Tjohoo");
}
[b][color=darkred]This is where the error happens[/color][/b]
return (KundTest)o;
}
public List loadAllCustomers()
throws HibernateException
{
return HibernateUtil.currentSession().
createSQLQuery("select {Kund.*} FROM LGAB_KUND {Kund}", "Kund", Kund.class).list();
}
private Adress loadSingleAdress(Integer par_nIntrId)
throws HibernateException
{
return (Adress)HibernateUtil.currentSession().load(Adress.class, par_nIntrId);
}
public Kund loadSingleCustomer(String par_strKundNr)
throws HibernateException
{
return (Kund)HibernateUtil.currentSession().load(Kund.class, par_strKundNr);
}
public static void main(String[] args)
{
try
{
new HibernateTestMain();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
package se.posten.logistik.applikation.hibernate;
/**
* <p>Title: Hibernate test</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
public class HibernateUtil
{
private static final SessionFactory sessionFactory;
static
{
try
{
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch(HibernateException ex)
{
throw new RuntimeException("Configuration problem: " + ex.getMessage(), ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession()
throws HibernateException
{
Session s = (Session)session.get();
// Open a new Session, if this Thread has none yet
if(s == null)
{
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession()
throws HibernateException
{
Session s = (Session)session.get();
session.set(null);
if(s != null)
{
s.close();
}
}
}