-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Hibernate Lists Help
PostPosted: Thu Nov 24, 2005 7:08 am 
Newbie

Joined: Thu Nov 24, 2005 6:41 am
Posts: 6
Hi,

What I am trying to do is from a static method on a Customer class I want to retrieve all customer objects from the database. I have attempted to define this is the mapping document but it fails. I am new to hibernate and would appreciate any help that can be given. I should also say that retrieving a specific object instance or persisting an instance works fine, it is just getting the list that I cannot get working

Hibernate version:
3.0.5

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="src">
<class name="Customer" table="customer">
<id name="id" column="id" type="integer" unsaved-value="null">
<generator class="native"/>
</id>

<property name="firstname" type="string" length="45"/>
<property name="lastname" type="string" length="45"/>
<property name="gender" type="string" length="1"/>

<set name="customers" table="customer" lazy="true">
<key column="id"/>
<composite-element class="Customer">
<property name="firstname"/>
<property name="lastname"/>
<property name="gender"/>
</composite-element>
</set>

</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory factory = cfg.buildSessionFactory();
session = factory.openSession();
Customer customer = (Customer) session.get(Customer.class, customerId);
session.close();
return customer;

Name and version of the database you are using:
MySQL 4.1

Debug level Hibernate log excerpt:
10:59:07,257 INFO SessionFactoryImpl:152 - building session factory
10:59:07,257 DEBUG SessionFactoryImpl:161 - Session factory constructed with filter configurations : {}
10:59:07,267 DEBUG SessionFactoryImpl:164 - instantiating session factory with properties: {hibernate.connection.password=practicum, java.runtime.name=Java(TM) 2 Runtime Environment, Standard Edition, sun.boot.library.path=C:\opt\java\jdk1.5.0_04\jre\bin, java.vm.version=1.5.0_04-b05, hibernate.connection.username=practicum, java.vm.vendor=Sun Microsystems Inc., java.vendor.url=http://java.sun.com/, path.separator=;, java.vm.name=Java HotSpot(TM) Client VM, file.encoding.pkg=sun.io, user.country=US, sun.os.patch.level=Service Pack 4, java.vm.specification.name=Java Virtual Machine Specification, user.dir=C:\usr\M.Sc\Practicum\Development\example\hibernate, java.runtime.version=1.5.0_04-b05, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, java.endorsed.dirs=C:\opt\java\jdk1.5.0_04\jre\lib\endorsed, os.arch=x86, java.io.tmpdir=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\, line.separator=
, java.vm.specification.vendor=Sun Microsystems Inc., user.variant=, os.name=Windows 2000, sun.jnu.encoding=Cp1252, java.library.path=C:\opt\java\jdk1.5.0_04\bin;.;C:\WINNT\system32;C:\WINNT;C:\opt\Perl\bin\;C:\opt\oracle\product\oracle902\bin;C:\opt\java\jdk1.5.0_04\bin;C:\Program Files\Oracle\jre\1.1.8\bin;C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;C:\opt\perforce;C:\opt\ant\1.6.5\bin\;C:\opt\java\Sun\AppServer\bin;C:\opt\java\jad1.5.8, java.specification.name=Java Platform API Specification, java.class.version=49.0, sun.management.compiler=HotSpot Client Compiler, os.version=5.0, connection.password=practicum, user.home=C:\Documents and Settings\Administrator, user.timezone=Europe/London, connection.username=practicum, java.awt.printerjob=sun.awt.windows.WPrinterJob, file.encoding=Cp1252, java.specification.version=1.5, hibernate.connection.driver_class=com.mysql.jdbc.Driver, user.name=Administrator, java.class.path=C:\usr\M.Sc\Practicum\Development\example\hibernate;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\antlr-2.7.5H3.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\ant-antlr-1.6.3.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\ant-junit-1.6.3.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\ant-launcher-1.6.3.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\ant-1.6.3.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\ant-swing-1.6.3.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\asm.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\asm-attrs.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\c3p0-0.8.5.2.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\cglib-2.1.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\cleanimports.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\commons-collections-2.1.1.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\commons-logging-1.0.4.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\concurrent-1.3.2.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\connector.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\dom4j-1.6.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\ehcache-1.1.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jaas.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jacc-1_0-fr.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jaxen-1.1-beta-4.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jboss-cache.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jboss-common.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jboss-jmx.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jboss-system.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jdbc2_0-stdext.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jgroups-2.2.7.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jta.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\junit-3.8.1.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\log4j-1.2.9.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\oscache-2.1.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\proxool-0.8.3.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\swarmcache-1.0rc2.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\versioncheck.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\xerces-2.6.2.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\xml-apis.jar;C:\opt\tomcat\5.5.9\common\lib\servlet-api.jar;C:\opt\tomcat\5.5.9\common\lib\mcj317.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\hibernate3.jar;/c:/opt/java/eclipse/3.1/eclipse/plugins/org.eclipse.jdt.junit_3.1.0/junitsupport.jar;/c:/opt/java/eclipse/3.1/eclipse/plugins/org.eclipse.jdt.junit.runtime_3.1.0/junitruntime.jar, java.vm.specification.version=1.0, java.home=C:\opt\java\jdk1.5.0_04\jre, sun.arch.data.model=32, hibernate.connection.url=jdbc:mysql://localhost/practicum, hibernate.dialect=org.hibernate.dialect.MySQLDialect, user.language=en, java.specification.vendor=Sun Microsystems Inc., awt.toolkit=sun.awt.windows.WToolkit, hibernate.cglib.use_reflection_optimizer=true, java.vm.info=mixed mode, java.version=1.5.0_04, java.ext.dirs=C:\opt\java\jdk1.5.0_04\jre\lib\ext, sun.boot.class.path=C:\opt\java\jdk1.5.0_04\jre\lib\rt.jar;C:\opt\java\jdk1.5.0_04\jre\lib\i18n.jar;C:\opt\java\jdk1.5.0_04\jre\lib\sunrsasign.jar;C:\opt\java\jdk1.5.0_04\jre\lib\jsse.jar;C:\opt\java\jdk1.5.0_04\jre\lib\jce.jar;C:\opt\java\jdk1.5.0_04\jre\lib\charsets.jar;C:\opt\java\jdk1.5.0_04\jre\classes, java.vendor=Sun Microsystems Inc., connection.driver_class=com.mysql.jdbc.Driver, file.separator=\, java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi, sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, sun.desktop=windows, connection.url=jdbc:mysql://localhost/practicum, dialect=org.hibernate.dialect.MySQLDialect, sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86}
10:59:07,267 DEBUG CacheManager:196 - Attempting to create an existing instance. Existing instance returned.
10:59:07,948 DEBUG ReflectHelper:201 - reflection optimizer disabled for: src.Customer, VerifyError: (class: src/Customer$$BulkBeanByCGLIB$$b5e5fa68_2, method: getPropertyValues signature: (Ljava/lang/Object;[Ljava/lang/Object;)V) Expecting to find integer on stack
10:59:07,948 DEBUG BasicEntityPersister:2220 - Static SQL for entity: src.Customer
10:59:07,958 DEBUG BasicEntityPersister:2222 - Version select: select id from customer where id =?
10:59:07,958 DEBUG BasicEntityPersister:2223 - Snapshot select: select customer_.id, customer_.firstname as firstname1_, customer_.lastname as lastname1_, customer_.gender as gender1_ from customer customer_ where customer_.id=?
10:59:07,958 DEBUG BasicEntityPersister:2225 - Insert 0: insert into customer (firstname, lastname, gender, id) values (?, ?, ?, ?)
10:59:08,048 DEBUG BasicEntityPersister:2226 - Update 0: update customer set firstname=?, lastname=?, gender=? where id=?
10:59:08,048 DEBUG BasicEntityPersister:2227 - Delete 0: delete from customer where id=?
10:59:08,048 DEBUG BasicEntityPersister:2230 - Identity insert: insert into customer (firstname, lastname, gender) values (?, ?, ?)
10:59:08,068 DEBUG AbstractCollectionPersister:479 - Static SQL for collection: src.Customer.customers
10:59:08,078 DEBUG AbstractCollectionPersister:480 - Row insert: insert into customer (id, firstname, lastname, gender) values (?, ?, ?, ?)
10:59:08,078 DEBUG AbstractCollectionPersister:481 - Row update: update customer set firstname=?, lastname=?, gender=? where id=? and firstname=? and lastname=? and gender=?
10:59:08,078 DEBUG AbstractCollectionPersister:482 - Row delete: delete from customer where id=? and firstname=? and lastname=? and gender=?
10:59:08,148 DEBUG AbstractCollectionPersister:483 - One-shot delete: delete from customer where id=?
10:59:08,148 DEBUG EntityLoader:95 - Static select for entity src.Customer: select customer0_.id as id0_, customer0_.firstname as firstname1_0_, customer0_.lastname as lastname1_0_, customer0_.gender as gender1_0_ from customer customer0_ where customer0_.id=?
10:59:08,158 DEBUG EntityLoader:95 - Static select for entity src.Customer: select customer0_.id as id0_, customer0_.firstname as firstname1_0_, customer0_.lastname as lastname1_0_, customer0_.gender as gender1_0_ from customer customer0_ where customer0_.id=?
10:59:08,168 DEBUG EntityLoader:95 - Static select for entity src.Customer: select customer0_.id as id0_, customer0_.firstname as firstname1_0_, customer0_.lastname as lastname1_0_, customer0_.gender as gender1_0_ from customer customer0_ where customer0_.id=? for update
10:59:08,168 DEBUG EntityLoader:95 - Static select for entity src.Customer: select customer0_.id as id0_, customer0_.firstname as firstname1_0_, customer0_.lastname as lastname1_0_, customer0_.gender as gender1_0_ from customer customer0_ where customer0_.id=? for update
10:59:08,168 DEBUG CollectionLoader:94 - Static select for collection src.Customer.customers: select customers0_.id as id0_, customers0_.firstname as firstname0_, customers0_.lastname as lastname0_, customers0_.gender as gender0_ from customer customers0_ where customers0_.id=?
10:59:08,178 DEBUG SessionFactoryObjectFactory:76 - registered: 8a8a8a8a07c1ed280107c1ed39120001 (unnamed)
10:59:08,178 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
10:59:08,178 DEBUG SessionFactoryImpl:262 - instantiated session factory
10:59:08,188 INFO SessionFactoryImpl:379 - Checking 0 named queries
10:59:08,188 DEBUG SessionImpl:250 - opened session at timestamp: 4640071467778048
10:59:13,456 DEBUG SessionImpl:829 - find: from customer
10:59:13,486 DEBUG QueryParameters:224 - named parameters: {}
10:59:13,967 DEBUG QueryTranslatorImpl:207 - parse() - HQL: from customer
10:59:14,057 DEBUG AST:223 - --- HQL AST ---
\-[QUERY] 'query'
\-[SELECT_FROM] 'SELECT_FROM'
\-[FROM] 'from'
\-[RANGE] 'RANGE'
\-[IDENT] 'customer'

10:59:14,067 DEBUG ErrorCounter:72 - throwQueryException() : no errors
10:59:14,327 DEBUG HqlSqlBaseWalker:120 - query() << begin, level = 1
10:59:14,437 ERROR PARSER:35 - *** ERROR: customer is not mapped.
10:59:14,497 DEBUG ErrorCounter:28 - customer is not mapped.
customer is not mapped.
at org.hibernate.hql.ast.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:85)
at org.hibernate.hql.ast.FromElementFactory.addFromElement(FromElementFactory.java:77)
at org.hibernate.hql.ast.FromClause.addFromElement(FromClause.java:67)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:217)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:2830)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2719)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:513)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:371)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:201)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:151)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:884)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:834)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at src.Customer.getCustomers(Customer.java:87)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:105)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.getForInsert(BasicPropertyAccessor.java:124)
at org.hibernate.tuple.AbstractTuplizer.getPropertyValuesToInsert(AbstractTuplizer.java:189)
at org.hibernate.tuple.PojoTuplizer.getPropertyValuesToInsert(PojoTuplizer.java:194)
at org.hibernate.persister.entity.BasicEntityPersister.getPropertyValuesToInsert(BasicEntityPersister.java:3005)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:216)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:95)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:96)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:463)
at src.HibernateUtil.save(HibernateUtil.java:61)
at tests.CustomerTest.testCreateCustomer(CustomerTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
10:59:14,558 DEBUG HqlSqlBaseWalker:125 - query() : finishing up , level = 1
10:59:14,598 DEBUG HqlSqlWalker:331 - processQuery() : ( SELECT FromClause{level=1} )
10:59:14,698 DEBUG HqlSqlWalker:451 - Derived SELECT clause created.
10:59:14,748 DEBUG HqlSqlBaseWalker:128 - query() >> end, level = 1
10:59:14,798 DEBUG AST:193 - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT'
+-[SELECT_CLAUSE] SelectClause: '{derived select clause}'
\-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=0, fromElements=0, fromElementByClassAlias=[], fromElementByTableAlias=[], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}


Thanks,

Rory


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 7:44 am 
Newbie

Joined: Thu Sep 01, 2005 8:43 am
Posts: 14
Why not just a:

Code:
static List<Customer> getAllCustomers(session) {
  session.createCriteria(Customer.class).list();
}
?

Otherwise you should maybe use a OneToMany relation instead of list of composite elements... Customer beeing an Entity itself. Never tried to see if it works for static members though, this sounds a bit weird.

cu,


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 8:27 am 
Newbie

Joined: Thu Nov 24, 2005 6:41 am
Posts: 6
Thanks for that landels, worked a treat

Rory


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.