java.lang.ClassCastException: Location

Hibernate JIRA | Jeroen van Vianen | 1 decade ago
tip
Click on the to mark the solution that helps you, Samebug will learn from it.
As a community member, you’ll be rewarded for you help.
  1. 0

    Hibernate / Mailing Lists

    sourceforge.net | 1 year ago
    java.lang.ClassCastException: Location
  2. 0

    Hi, I have the following problem with Hibernate 3.0 through 3.0.2. I have a table with reports. Each report has a location. Locations have names and are "hierarchically ordered" and either 2, 4, 6 or 7 digits long. Imagine a 2 digit code to map to a state, a 4 digit to a city and a 6 and 7 digits to a neighbourhood. The locations table contains just a limited amount of approx 50000 rows. Hence a 6 or 7 digit location might not be found whereas the first 4 or first 2 will probably be available. To get the name of my location on either one of these four levels in ordinary SQL I would use: select l.* from reports r, locations l where r.id = ? and (r.location = l.location or substring(r.location,1,6) = l.location or substring(r.location,1,4) = l.location substring(r.location,1,2) = l.location) Therefore I used a mapping with a custom <sql-query> as seen below. However, they either fail with a ClassCastException for my Location class (Location.hbm.xml version 1) or with 0 rows found (version 2) whereas the debugging shows the Location objects for my Report are being found, but not set on Report's locations property I defined with appropriate getters and setters. Assume for the example a Report with id 1 and location '123456' and 3 Locations with keys '12', '1234' and '123456'. All these three Locations should hence be returned for Report's locations property. The relevant portion of the log for version 1 is: DEBUG org.hibernate.SQL - select l.location as location__, l.name as name__,.location as location0_, l.name as name0_ from reports r, locations l where r.id=? and l.location in (r.location, substring(r.location,1,6), substring(r.location,1,4), substring(r.location,1,2)) Hibernate: select l.location as location__, l.name as name__, l.location as location0_, l.name as name0_ from reports r, locations l where r.id=? and l.location in (r.location, substring(r.location,1,6), substring(r.location,1,4), substring(r.location,1,2)) DEBUG org.hibernate.jdbc.AbstractBatcher - preparing statement DEBUG org.hibernate.type.IntegerType - binding '1' to parameter: 1 DEBUG org.hibernate.jdbc.AbstractBatcher - about to open ResultSet (open ResultSets: 0, globally: 0) DEBUG org.hibernate.loader.Loader - result set contains (possibly empty) collection: [Report.locations#1] DEBUG org.hibernate.engine.CollectionLoadContext - uninitialized collection: initializing DEBUG org.hibernate.loader.Loader - processing result set DEBUG org.hibernate.loader.Loader - result set row: 0 DEBUG org.hibernate.type.StringType - returning '12' as column: location0_ DEBUG org.hibernate.type.StringType - returning 'State' as column: name0_ DEBUG org.hibernate.jdbc.AbstractBatcher - about to close ResultSet (open ResultSets: 1, globally: 1) DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) DEBUG org.hibernate.jdbc.AbstractBatcher - closing statement DEBUG org.hibernate.impl.SessionImpl - setting flush mode to: AUTO The stack trace for version 1 is: java.lang.ClassCastException: Location at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:693) at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:282) at org.hibernate.loader.Loader.doQuery(Loader.java:389) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:208) at org.hibernate.loader.Loader.doList(Loader.java:1522) at org.hibernate.loader.Loader.list(Loader.java:1505) at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:103) at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1343) at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:151) at org.hibernate.persister.collection.NamedQueryCollectionInitializer.initialize(NamedQueryCollectionInitializer.java:49) at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:488) at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60) at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1356) at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:171) at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48) at org.hibernate.collection.PersistentBag.size(PersistentBag.java:218) at ReportTest.main(ReportTest.java:35) The log for version 2 is: Hibernate: select l.location as location__, l.location as location0_, l.name as name1_0_ from reports r, locations l where r.id=? and l.location in(r.location, substring(r.location,1,6), substring(r.location,1,4), substring(r.location,1,2)) DEBUG org.hibernate.jdbc.AbstractBatcher - preparing statement DEBUG org.hibernate.type.IntegerType - binding '1' to parameter: 1 DEBUG org.hibernate.jdbc.AbstractBatcher - about to open ResultSet (open ResultSets: 0, globally: 0) DEBUG org.hibernate.loader.Loader - result set contains (possibly empty) collection: [Report.locations#1] DEBUG org.hibernate.engine.CollectionLoadContext - uninitialized collection: initializing DEBUG org.hibernate.loader.Loader - processing result set DEBUG org.hibernate.loader.Loader - result set row: 0 DEBUG org.hibernate.type.StringType - returning '12' as column: location0_ DEBUG org.hibernate.loader.Loader - result row: EntityKey[Location#12] DEBUG org.hibernate.loader.Loader - Initializing object from ResultSet: [Location#12] DEBUG org.hibernate.persister.entity.BasicEntityPersister - Hydrating entity: [Location#12] DEBUG org.hibernate.type.StringType - returning 'State' as column: name1_0_ DEBUG org.hibernate.type.IntegerType - returning '12' as column: location__ DEBUG org.hibernate.loader.Loader - found row of collection: [Report.locations#12] DEBUG org.hibernate.engine.CollectionLoadContext - newcollection: instantiating DEBUG org.hibernate.type.StringType - returning '12' as column: location__ DEBUG org.hibernate.event.def.DefaultLoadEventListener - loading entity: [Location#12] DEBUG org.hibernate.event.def.DefaultLoadEventListener - entity found in session cache DEBUG org.hibernate.event.def.DefaultLoadEventListener - attempting to resolve: [Location#12] DEBUG org.hibernate.event.def.DefaultLoadEventListener - resolved object in session cache: [Location#12] DEBUG org.hibernate.loader.Loader - result set row: 1 DEBUG org.hibernate.type.StringType - returning '1234' as column: location0_ DEBUG org.hibernate.loader.Loader - result row: EntityKey[Location#1234] DEBUG org.hibernate.loader.Loader - Initializing object from ResultSet: [Location#1234] DEBUG org.hibernate.persister.entity.BasicEntityPersister - Hydrating entity: [Location#1234] DEBUG org.hibernate.type.StringType - returning 'City' as column: name1_0_ DEBUG org.hibernate.type.IntegerType - returning '1234' as column: location__ DEBUG org.hibernate.loader.Loader - found row of collection: [Report.locations#1234] DEBUG org.hibernate.engine.CollectionLoadContext - new collection: instantiating DEBUG org.hibernate.type.StringType - returning '1234' as column: location__ DEBUG org.hibernate.event.def.DefaultLoadEventListener - loading entity: [Location#1234] DEBUG org.hibernate.event.def.DefaultLoadEventListener - entity found in session cache DEBUG org.hibernate.event.def.DefaultLoadEventListener - attempting to resolve: [Location#1234] DEBUG org.hibernate.event.def.DefaultLoadEventListener - resolved object in session cache: [Location#1234] DEBUG org.hibernate.loader.Loader - result set row: 2 DEBUG org.hibernate.type.StringType - returning '123456' as column: location0_ DEBUG org.hibernate.loader.Loader - result row: EntityKey[Location#123456] DEBUG org.hibernate.loader.Loader - Initializing object from ResultSet: [Location#123456] DEBUG org.hibernate.persister.entity.BasicEntityPersister - Hydrating entity: [Location#123456] DEBUG org.hibernate.type.StringType - returning 'Neighbourhood' as column: name1_0_ DEBUG org.hibernate.type.IntegerType - returning '123456' as column: location__ DEBUG org.hibernate.loader.Loader - found row of collection: [Report.locations#123456] DEBUG org.hibernate.engine.CollectionLoadContext - new collection: instantiating DEBUG org.hibernate.type.StringType - returning '123456' as column: location__ DEBUG org.hibernate.event.def.DefaultLoadEventListener - loading entity: [Location#123456] DEBUG org.hibernate.event.def.DefaultLoadEventListener - entity found in session cache DEBUG org.hibernate.event.def.DefaultLoadEventListener - attempting to resolve: [Location#123456] DEBUG org.hibernate.event.def.DefaultLoadEventListener - resolved object in session cache: [Location#123456] DEBUG org.hibernate.loader.Loader - done processing result set (3 rows) DEBUG org.hibernate.jdbc.AbstractBatcher - about to close ResultSet (open ResultSets: 1, globally: 1) DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) DEBUG org.hibernate.jdbc.AbstractBatcher - closing statement DEBUG org.hibernate.loader.Loader - total objects hydrated: 3 DEBUG org.hibernate.engine.TwoPhaseLoad - resolving associations for [Location#12] DEBUG org.hibernate.engine.TwoPhaseLoad - done materializing entity [Location#12] DEBUG org.hibernate.engine.TwoPhaseLoad - resolving associations for [Location#1234] DEBUG org.hibernate.engine.TwoPhaseLoad - done materializing entity [Location#1234] DEBUG org.hibernate.engine.TwoPhaseLoad - resolving associations for [Location#123456] DEBUG org.hibernate.engine.TwoPhaseLoad - done materializing entity [Location#123456] DEBUG org.hibernate.engine.CollectionLoadContext - 4 collections were found in result set DEBUG org.hibernate.engine.CollectionLoadContext - collection fully initialized: [Report.locations#1234] DEBUG org.hibernate.engine.CollectionLoadContext - collection fully initialized: [Report.locations#123456] DEBUG org.hibernate.engine.CollectionLoadContext - collection fully initialized: [Report.locations#1] DEBUG org.hibernate.engine.CollectionLoadContext - collection fully initialized: [Report.locations#12] DEBUG org.hibernate.engine.CollectionLoadContext - 4 collections initialized DEBUG org.hibernate.engine.PersistenceContext - initializing non-lazy collections DEBUG org.hibernate.impl.SessionImpl - setting flush mode to: AUTO DEBUG org.hibernate.event.def.DefaultInitializeCollectionEventListener - collection initialized No locations My two mappings are: Report.hbm.xml: <?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Report" table="reports"> <id name="id"> <generator class="native"> <param name="sequence">seq_reports</param> </generator> </id> <bag name="locations" inverse="true"> <key column="location"/> <one-to-many class="Location"/> <loader query-ref="query-locations"/> </bag> </class> <sql-query name="query-locations"> <load-collection alias="l" role="Report.locations"/> select {l.*} from reports r, locations l where r.id=? and l.location in (r.location, substring(r.location,1,6), substring(r.location,1,4), substring(r.location,1,2)) </sql-query> </hibernate-mapping> Location.hbm.xml (version 1): I used a composite-id because a single location could have multiple names. <?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Location" table="locations"> <composite-id> <key-property name="location"/> <key-property name="name"/> </composite-id> </class> </hibernate-mapping> Location.hbm.xml (version 2): Assume a single location has only one name. <?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Location" table="locations"> <id name="location"> <generator class="assigned"/> </id> <property name="name"/> </class> </hibernate-mapping> My Report.java is: import java.util.List; public class Report { private int id = 0; private List locations = null; public Report() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public List getLocations() { return locations; } public void setLocations(List locations) { this.locations = locations; } } My Location.java is: import java.util.List; public class Location { private String name = null; private String location = null; public Location() { } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Location)) return false; final Location location1 = (Location) o; if (!location.equals(location1.location)) return false; if (!name.equals(location1.name)) return false; return true; } public int hashCode() { return 29 * (name != null ? name.hashCode() : 0) + (location != null ? location.hashCode() : 0); } } My test code would be: Report report = (Report) session.get(Report.class, 1); List list = report.getLocations(); for (int i = 0; i < list.size(); i++) { Location location = (Location) list.get(i); System.out.println(location.getName()); } if (list.size() == 0) { System.out.println("No locations"); } If you need a complete example with SQL input please contact me so I can send you a ZIP file with complete sources. Regards, Jeroen

    Hibernate JIRA | 1 decade ago | Jeroen van Vianen
    java.lang.ClassCastException: Location
  3. 0

    Hi, I have the following problem with Hibernate 3.0 through 3.0.2. I have a table with reports. Each report has a location. Locations have names and are "hierarchically ordered" and either 2, 4, 6 or 7 digits long. Imagine a 2 digit code to map to a state, a 4 digit to a city and a 6 and 7 digits to a neighbourhood. The locations table contains just a limited amount of approx 50000 rows. Hence a 6 or 7 digit location might not be found whereas the first 4 or first 2 will probably be available. To get the name of my location on either one of these four levels in ordinary SQL I would use: select l.* from reports r, locations l where r.id = ? and (r.location = l.location or substring(r.location,1,6) = l.location or substring(r.location,1,4) = l.location substring(r.location,1,2) = l.location) Therefore I used a mapping with a custom <sql-query> as seen below. However, they either fail with a ClassCastException for my Location class (Location.hbm.xml version 1) or with 0 rows found (version 2) whereas the debugging shows the Location objects for my Report are being found, but not set on Report's locations property I defined with appropriate getters and setters. Assume for the example a Report with id 1 and location '123456' and 3 Locations with keys '12', '1234' and '123456'. All these three Locations should hence be returned for Report's locations property. The relevant portion of the log for version 1 is: DEBUG org.hibernate.SQL - select l.location as location__, l.name as name__,.location as location0_, l.name as name0_ from reports r, locations l where r.id=? and l.location in (r.location, substring(r.location,1,6), substring(r.location,1,4), substring(r.location,1,2)) Hibernate: select l.location as location__, l.name as name__, l.location as location0_, l.name as name0_ from reports r, locations l where r.id=? and l.location in (r.location, substring(r.location,1,6), substring(r.location,1,4), substring(r.location,1,2)) DEBUG org.hibernate.jdbc.AbstractBatcher - preparing statement DEBUG org.hibernate.type.IntegerType - binding '1' to parameter: 1 DEBUG org.hibernate.jdbc.AbstractBatcher - about to open ResultSet (open ResultSets: 0, globally: 0) DEBUG org.hibernate.loader.Loader - result set contains (possibly empty) collection: [Report.locations#1] DEBUG org.hibernate.engine.CollectionLoadContext - uninitialized collection: initializing DEBUG org.hibernate.loader.Loader - processing result set DEBUG org.hibernate.loader.Loader - result set row: 0 DEBUG org.hibernate.type.StringType - returning '12' as column: location0_ DEBUG org.hibernate.type.StringType - returning 'State' as column: name0_ DEBUG org.hibernate.jdbc.AbstractBatcher - about to close ResultSet (open ResultSets: 1, globally: 1) DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) DEBUG org.hibernate.jdbc.AbstractBatcher - closing statement DEBUG org.hibernate.impl.SessionImpl - setting flush mode to: AUTO The stack trace for version 1 is: java.lang.ClassCastException: Location at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:693) at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:282) at org.hibernate.loader.Loader.doQuery(Loader.java:389) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:208) at org.hibernate.loader.Loader.doList(Loader.java:1522) at org.hibernate.loader.Loader.list(Loader.java:1505) at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:103) at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1343) at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:151) at org.hibernate.persister.collection.NamedQueryCollectionInitializer.initialize(NamedQueryCollectionInitializer.java:49) at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:488) at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60) at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1356) at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:171) at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48) at org.hibernate.collection.PersistentBag.size(PersistentBag.java:218) at ReportTest.main(ReportTest.java:35) The log for version 2 is: Hibernate: select l.location as location__, l.location as location0_, l.name as name1_0_ from reports r, locations l where r.id=? and l.location in(r.location, substring(r.location,1,6), substring(r.location,1,4), substring(r.location,1,2)) DEBUG org.hibernate.jdbc.AbstractBatcher - preparing statement DEBUG org.hibernate.type.IntegerType - binding '1' to parameter: 1 DEBUG org.hibernate.jdbc.AbstractBatcher - about to open ResultSet (open ResultSets: 0, globally: 0) DEBUG org.hibernate.loader.Loader - result set contains (possibly empty) collection: [Report.locations#1] DEBUG org.hibernate.engine.CollectionLoadContext - uninitialized collection: initializing DEBUG org.hibernate.loader.Loader - processing result set DEBUG org.hibernate.loader.Loader - result set row: 0 DEBUG org.hibernate.type.StringType - returning '12' as column: location0_ DEBUG org.hibernate.loader.Loader - result row: EntityKey[Location#12] DEBUG org.hibernate.loader.Loader - Initializing object from ResultSet: [Location#12] DEBUG org.hibernate.persister.entity.BasicEntityPersister - Hydrating entity: [Location#12] DEBUG org.hibernate.type.StringType - returning 'State' as column: name1_0_ DEBUG org.hibernate.type.IntegerType - returning '12' as column: location__ DEBUG org.hibernate.loader.Loader - found row of collection: [Report.locations#12] DEBUG org.hibernate.engine.CollectionLoadContext - newcollection: instantiating DEBUG org.hibernate.type.StringType - returning '12' as column: location__ DEBUG org.hibernate.event.def.DefaultLoadEventListener - loading entity: [Location#12] DEBUG org.hibernate.event.def.DefaultLoadEventListener - entity found in session cache DEBUG org.hibernate.event.def.DefaultLoadEventListener - attempting to resolve: [Location#12] DEBUG org.hibernate.event.def.DefaultLoadEventListener - resolved object in session cache: [Location#12] DEBUG org.hibernate.loader.Loader - result set row: 1 DEBUG org.hibernate.type.StringType - returning '1234' as column: location0_ DEBUG org.hibernate.loader.Loader - result row: EntityKey[Location#1234] DEBUG org.hibernate.loader.Loader - Initializing object from ResultSet: [Location#1234] DEBUG org.hibernate.persister.entity.BasicEntityPersister - Hydrating entity: [Location#1234] DEBUG org.hibernate.type.StringType - returning 'City' as column: name1_0_ DEBUG org.hibernate.type.IntegerType - returning '1234' as column: location__ DEBUG org.hibernate.loader.Loader - found row of collection: [Report.locations#1234] DEBUG org.hibernate.engine.CollectionLoadContext - new collection: instantiating DEBUG org.hibernate.type.StringType - returning '1234' as column: location__ DEBUG org.hibernate.event.def.DefaultLoadEventListener - loading entity: [Location#1234] DEBUG org.hibernate.event.def.DefaultLoadEventListener - entity found in session cache DEBUG org.hibernate.event.def.DefaultLoadEventListener - attempting to resolve: [Location#1234] DEBUG org.hibernate.event.def.DefaultLoadEventListener - resolved object in session cache: [Location#1234] DEBUG org.hibernate.loader.Loader - result set row: 2 DEBUG org.hibernate.type.StringType - returning '123456' as column: location0_ DEBUG org.hibernate.loader.Loader - result row: EntityKey[Location#123456] DEBUG org.hibernate.loader.Loader - Initializing object from ResultSet: [Location#123456] DEBUG org.hibernate.persister.entity.BasicEntityPersister - Hydrating entity: [Location#123456] DEBUG org.hibernate.type.StringType - returning 'Neighbourhood' as column: name1_0_ DEBUG org.hibernate.type.IntegerType - returning '123456' as column: location__ DEBUG org.hibernate.loader.Loader - found row of collection: [Report.locations#123456] DEBUG org.hibernate.engine.CollectionLoadContext - new collection: instantiating DEBUG org.hibernate.type.StringType - returning '123456' as column: location__ DEBUG org.hibernate.event.def.DefaultLoadEventListener - loading entity: [Location#123456] DEBUG org.hibernate.event.def.DefaultLoadEventListener - entity found in session cache DEBUG org.hibernate.event.def.DefaultLoadEventListener - attempting to resolve: [Location#123456] DEBUG org.hibernate.event.def.DefaultLoadEventListener - resolved object in session cache: [Location#123456] DEBUG org.hibernate.loader.Loader - done processing result set (3 rows) DEBUG org.hibernate.jdbc.AbstractBatcher - about to close ResultSet (open ResultSets: 1, globally: 1) DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) DEBUG org.hibernate.jdbc.AbstractBatcher - closing statement DEBUG org.hibernate.loader.Loader - total objects hydrated: 3 DEBUG org.hibernate.engine.TwoPhaseLoad - resolving associations for [Location#12] DEBUG org.hibernate.engine.TwoPhaseLoad - done materializing entity [Location#12] DEBUG org.hibernate.engine.TwoPhaseLoad - resolving associations for [Location#1234] DEBUG org.hibernate.engine.TwoPhaseLoad - done materializing entity [Location#1234] DEBUG org.hibernate.engine.TwoPhaseLoad - resolving associations for [Location#123456] DEBUG org.hibernate.engine.TwoPhaseLoad - done materializing entity [Location#123456] DEBUG org.hibernate.engine.CollectionLoadContext - 4 collections were found in result set DEBUG org.hibernate.engine.CollectionLoadContext - collection fully initialized: [Report.locations#1234] DEBUG org.hibernate.engine.CollectionLoadContext - collection fully initialized: [Report.locations#123456] DEBUG org.hibernate.engine.CollectionLoadContext - collection fully initialized: [Report.locations#1] DEBUG org.hibernate.engine.CollectionLoadContext - collection fully initialized: [Report.locations#12] DEBUG org.hibernate.engine.CollectionLoadContext - 4 collections initialized DEBUG org.hibernate.engine.PersistenceContext - initializing non-lazy collections DEBUG org.hibernate.impl.SessionImpl - setting flush mode to: AUTO DEBUG org.hibernate.event.def.DefaultInitializeCollectionEventListener - collection initialized No locations My two mappings are: Report.hbm.xml: <?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Report" table="reports"> <id name="id"> <generator class="native"> <param name="sequence">seq_reports</param> </generator> </id> <bag name="locations" inverse="true"> <key column="location"/> <one-to-many class="Location"/> <loader query-ref="query-locations"/> </bag> </class> <sql-query name="query-locations"> <load-collection alias="l" role="Report.locations"/> select {l.*} from reports r, locations l where r.id=? and l.location in (r.location, substring(r.location,1,6), substring(r.location,1,4), substring(r.location,1,2)) </sql-query> </hibernate-mapping> Location.hbm.xml (version 1): I used a composite-id because a single location could have multiple names. <?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Location" table="locations"> <composite-id> <key-property name="location"/> <key-property name="name"/> </composite-id> </class> </hibernate-mapping> Location.hbm.xml (version 2): Assume a single location has only one name. <?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Location" table="locations"> <id name="location"> <generator class="assigned"/> </id> <property name="name"/> </class> </hibernate-mapping> My Report.java is: import java.util.List; public class Report { private int id = 0; private List locations = null; public Report() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public List getLocations() { return locations; } public void setLocations(List locations) { this.locations = locations; } } My Location.java is: import java.util.List; public class Location { private String name = null; private String location = null; public Location() { } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Location)) return false; final Location location1 = (Location) o; if (!location.equals(location1.location)) return false; if (!name.equals(location1.name)) return false; return true; } public int hashCode() { return 29 * (name != null ? name.hashCode() : 0) + (location != null ? location.hashCode() : 0); } } My test code would be: Report report = (Report) session.get(Report.class, 1); List list = report.getLocations(); for (int i = 0; i < list.size(); i++) { Location location = (Location) list.get(i); System.out.println(location.getName()); } if (list.size() == 0) { System.out.println("No locations"); } If you need a complete example with SQL input please contact me so I can send you a ZIP file with complete sources. Regards, Jeroen

    Hibernate JIRA | 1 decade ago | Jeroen van Vianen
    java.lang.ClassCastException: Location
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Hibernate Query Caching - Why the misses?

    Coderanch | 6 years ago | Michael Freake
    java.lang.ClassCastException: java.math.BigDecimal cannot be cast to [Ljava.lang.Object;

    Root Cause Analysis

    1. java.lang.ClassCastException

      Location

      at org.hibernate.loader.Loader.getKeyFromResultSet()
    2. Hibernate
      CustomLoader.list
      1. org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:693)
      2. org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:282)
      3. org.hibernate.loader.Loader.doQuery(Loader.java:389)
      4. org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:208)
      5. org.hibernate.loader.Loader.doList(Loader.java:1522)
      6. org.hibernate.loader.Loader.list(Loader.java:1505)
      7. org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:103)
      7 frames
    3. Hibernate
      SQLQueryImpl.list
      1. org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1343)
      2. org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:151)
      2 frames
    4. Hibernate
      AbstractCollectionPersister.initialize
      1. org.hibernate.persister.collection.NamedQueryCollectionInitializer.initialize(NamedQueryCollectionInitializer.java:49)
      2. org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:488)
      2 frames
    5. Hibernate
      PersistentBag.size
      1. org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
      2. org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1356)
      3. org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:171)
      4. org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48)
      5. org.hibernate.collection.PersistentBag.size(PersistentBag.java:218)
      5 frames
    6. Unknown
      ReportTest.main
      1. ReportTest.main(ReportTest.java:35)
      1 frame