Hi,
thanks for your reply and your benchmarks!
This operation is done by a user inserting data into an existing database (although an extreme case). I also use Derby.
I am not sure if I follow all of your comments though:
Quote:
Hibernate shouldn't need to query the database to determine if the object exists - the lack of a primary key indicates this.
do you mean that hibernate knows of all existing primary keys in the first
place, so that if this is a new primary key, hbn should know firsthand?
I am using composite keys for both A and B, maybe that is important?
It seems at least to me that hibernate queries the database before saving a new object:
Below I have posted the debug + SQL output from Hibernate that happens when I do
aSession.save(Object object)
on an object (in this case an object of class Observation. Time stamps are strange because I was stepping in debug mode).
Code:
11:09:53,062 DEBUG DefaultSaveOrUpdateEventListener:158 - saving transient instance
11:09:53,078 DEBUG AbstractSaveEventListener:112 - generated identifier: component[nation,platform,survey,observationDate,observationTime,observationType]{observationTime=1212779, observationDate=20031027, platform=2471, survey=7, observationType=3000, nation=578}, using strategy: org.hibernate.id.Assigned
11:09:53,078 DEBUG AbstractSaveEventListener:153 - saving [no.imr.lsss.database.tables.hibernate.Observation#component[nation,platform,survey,observationDate,observationTime,observationType]{observationTime=1212779, observationDate=20031027, platform=2471, survey=7, observationType=3000, nation=578}]
11:09:53,078 DEBUG IdentifierValue:77 - id unsaved-value strategy UNDEFINED
11:10:13,750 DEBUG AbstractEntityPersister:1013 - Getting current persistent state for: [no.imr.lsss.database.tables.hibernate.Survey#component[nation,platform,survey]{platform=2471, survey=7, nation=578}]
11:10:13,765 DEBUG AbstractBatcher:358 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
11:10:13,765 DEBUG SQL:393 - select survey_.Nation, survey_.Platform, survey_.Survey, survey_.SurveyTitle as SurveyTi4_6_, survey_.StartDate as StartDate6_, survey_.StartTime as StartTime6_, survey_.StopDate as StopDate6_, survey_.StopTime as StopTime6_, survey_.Comment as Comment6_, survey_.BoundaryNorth as Boundar10_6_, survey_.BoundarySouth as Boundar11_6_, survey_.BoundaryWest as Boundar12_6_, survey_.BoundaryEast as Boundar13_6_ from survey survey_ where survey_.Nation=? and survey_.Platform=? and survey_.Survey=?
Hibernate: select survey_.Nation, survey_.Platform, survey_.Survey, survey_.SurveyTitle as SurveyTi4_6_, survey_.StartDate as StartDate6_, survey_.StartTime as StartTime6_, survey_.StopDate as StopDate6_, survey_.StopTime as StopTime6_, survey_.Comment as Comment6_, survey_.BoundaryNorth as Boundar10_6_, survey_.BoundarySouth as Boundar11_6_, survey_.BoundaryWest as Boundar12_6_, survey_.BoundaryEast as Boundar13_6_ from survey survey_ where survey_.Nation=? and survey_.Platform=? and survey_.Survey=?
11:10:13,765 DEBUG AbstractBatcher:476 - preparing statement
11:10:13,765 DEBUG ShortType:133 - binding '578' to parameter: 1
11:10:13,781 DEBUG ShortType:133 - binding '2471' to parameter: 2
11:10:13,781 DEBUG IntegerType:133 - binding '7' to parameter: 3
11:10:13,781 DEBUG StringType:172 - returning 'jkl' as column: SurveyTi4_6_
11:10:13,781 DEBUG IntegerType:172 - returning '20070814' as column: StartDate6_
11:10:13,796 DEBUG IntegerType:172 - returning '0' as column: StartTime6_
11:10:13,796 DEBUG IntegerType:172 - returning '20070814' as column: StopDate6_
11:10:13,796 DEBUG IntegerType:172 - returning '23590000' as column: StopTime6_
11:10:13,796 DEBUG StringType:172 - returning '' as column: Comment6_
11:10:13,812 DEBUG FloatType:172 - returning '0.0' as column: Boundar10_6_
11:10:13,812 DEBUG FloatType:172 - returning '0.0' as column: Boundar11_6_
11:10:13,812 DEBUG FloatType:172 - returning '0.0' as column: Boundar12_6_
11:10:13,812 DEBUG FloatType:172 - returning '0.0' as column: Boundar13_6_
11:10:13,812 DEBUG AbstractBatcher:366 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
11:10:13,828 DEBUG AbstractBatcher:525 - closing statement
11:10:24,937 DEBUG IdentifierValue:77 - id unsaved-value strategy UNDEFINED
11:10:25,796 DEBUG AbstractEntityPersister:1013 - Getting current persistent state for: [no.imr.lsss.database.tables.hibernate.Observationtype#3000]
11:10:25,796 DEBUG AbstractBatcher:358 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
11:10:25,796 DEBUG SQL:393 - select observatio_.ObservationType, observatio_.ObservationTypeName as Observat2_17_ from observationtype observatio_ where observatio_.ObservationType=?
Hibernate: select observatio_.ObservationType, observatio_.ObservationTypeName as Observat2_17_ from observationtype observatio_ where observatio_.ObservationType=?
11:10:25,812 DEBUG AbstractBatcher:476 - preparing statement
11:10:25,843 DEBUG ShortType:133 - binding '3000' to parameter: 1
11:10:25,859 DEBUG StringType:172 - returning 'Scattered Fish Data' as column: Observat2_17_
11:10:25,859 DEBUG AbstractBatcher:366 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
11:10:25,859 DEBUG AbstractBatcher:525 - closing statement
I interpret the line
11:10:13,750 DEBUG AbstractEntityPersister:1013 - Getting current persistent state for:
to mean that hibernate is checking with the database if this object already exists?
(the insert is of course done later when I do Transaction:commit() )
Sorry for the long post and thanks in advance.