Hibernate version:
3.0rc1
database
hsqldb 1.7
I have a simple database with 2 tables.
Contacts (1)-<(n) ContactNumbers
This is bidirectional inversed one to many relationship.
A ContactNumber always belongs to 1 Contact. There can be 0..n ContactNumbers pointing to
any existing Contact.
Just like Product and Parts. A Product (Contact) is made of 0..n Parts (ContactNumbers).
Code:
CREATE CACHED TABLE contacts (
contact_id BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
name VARCHAR(255),
UNIQUE ( name )
);
CREATE CACHED TABLE contact_numbers (
contact_number_id BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
contact BIGINT NOT NULL,
number VARCHAR(255) NOT NULL,
comment VARCHAR(255) NULL,
UNIQUE ( number ),
FOREIGN KEY (contact) REFERENCES contacts (contact_id)
);
Problem:I am trying to list the contents of my table via the code shown at
Code between sessionFactory.openSession() and session.close():I simply get List of all pojos in the table. then I iterate that list and log each
object in that list.
You can see what the tables look like in
Data in the database before runningbefore I run the test application.
This gives me the headache:
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [de.greyshine.telab.model.Contact#2]
I could guess that there is a problem with the <many-to-one> in the ContactNumber.hbm.xml
When hibernate internally sets up my object ContactNumber it tries to look up the matching
Contact. The ContactNumber's foreign key ( property=contact:Contact; column=contact:long/BIGINT )
does map an existing primary key at Contacts.
For me it looks like when a ContactNumber is fetched from the database, setting the regular propertys
works well. But when it comes to the foreign key property which is of type Contact.
Hibernate tries to find the parent pojo Contact with the CONTACT_ID by taking
the CONTACT_NUMBERS_ID and not like expected looking up the foreign key column CONTACT.
->
No row with the given identifier exists: [de.greyshine.telab.model.Contact#2]Of course there's no Contact with that id.
Since I can see when a ContactNumber.setId() is invoked. It reads the table correctly.
Just getting to the point reading the foreign key it messes up. (I hope I messed up and you are
willing and able to help me :-)
This behavior is the same if the id's of Contact and ContactNumber have other values which
are correct and valid.
So I guess it is a mapping problem of the ContactNumber.hbm.xml
But I have no clue what to change.
Pojo: de.greyshine.telab.model.ContactNumber: Code:
public class ContactNumber implements Comparable {
private long id;
private String number;
private Contact contact; // FK
private String comment;
public ContactNumber( ) {}
public long getId() {
return id;
}
public void setId(long id) {
Logger.log("SETTING ID: "+ id +" - "+ this.number );
this.id = id;
}
public String getNumber( ) {
return number;
}
public void setNumber( String number ) {
this.number = number;
}
public Contact getContact( ) {
return contact;
}
public void setContact( Contact contact ) {
this.contact = contact;
}
public String getComment( ) {
return comment;
}
public void setComment( String comment ) {
this.comment = comment;
}
public boolean equals( Object pojo ) {
if ( pojo == this ) {
return true;
} else if ( ( pojo instanceof ContactNumber == false ) || ( this.number == null ) ) {
return false;
}
return this.number.equals( ( (ContactNumber)pojo ).getNumber( ) );
}
public int hashCode( ) {
if ( this.number == null ) {
return 0;
}
int result = this.number.hashCode( );
if ( this.contact != null ) {
result += this.contact.hashCode( );
}
return result * 29;
}
public int compareTo(Object arg0) {
ContactNumber cn = (ContactNumber) arg0;
int result = this.contact.compareTo( cn.contact );
if ( result != 0 )
return result;
return this.number.compareTo( cn.number );
}
public String toString( ) {
return "ContactNumber [id="+ this.id +"; number=" + this.number + "; contact=" + this.contact + "; comment="+ this.comment +"]";
}
}
Pojo: de.greyshine.telab.model.Contact: Code:
public class Contact implements Comparable {
private long id;
private String name;
private Set contactNumbers = new HashSet( );
/**
*
*/
public Contact( ) {}
public long getId( ) {
return id;
}
public void setId( long id ) {
this.id = id;
}
public String getName( ) {
return name;
}
public void setName( String name ) {
this.name = name;
}
public Set getContactNumbers( ) {
return contactNumbers;
}
public void setContactNumbers( Set contactNumbers ) {
this.contactNumbers = contactNumbers;
}
public void addContactNumber( ContactNumber contactNumber ) {
contactNumber.setContact(this);
this.contactNumbers.add( contactNumber );
}
public void removeContactNumber( ContactNumber contactNumber ) {
this.contactNumbers.remove( contactNumber );
}
public String toString( ) {
return "Contact [id="+ this.id +"; name=" + this.name + "]";
}
public boolean equals( Object pojo ) {
if ( this == pojo ) {
return true;
}
if ( ( this.name == null ) || ( pojo instanceof Contact == false ) ) {
return false;
}
return ( this.name.equals( ( (Contact)pojo ).name ) );
}
public int hashCode( ) {
if ( this.name == null ) {
return 0;
}
return this.name.hashCode( );
}
public int compareTo( Object arg0 ) {
return this.name.compareTo( ( (Contact)arg0 ).name );
}
}
Data in the database before runningtable CONTACTS:
Code:
CONTACT_ID NAME
---------- ----
1 Gott
table CONTACT_NUMBERS:
Code:
CONTACT_NUMBER_ID CONTACT NUMBER COMMENT
----------------- ------- ------ ------------
1 1 110 Privat
2 1 112 Petrus Bürro
Mapping documents:CONTACT:
--------
Code:
<hibernate-mapping default-cascade="delete">
<class name="de.greyshine.telab.model.Contact" table="contacts">
<id name="id"
column="contact_id"
type="long">
<generator class="native"/>
</id>
<property name="name"
column="name"
type="string" />
<set name="contactNumbers" inverse="true" >
<key column="contact" />
<one-to-many class="de.greyshine.telab.model.ContactNumber"/>
</set>
</class>
</hibernate-mapping>
CONTACTNUMBER:
--------------
Code:
<hibernate-mapping>
<class name="de.greyshine.telab.model.ContactNumber" table="contact_numbers">
<id name="id"
column="contact_number_id"
type="long">
<generator class="native"/>
</id>
<property name="number"
column="number" />
<property name="comment"
column="comment"/>
<many-to-one name="contact"
column="contact"
class="de.greyshine.telab.model.Contact"
not-null="true" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():-- the code for listing all ContactNumbers
Transaction tx = session.beginTransaction( );
Query query = session.createQuery( "from de.greyshine.telab.model.ContactNumber" );
Iterator iter = query.list().toArray();
tx.commit();
// iterate the list
Logger.log( iter.next() ); // the Logger.log( Object o ) is equally to System.out.println( o.toString() );
-- The pojo's toString():
return "ContactNumber [id="+ this.id +"; number=" + this.number + "; contact=" + this.contact + "; comment="+ this.comment +"]";
Full stack trace of any exception that occurs:Code:
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [de.greyshine.telab.model.Contact#2]
at org.hibernate.ObjectNotFoundException.throwIfNull(ObjectNotFoundException.java:27)
at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:573)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:59)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:80)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:133)
at de.greyshine.telab.model.Contact$$EnhancerByCGLIB$$8868545a.toString(<generated>)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuffer.append(Unknown Source)
at de.greyshine.telab.model.ContactNumber.toString(ContactNumber.java:154)
at de.greyshine.telab.utils.Logger.log(Logger.java:90)
...
rest is my application
The generated SQL (show_sql=true):
Hibernate: select contact0_.contact_id as contact1_0_, contact0_.name as name2_0_ from contacts contact0_ where contact0_.contact_id=?
Debug level Hibernate log excerpt:
Well this is a long one. Sorry.
Lines starting with [<simple_class_name>:<lineNo>] (i.e [SimpleDbTests:84]) are my loggings.
I logged when the setId(long id) is invoked. This is done as first line of code in this setter.
[SimpleDbTests:84] List: "de.greyshine.telab.model.ContactNumber"
08:59:18,738 DEBUG SessionImpl:229 - opened session
08:59:18,748 DEBUG JDBCTransaction:46 - begin
08:59:18,748 DEBUG AbstractBatcher:379 - opening JDBC connection
08:59:18,748 DEBUG DriverManagerConnectionProvider:93 - total checked-out connections: 0
08:59:18,748 DEBUG DriverManagerConnectionProvider:99 - using pooled JDBC connection, pool size: 0
08:59:18,748 DEBUG JDBCTransaction:50 - current autocommit status: false
08:59:18,768 DEBUG SessionImpl:768 - find: from de.greyshine.telab.model.ContactNumber
08:59:18,778 DEBUG QueryParameters:207 - named parameters: {}
08:59:18,778 DEBUG QueryTranslatorImpl:206 - parse() - HQL: from de.greyshine.telab.model.ContactNumber
08:59:18,778 DEBUG AST:226 - --- HQL AST ---
\-[QUERY] CommonAST: 'query'
\-[SELECT_FROM] CommonAST: 'SELECT_FROM'
\-[FROM] CommonAST: 'from'
\-[DOT] CommonAST: '.'
+-[DOT] CommonAST: '.'
| +-[DOT] CommonAST: '.'
| | +-[DOT] CommonAST: '.'
| | | +-[IDENT] CommonAST: 'de'
| | | \-[IDENT] CommonAST: 'greyshine'
| | \-[IDENT] CommonAST: 'telab'
| \-[IDENT] CommonAST: 'model'
\-[IDENT] CommonAST: 'ContactNumber'
08:59:18,788 DEBUG HqlSqlBaseWalker:113 - query() << begin, level = 1
08:59:18,788 DEBUG FromElement:79 - de.greyshine.telab.model.ContactNumber (no alias) -> contactnum0_
08:59:18,788 DEBUG HqlSqlBaseWalker:118 - query() : finishing up...
08:59:18,788 DEBUG HqlSqlWalker:425 - Derived SELECT clause created.
08:59:18,788 DEBUG JoinProcessor:111 - Using FROM fragment [contact_numbers contactnum0_]
08:59:18,788 DEBUG HqlSqlBaseWalker:121 - query() >> end, level = 1
08:59:18,798 DEBUG AST:226 - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (contact_numbers)
+-[SELECT_CLAUSE] SelectClause: '{derived select clause}'
| +-[SELECT_EXPR] SelectExpressionImpl: 'contactnum0_.contact_number_id as contact1_' {FromElement{explicit,not a collection join,classAlias=null,role=null,tableName=contact_numbers,tableAlias=contactnum0_,colums={,className=de.greyshine.telab.model.ContactNumber}}}
| \-[SQL_TOKEN] SqlFragment: 'contactnum0_.number as number1_, contactnum0_.comment as comment1_'
\-[FROM] FromClause: 'from' FromClause{from}
\-[FROM_FRAGMENT] FromElement: 'contact_numbers contactnum0_' FromElement{explicit,not a collection join,classAlias=null,role=null,tableName=contact_numbers,tableAlias=contactnum0_,colums={,className=de.greyshine.telab.model.ContactNumber}}
08:59:18,798 DEBUG QueryTranslatorImpl:177 - HQL: from de.greyshine.telab.model.ContactNumber
08:59:18,798 DEBUG QueryTranslatorImpl:178 - SQL: select contactnum0_.contact_number_id as contact1_, contactnum0_.number as number1_, contactnum0_.comment as comment1_ from contact_numbers contactnum0_
08:59:18,798 DEBUG AbstractBatcher:258 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
08:59:18,808 DEBUG SQL:292 - select contactnum0_.contact_number_id as contact1_, contactnum0_.number as number1_, contactnum0_.comment as comment1_ from contact_numbers contactnum0_
Hibernate: select contactnum0_.contact_number_id as contact1_, contactnum0_.number as number1_, contactnum0_.comment as comment1_ from contact_numbers contactnum0_
08:59:18,808 DEBUG AbstractBatcher:343 - preparing statement
08:59:18,818 DEBUG AbstractBatcher:274 - about to open ResultSet (open ResultSets: 0, globally: 0)
08:59:18,818 DEBUG Loader:377 - processing result set
08:59:18,818 DEBUG Loader:382 - result set row: 0
08:59:18,828 DEBUG Loader:719 - result row: EntityKey[de.greyshine.telab.model.ContactNumber#1]
[ContactNumber:38] SETTING ID: 1 - null
08:59:18,828 DEBUG Loader:864 - Initializing object from ResultSet: [de.greyshine.telab.model.ContactNumber#1]
08:59:18,868 DEBUG BasicEntityPersister:1625 - Hydrating entity: [de.greyshine.telab.model.ContactNumber#1]
08:59:18,878 DEBUG Loader:382 - result set row: 1
08:59:18,878 DEBUG Loader:719 - result row: EntityKey[de.greyshine.telab.model.ContactNumber#2]
[ContactNumber:38] SETTING ID: 2 - null
08:59:18,878 DEBUG Loader:864 - Initializing object from ResultSet: [de.greyshine.telab.model.ContactNumber#2]
08:59:18,878 DEBUG BasicEntityPersister:1625 - Hydrating entity: [de.greyshine.telab.model.ContactNumber#2]
08:59:18,878 DEBUG Loader:399 - done processing result set (2 rows)
08:59:18,878 DEBUG AbstractBatcher:281 - about to close ResultSet (open ResultSets: 1, globally: 1)
08:59:18,888 DEBUG AbstractBatcher:266 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
08:59:18,888 DEBUG AbstractBatcher:363 - closing statement
08:59:18,888 DEBUG Loader:450 - total objects hydrated: 2
08:59:18,888 DEBUG TwoPhaseLoad:96 - resolving associations for [de.greyshine.telab.model.ContactNumber#1]
08:59:18,918 DEBUG DefaultLoadEventListener:193 - loading entity: [de.greyshine.telab.model.Contact#1]
08:59:18,918 DEBUG DefaultLoadEventListener:240 - creating new proxy for entity
08:59:18,918 DEBUG TwoPhaseLoad:167 - done materializing entity [de.greyshine.telab.model.ContactNumber#1]
08:59:18,918 DEBUG TwoPhaseLoad:96 - resolving associations for [de.greyshine.telab.model.ContactNumber#2]
08:59:18,928 DEBUG DefaultLoadEventListener:193 - loading entity: [de.greyshine.telab.model.Contact#2]
08:59:18,928 DEBUG DefaultLoadEventListener:240 - creating new proxy for entity
08:59:18,928 DEBUG TwoPhaseLoad:167 - done materializing entity [de.greyshine.telab.model.ContactNumber#2]
08:59:18,928 DEBUG PersistenceContext:738 - initializing non-lazy collections
08:59:18,928 DEBUG JDBCTransaction:83 - commit
08:59:18,928 DEBUG SessionImpl:292 - automatically flushing session
08:59:18,928 DEBUG AbstractFlushingEventListener:52 - flushing session
08:59:18,928 DEBUG AbstractFlushingEventListener:102 - processing flush-time cascades
08:59:18,938 DEBUG AbstractFlushingEventListener:150 - dirty checking collections
08:59:18,938 DEBUG AbstractFlushingEventListener:167 - Flushing entities and processing referenced collections
08:59:18,968 DEBUG AbstractFlushingEventListener:203 - Processing unreferenced collections
08:59:18,968 DEBUG AbstractFlushingEventListener:217 - Scheduling collection removes/(re)creates/updates
08:59:18,968 DEBUG AbstractFlushingEventListener:79 - Flushed: 0 insertions, 0 updates, 0 deletions to 2 objects
08:59:18,968 DEBUG AbstractFlushingEventListener:85 - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
08:59:18,968 DEBUG Printer:83 - listing entities:
08:59:18,968 DEBUG Printer:90 - de.greyshine.telab.model.ContactNumber{contact=de.greyshine.telab.model.Contact#2, comment=Petrus Bürro, id=2, number=112}
08:59:18,968 DEBUG Printer:90 - de.greyshine.telab.model.ContactNumber{contact=de.greyshine.telab.model.Contact#1, comment=Privat, id=1, number=110}
08:59:18,968 DEBUG AbstractFlushingEventListener:267 - executing flush
08:59:18,968 DEBUG AbstractFlushingEventListener:294 - post flush
08:59:18,968 DEBUG JDBCContext:208 - before transaction completion
08:59:18,978 DEBUG SessionImpl:337 - before transaction completion
08:59:18,978 DEBUG JDBCTransaction:96 - committed JDBC Connection
08:59:18,978 DEBUG JDBCContext:213 - after transaction completion
08:59:18,978 DEBUG SessionImpl:353 - after transaction completion
[SimpleDbTests:98] 2 items.
08:59:18,978 DEBUG DefaultLoadEventListener:326 - attempting to resolve: [de.greyshine.telab.model.Contact#1]
08:59:18,978 DEBUG DefaultLoadEventListener:362 - object not resolved in any cache: [de.greyshine.telab.model.Contact#1]
08:59:18,978 DEBUG BasicEntityPersister:2449 - Materializing entity: [de.greyshine.telab.model.Contact#1]
08:59:18,988 DEBUG Loader:1250 - loading entity: [de.greyshine.telab.model.Contact#1]
08:59:18,988 DEBUG AbstractBatcher:258 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
08:59:18,988 DEBUG SQL:292 - select contact0_.contact_id as contact1_0_, contact0_.name as name2_0_ from contacts contact0_ where contact0_.contact_id=?
Hibernate: select contact0_.contact_id as contact1_0_, contact0_.name as name2_0_ from contacts contact0_ where contact0_.contact_id=?
08:59:18,988 DEBUG AbstractBatcher:343 - preparing statement
08:59:18,988 DEBUG AbstractBatcher:274 - about to open ResultSet (open ResultSets: 0, globally: 0)
08:59:18,988 DEBUG Loader:377 - processing result set
08:59:18,998 DEBUG Loader:382 - result set row: 0
08:59:18,998 DEBUG Loader:719 - result row: EntityKey[de.greyshine.telab.model.Contact#1]
08:59:18,998 DEBUG Loader:864 - Initializing object from ResultSet: [de.greyshine.telab.model.Contact#1]
08:59:18,998 DEBUG BasicEntityPersister:1625 - Hydrating entity: [de.greyshine.telab.model.Contact#1]
08:59:18,998 DEBUG Loader:399 - done processing result set (1 rows)
08:59:18,998 DEBUG AbstractBatcher:281 - about to close ResultSet (open ResultSets: 1, globally: 1)
08:59:18,998 DEBUG AbstractBatcher:266 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
08:59:18,998 DEBUG AbstractBatcher:363 - closing statement
08:59:18,998 DEBUG Loader:450 - total objects hydrated: 1
08:59:19,008 DEBUG TwoPhaseLoad:96 - resolving associations for [de.greyshine.telab.model.Contact#1]
08:59:19,018 DEBUG CollectionLoadContext:134 - creating collection wrapper:[de.greyshine.telab.model.Contact.contactNumbers#1]
08:59:19,028 DEBUG TwoPhaseLoad:167 - done materializing entity [de.greyshine.telab.model.Contact#1]
08:59:19,028 DEBUG PersistenceContext:738 - initializing non-lazy collections
08:59:19,028 DEBUG Loader:1278 - done entity load
[SimpleDbTests:102] ContactNumber [id=1; number=110; contact=Contact [id=1; name=Gott]; comment=Privat]
08:59:19,038 DEBUG DefaultLoadEventListener:326 - attempting to resolve: [de.greyshine.telab.model.Contact#2]
08:59:19,038 DEBUG DefaultLoadEventListener:362 - object not resolved in any cache: [de.greyshine.telab.model.Contact#2]
08:59:19,038 DEBUG BasicEntityPersister:2449 - Materializing entity: [de.greyshine.telab.model.Contact#2]
08:59:19,038 DEBUG Loader:1250 - loading entity: [de.greyshine.telab.model.Contact#2]
08:59:19,038 DEBUG AbstractBatcher:258 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
08:59:19,038 DEBUG SQL:292 - select contact0_.contact_id as contact1_0_, contact0_.name as name2_0_ from contacts contact0_ where contact0_.contact_id=?
Hibernate: select contact0_.contact_id as contact1_0_, contact0_.name as name2_0_ from contacts contact0_ where contact0_.contact_id=?
08:59:19,049 DEBUG AbstractBatcher:343 - preparing statement
08:59:19,049 DEBUG AbstractBatcher:274 - about to open ResultSet (open ResultSets: 0, globally: 0)
08:59:19,049 DEBUG Loader:377 - processing result set
08:59:19,049 DEBUG Loader:399 - done processing result set (0 rows)
08:59:19,049 DEBUG AbstractBatcher:281 - about to close ResultSet (open ResultSets: 1, globally: 1)
08:59:19,049 DEBUG AbstractBatcher:266 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
08:59:19,049 DEBUG AbstractBatcher:363 - closing statement
08:59:19,079 DEBUG Loader:450 - total objects hydrated: 0
08:59:19,079 DEBUG PersistenceContext:738 - initializing non-lazy collections
08:59:19,079 DEBUG Loader:1278 - done entity load
Thanx a lot for any help.