-->
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.  [ 2 posts ] 
Author Message
 Post subject: Save() not setting Java instance's identifier
PostPosted: Wed Mar 02, 2005 2:40 pm 
Newbie

Joined: Wed Mar 02, 2005 1:50 pm
Posts: 2
Location: Franklin, NC
I have a Java object that Hibernate is persisting with the "increment" identifier generator. When Hibernate's save(myObject) is called, I expected hibernate to update myObject's identifier automatically, but it is not. It appears that I have to query the database to get the identifier and set it mannually. But I would like this setter method to be private; nobody should be setting myObject's identifier except Hibernate.

Is there a configuration setting that I need to add which will tell Hibernate to set myObject's identifier automatically?

Hibernate version: 2.1

Mapping documents:

Account.hbm.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "file:///hibernate-2.1/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="domain">
<class name="Account" table="account">
<id column="uid" type="long">
<generator class="identity"/>
</id>
<property name="type" column="account_type" type="int" />
<property name="name" column="name" type="string" />
<property name="accountNumber" column="account_number" type="string" />
<component name="balance" class="Money">
<property name="rawAmount" column="balance_raw_amount" type="long" />
<property name="currencyCode" column="balance_currency_code" type="string" />
</component>
<component name="startingBalance" class="Money">
<property name="rawAmount" column="starting_balance_raw_amount" type="long" />
<property name="currencyCode" column="starting_balance_currency_code" type="string" />
</component>
<property name="active" column="is_active" type="boolean" />
<property name="startingBalanceEditable" column="is_starting_balance_editable" type="boolean" />
<property name="interestRate" column="interest_rate" type="double" />
<property name="deleted" column="is_deleted" type="boolean" />
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Session session = factory.openSession();
logger.debug( "session = " + session );

Account acct = new Account( Account.CHECKING, "Jim's Bank", "123",
new Money( 100.0D ) );
logger.debug( "acct = " + acct );
logger.debug( "acct.uid = " + acct.getUid() );

session.save( acct );
logger.debug( "saved acct" );
logger.debug( "acct.uid = " + acct.getUid() );


Object id = session.getIdentifier( acct );
long uid = ( (Long) id ).longValue();
acct.setUid( uid );
logger.debug( "modified acct.uid to " + uid );
logger.debug( "acct.uid = " + acct.getUid() );

session.flush();
logger.debug( "flushed session" );

session.connection().commit();
logger.debug( "commited session" );

session.close();

session = factory.openSession();
acct = Account.EXTERNAL_ACCOUNT;
session.load( acct, uid );
logger.debug( "loaded acct = " + acct );
logger.debug( "acct starting bal = " + acct.getStartingBalance() );
session.close();

Full stack trace of any exception that occurs: N/A

Name and version of the database you are using: hsqldb 1.7.2

The generated SQL (show_sql=true):

[java] Hibernate: insert into account (account_type, name, account_number,
balance_raw_amount, balance_currency_code, starting_balance_raw_amount, starting
_balance_currency_code, is_active, is_starting_balance_editable, interest_rate,
is_deleted, uid) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, null)
[java] preparing statement
[java] Dehydrating entity: [domain.Account#<null>]
[java] binding '1' to parameter: 1
[java] binding 'Jim's Bank' to parameter: 2
[java] binding '123' to parameter: 3
[java] binding '10000' to parameter: 4
[java] binding 'USD' to parameter: 5
[java] binding '10000' to parameter: 6
[java] binding 'USD' to parameter: 7
[java] binding 'true' to parameter: 8
[java] binding 'true' to parameter: 9
[java] binding '0.0' to parameter: 10
[java] binding 'false' to parameter: 11
[java] done closing: 0 open PreparedStatements, 0 open ResultSets
[java] closing statement
[java] about to open: 0 open PreparedStatements, 0 open ResultSets
[java] call identity()
[java] Hibernate: call identity()
[java] preparing statement
[java] Natively generated identity: 13
[java] done closing: 0 open PreparedStatements, 0 open ResultSets
[java] closing statement

Debug level Hibernate log excerpt:

Buildfile: persist.xml

run:
[java] Hibernate 2.1.7
[java] loaded properties from resource hibernate.properties: {hibernate.c3p
0.timeout=300, hibernate.connection.driver_class=org.hsqldb.jdbcDriver, hibernat
e.cglib.use_reflection_optimizer=true, hibernate.c3p0.max_statements=50, hiberna
te.c3p0.max_size=1, hibernate.dialect=net.sf.hibernate.dialect.HSQLDialect, hibe
rnate.c3p0.idle_test_period=3000, hibernate.c3p0.min_size=1, hibernate.connectio
n.username=sa, hibernate.connection.url=jdbc:hsqldb:hsql://localhost/moneymatter
s, hibernate.show_sql=true, hibernate.connection.password=}
[java] using CGLIB reflection optimizer
[java] using JDK 1.4 java.sql.Timestamp handling
[java] Mapping resource: domain/Account.hbm.xml
[java] Mapping class: domain.Account -> account
[java] Mapped property: type -> account_type, type: integer
[java] Mapped property: name -> name, type: string
[java] Mapped property: accountNumber -> account_number, type: string
[java] Mapped property: rawAmount -> balance_raw_amount, type: long
[java] Mapped property: currencyCode -> balance_currency_code, type: string

[java] reflection optimizer disabled for: domain.Money, BulkBeanException:
Property is private (property setRawAmount)
[java] Mapped property: balance -> balance_raw_amount, balance_currency_cod
e, type: domain.Money
[java] Mapped property: rawAmount -> starting_balance_raw_amount, type: lon
g
[java] Mapped property: currencyCode -> starting_balance_currency_code, typ
e: string
[java] reflection optimizer disabled for: domain.Money, BulkBeanException:
Property is private (property setRawAmount)
[java] Mapped property: startingBalance -> starting_balance_raw_amount, sta
rting_balance_currency_code, type: domain.Money
[java] Mapped property: active -> is_active, type: boolean
[java] Mapped property: startingBalanceEditable -> is_starting_balance_edit
able, type: boolean
[java] Mapped property: interestRate -> interest_rate, type: double
[java] Mapped property: deleted -> is_deleted, type: boolean
[java] processing one-to-many association mappings
[java] processing one-to-one association property references
[java] processing foreign key constraints
[java] Using dialect: net.sf.hibernate.dialect.HSQLDialect
[java] Using dialect defined converter
[java] Use outer join fetching: true
[java] C3P0 using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:hsql://
localhost/moneymatters
[java] Connection properties: {user=sa, password=}
[java] No TransactionManagerLookup configured (in JTA environment, use of p
rocess level read-write cache is not recommended)
[java] Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@c
a470 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolData
Source@b8f8eb [ acquireIncrement -> 1, autoCommitOnClose -> false, connectionTes
terClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, factoryClassLo
cation -> null, forceIgnoreUnresolvedTransactions -> false, idleConnectionTestPe
riod -> 3000, initialPoolSize -> 1, maxIdleTime -> 300, maxPoolSize -> 1, maxSta
tements -> 50, minPoolSize -> 1, nestedDataSource -> com.mchange.v2.c3p0.DriverM
anagerDataSource@1ffc686 [ description -> null, driverClass -> null, factoryClas
sLocation -> null, jdbcUrl -> jdbc:hsqldb:hsql://localhost/moneymatters, propert
ies -> {user=sa, password=} ] , propertyCycle -> 300, testConnectionOnCheckout -
> false ] , factoryClassLocation -> null, numHelperThreads -> 3 ]
[java] Use scrollable result sets: true
[java] Use JDBC3 getGeneratedKeys(): false
[java] Optimize cache for minimal puts: false
[java] echoing all SQL to stdout
[java] Query language substitutions: {}
[java] cache provider: net.sf.hibernate.cache.EhCacheProvider
[java] instantiating and configuring caches
[java] building session factory
[java] instantiating session factory with properties: {java.runtime.name=Ja
va(TM) 2 Runtime Environment, Standard Edition, hibernate.connection.password=,
sun.boot.library.path=C:\Program Files\Java\jdk1.5.0\jre\bin, java.vm.version=1.
5.0-b64, ant.library.dir=C:\apache-ant-1.6.1\lib, hibernate.connection.username=
sa, 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 2, java.vm.specification.n
ame=Java Virtual Machine Specification, user.dir=C:\hibernate-2.1, java.runtime.
version=1.5.0-b64, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, java.e
ndorsed.dirs=C:\Program Files\Java\jdk1.5.0\jre\lib\endorsed, os.arch=x86, hiber
nate.c3p0.max_statements=50, java.io.tmpdir=C:\DOCUME~1\jbowman\LOCALS~1\Temp\,
line.separator=
[java] , java.vm.specification.vendor=Sun Microsystems Inc., user.variant=,
os.name=Windows XP, ant.home=C:\apache-ant-1.6.1, hibernate.c3p0.idle_test_peri
od=3000, sun.jnu.encoding=Cp1252, hibernate.c3p0.timeout=300, java.library.path=
C:\Program Files\Java\jdk1.5.0\bin;.;C:\WINDOWS\system32;C:\WINDOWS;C:\Program F
iles\Java\jdk1.5.0\bin;C:\jwsdp-1.4\jaxb\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\W
INDOWS\System32\Wbem;C:\PROGRA~1\ULTRAE~1;C:\MSSQL7\BINN;C:\bin;C:\apache-ant-1.
6.1\bin;C:\MinGW\bin;C:\yjp-3.0-build431\bin;C:\cygwin\bin;C:\Program Files\IBM\
Cloudscape_10.0\frameworks\embedded\bin;c:\ruby\bin;C:\yjp-3.0-build431\bin;C:\g
roovy-1.0-beta-7\bin;C:\aspectj1.2\bin;C:\BATCH;C:\WINDOWS\COMMAND;Z:.;Y:.;X:.;W
:.;, java.specification.name=Java Platform API Specification, java.class.version
=49.0, sun.management.compiler=HotSpot Client Compiler, os.version=5.1, user.hom
e=C:\Documents and Settings\jbowman, user.timezone=GMT-05:00, java.awt.printerjo
b=sun.awt.windows.WPrinterJob, file.encoding=Cp1252, java.specification.version=
1.5, hibernate.c3p0.min_size=1, hibernate.connection.driver_class=org.hsqldb.jdb
cDriver, user.name=jbowman, java.class.path=C:\apache-ant-1.6.1\lib\ant-launcher
.jar;C:\hibernate-2.1\classes\;C:\hibernate-2.1\hibernate2.jar;C:\hibernate-2.1\
hsqldb.jar;C:\hibernate-2.1\lib\log4j-1.2.8.jar;C:\apache-ant-1.6.1\lib\ant-antl
r.jar;C:\apache-ant-1.6.1\lib\ant-apache-bsf.jar;C:\apache-ant-1.6.1\lib\ant-apa
che-resolver.jar;C:\apache-ant-1.6.1\lib\ant-commons-logging.jar;C:\apache-ant-1
.6.1\lib\ant-commons-net.jar;C:\apache-ant-1.6.1\lib\ant-icontract.jar;C:\apache
-ant-1.6.1\lib\ant-jai.jar;C:\apache-ant-1.6.1\lib\ant-jakarta-bcel.jar;C:\apach
e-ant-1.6.1\lib\ant-jakarta-log4j.jar;C:\apache-ant-1.6.1\lib\ant-jakarta-oro.ja
r;C:\apache-ant-1.6.1\lib\ant-jakarta-regexp.jar;C:\apache-ant-1.6.1\lib\ant-jav
amail.jar;C:\apache-ant-1.6.1\lib\ant-jdepend.jar;C:\apache-ant-1.6.1\lib\ant-jm
f.jar;C:\apache-ant-1.6.1\lib\ant-jsch.jar;C:\apache-ant-1.6.1\lib\ant-junit.jar
;C:\apache-ant-1.6.1\lib\ant-launcher.jar;C:\apache-ant-1.6.1\lib\ant-netrexx.ja
r;C:\apache-ant-1.6.1\lib\ant-nodeps.jar;C:\apache-ant-1.6.1\lib\ant-starteam.ja
r;C:\apache-ant-1.6.1\lib\ant-stylebook.jar;C:\apache-ant-1.6.1\lib\ant-swing.ja
r;C:\apache-ant-1.6.1\lib\ant-trax.jar;C:\apache-ant-1.6.1\lib\ant-vaj.jar;C:\ap
ache-ant-1.6.1\lib\ant-weblogic.jar;C:\apache-ant-1.6.1\lib\ant-xalan1.jar;C:\ap
ache-ant-1.6.1\lib\ant-xalan2.jar;C:\apache-ant-1.6.1\lib\ant-xslp.jar;C:\apache
-ant-1.6.1\lib\ant.jar;C:\apache-ant-1.6.1\lib\esanalyzer.jar;C:\apache-ant-1.6.
1\lib\ESCodeAnalyzerDoc.jar;C:\apache-ant-1.6.1\lib\escoreplugin.jar;C:\apache-a
nt-1.6.1\lib\xercesImpl.jar;C:\apache-ant-1.6.1\lib\xml-apis.jar;C:\Program File
s\Java\jdk1.5.0\lib\tools.jar, hibernate.show_sql=true, java.vm.specification.ve
rsion=1.0, sun.arch.data.model=32, java.home=C:\Program Files\Java\jdk1.5.0\jre,
hibernate.dialect=net.sf.hibernate.dialect.HSQLDialect, hibernate.connection.ur
l=jdbc:hsqldb:hsql://localhost/moneymatters, java.specification.vendor=Sun Micro
systems Inc., user.language=en, hibernate.c3p0.max_size=1, awt.toolkit=sun.awt.w
indows.WToolkit, hibernate.cglib.use_reflection_optimizer=true, java.vm.info=mix
ed mode, sharing, java.version=1.5.0, java.ext.dirs=C:\Program Files\Java\jdk1.5
.0\jre\lib\ext, sun.boot.class.path=C:\Program Files\Java\jdk1.5.0\jre\lib\rt.ja
r;C:\Program Files\Java\jdk1.5.0\jre\lib\i18n.jar;C:\Program Files\Java\jdk1.5.0
\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.5.0\jre\lib\jsse.jar;C:\Progr
am Files\Java\jdk1.5.0\jre\lib\jce.jar;C:\Program Files\Java\jdk1.5.0\jre\lib\ch
arsets.jar;C:\Program Files\Java\jdk1.5.0\jre\classes, java.vendor=Sun Microsyst
ems Inc., file.separator=\, java.vendor.url.bug=http://java.sun.com/cgi-bin/bugr
eport.cgi, sun.cpu.endian=little, sun.io.unicode.encoding=UnicodeLittle, sun.des
ktop=windows, sun.cpu.isalist=}
[java] reflection optimizer disabled for: domain.Account, VerifyError: (cla
ss: domain/Account$$BulkBeanByCGLIB$$d5f80b30, method: setPropertyValues signatu
re: (Ljava/lang/Object;[Ljava/lang/Object;)V) Expecting to find array of objects
or arrays on stack
[java] initializing class SessionFactoryObjectFactory
[java] registered: bfc4a2ec0264764d0102647656cd0000 (unnamed)
[java] Not binding factory to JNDI, no JNDI name configured
[java] instantiated session factory
[java] opened session
[java] saving [domain.Account#<null>]
[java] executing insertions
[java] Inserting entity: domain.Account (native id)
[java] about to open: 0 open PreparedStatements, 0 open ResultSets
[java] insert into account (account_type, name, account_number, balance_raw
_amount, balance_currency_code, starting_balance_raw_amount, starting_balance_cu
rrency_code, is_active, is_starting_balance_editable, interest_rate, is_deleted,
uid) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, null)
[java] Hibernate: insert into account (account_type, name, account_number,
balance_raw_amount, balance_currency_code, starting_balance_raw_amount, starting
_balance_currency_code, is_active, is_starting_balance_editable, interest_rate,
is_deleted, uid) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, null)
[java] preparing statement
[java] Dehydrating entity: [domain.Account#<null>]
[java] binding '1' to parameter: 1
[java] binding 'Jim's Bank' to parameter: 2
[java] binding '123' to parameter: 3
[java] binding '10000' to parameter: 4
[java] binding 'USD' to parameter: 5
[java] binding '10000' to parameter: 6
[java] binding 'USD' to parameter: 7
[java] binding 'true' to parameter: 8
[java] binding 'true' to parameter: 9
[java] binding '0.0' to parameter: 10
[java] binding 'false' to parameter: 11
[java] done closing: 0 open PreparedStatements, 0 open ResultSets
[java] closing statement
[java] about to open: 0 open PreparedStatements, 0 open ResultSets
[java] call identity()
[java] Hibernate: call identity()
[java] preparing statement
[java] Natively generated identity: 13
[java] done closing: 0 open PreparedStatements, 0 open ResultSets
[java] closing statement
[java] flushing session
[java] Flushing entities and processing referenced collections
[java] Processing unreferenced collections
[java] Scheduling collection removes/(re)creates/updates
[java] Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
[java] Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
[java] listing entities:
[java] domain.Account{deleted=false, active=true, balance=Money{currencyCod
e=USD, rawAmount=10000}, interestRate=0.0, startingBalanceEditable=true, type=1,
accountNumber=123, startingBalance=Money{currencyCode=USD, rawAmount=10000}, na
me=Jim's Bank}
[java] executing flush
[java] post flush
[java] closing session
[java] disconnecting session
[java] transaction completion
[java] opened session
[java] attempting to resolve [domain.Account#13]
[java] object not resolved in any cache [domain.Account#13]
[java] Materializing entity: [domain.Account#13]
[java] about to open: 0 open PreparedStatements, 0 open ResultSets
[java] select account0_.uid as uid0_, account0_.account_type as account_2_0
_, account0_.name as name0_, account0_.account_number as account_4_0_, account0_
.balance_raw_amount as balance_5_0_, account0_.balance_currency_code as balance_
6_0_, account0_.starting_balance_raw_amount as starting7_0_, account0_.starting_
balance_currency_code as starting8_0_, account0_.is_active as is_active0_, accou
nt0_.is_starting_balance_editable as is_star10_0_, account0_.interest_rate as in
teres11_0_, account0_.is_deleted as is_deleted0_ from account account0_ where ac
count0_.uid=?
[java] Hibernate: select account0_.uid as uid0_, account0_.account_type as
account_2_0_, account0_.name as name0_, account0_.account_number as account_4_0_
, account0_.balance_raw_amount as balance_5_0_, account0_.balance_currency_code
as balance_6_0_, account0_.starting_balance_raw_amount as starting7_0_, account0
_.starting_balance_currency_code as starting8_0_, account0_.is_active as is_acti
ve0_, account0_.is_starting_balance_editable as is_star10_0_, account0_.interest
_rate as interes11_0_, account0_.is_deleted as is_deleted0_ from account account
0_ where account0_.uid=?
[java] preparing statement
[java] binding '13' to parameter: 1
[java] processing result set
[java] result row: 13
[java] Initializing object from ResultSet: 13
[java] Hydrating entity: domain.Account#13
[java] returning '1' as column: account_2_0_
[java] returning 'Jim's Bank' as column: name0_
[java] returning '123' as column: account_4_0_
[java] returning '10000' as column: balance_5_0_
[java] returning 'USD' as column: balance_6_0_
[java] returning '10000' as column: starting7_0_
[java] returning 'USD' as column: starting8_0_
[java] returning 'true' as column: is_active0_
[java] returning 'true' as column: is_star10_0_
[java] returning '0.0' as column: interes11_0_
[java] returning 'false' as column: is_deleted0_
[java] done processing result set (1 rows)
[java] done closing: 0 open PreparedStatements, 0 open ResultSets
[java] closing statement
[java] total objects hydrated: 1
[java] resolving associations for [domain.Account#13]
[java] done materializing entity [domain.Account#13]
[java] initializing non-lazy collections
[java] closing session
[java] disconnecting session
[java] transaction completion

all:
[echo] Ran all targets.

BUILD SUCCESSFUL
Total time: 9 seconds

_________________
Jim


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 6:41 pm 
Newbie

Joined: Wed Mar 02, 2005 1:50 pm
Posts: 2
Location: Franklin, NC
I figured it out. I updated myObject's mapping (in Account.hbm.xml) as follows (added text in bold):

<id name="uid" column="uid" type="long">
<generator class="identity"/>
</id>

Without the added name property, the Java object's id is updated when hibernate's save method is called. Without this name property, the object is persisted to the database, but the Java object's id is not updated.

_________________
Jim


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.