This is driving me nuts. I am trying to get the most simple many-to-one example to work with lazy loading, but the referenced object won't load. When I try to reference it I get "No row with the given identifier exists: [Pet#4294967298]". Please advice!
Database (an owner has a pet)
Note that database is populated as well.
Code:
CREATE TABLE `owner` (
`ID` int(10) unsigned NOT NULL default '0',
`NAME` varchar(45) NOT NULL default '',
`PET_ID` int(10) unsigned default NULL,
PRIMARY KEY (`ID`),
KEY `FK_owner_pet` (`PET_ID`),
CONSTRAINT `FK_owner_pet` FOREIGN KEY (`PET_ID`) REFERENCES `pet` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='InnoDB free: 11264 kB; (`ID`) REFER `test/pet`(`ID`)';
INSERT INTO `owner` (`ID`,`NAME`,`PET_ID`) VALUES
(1,'Rick',1),
(2,'Matt',2),
(3,'R.J.',3);
CREATE TABLE `pet` (
`ID` int(10) unsigned NOT NULL default '0',
`NAME` varchar(45) NOT NULL default '',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `pet` (`ID`,`NAME`) VALUES
(1,'Snoopy'),
(2,'Garfield'),
(3,'Satchel');
Java modelCode:
@Entity
@Table(name = "PET")
public class Pet {
private Long id;
private String name;
@Id
@Column(name = "ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity
@Table(name = "OWNER")
public class Owner {
private Long id;
private String name;
private Pet pet;
@Id
@Column(name = "ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne(cascade={CascadeType.PERSIST}, fetch=FetchType.LAZY)
@JoinColumn(name="PET_ID")
public Pet getPet() {
return pet;
}
public void setPet(Pet pet) {
this.pet = pet;
}
}
Hibernate version: Hibernate 3.2, Annotations 3.3.0
hibernate.cfg.xmlCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.password">******</property>
<property name="connection.url">jdbc:mysql://localhost/test?relaxAutoCommit=true</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping class="Pet"/>
<mapping class="Owner"/>
</session-factory>
</hibernate-configuration>
Code between sessionFactory.openSession() and session.close():Code:
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
Session s = sessionFactory.openSession();
Query ownerQ = s.createQuery("from Owner");
List<Owner> owners = ownerQ.list();
Owner owner = owners.get(1);
System.out.println("Pet: " + owner.getPet().getName());
s.close();
Full stack trace of any exception that occurs:The exception occurs at the System.out.println. An owner object without the pet is retrieved successfully before I try to reference the pet.name.
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [Pet#4294967298]
at org.hibernate.impl.SessionFactoryImpl$1.handleEntityNotFound(SessionFactoryImpl.java:377)
at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:79)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:68)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
at Pet$$EnhancerByCGLIB$$dc7036d2.getName(<generated>)
at Vet.main(Vet.java:20)
Name and version of the database:MySql 4.1
Debug level Hibernate log excerpt:17:59:50,984 INFO Version:15 - Hibernate Annotations 3.3.0.GA
17:59:51,015 INFO Environment:514 - Hibernate 3.2.5
17:59:51,015 INFO Environment:547 - hibernate.properties not found
17:59:51,015 INFO Environment:681 - Bytecode provider name : cglib
17:59:51,031 INFO Environment:598 - using JDK 1.4 java.sql.Timestamp handling
17:59:51,125 INFO Configuration:1426 - configuring from resource: /hibernate.cfg.xml
17:59:51,125 INFO Configuration:1403 - Configuration resource: /hibernate.cfg.xml
17:59:51,421 INFO Configuration:1541 - Configured SessionFactory: null
17:59:51,515 INFO AnnotationBinder:398 - Binding entity from annotated class: Pet
17:59:51,578 INFO EntityBinder:420 - Bind entity Pet on table PET
17:59:51,640 INFO AnnotationBinder:398 - Binding entity from annotated class: Owner
17:59:51,640 INFO EntityBinder:420 - Bind entity Owner on table OWNER
17:59:51,750 INFO AnnotationConfiguration:350 - Hibernate Validator not found: ignoring
17:59:51,750 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
17:59:51,750 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 20
17:59:51,750 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
17:59:51,765 INFO DriverManagerConnectionProvider:80 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/test?relaxAutoCommit=true
17:59:51,765 INFO DriverManagerConnectionProvider:86 - connection properties: {user=root, password=****, autocommit=false}
17:59:52,015 INFO SettingsFactory:89 - RDBMS: MySQL, version: 4.1.7-nt
17:59:52,015 INFO SettingsFactory:90 - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.10 ( $Date: 2005/05/19 15:52:23 $, $Revision: 1.1.2.2 $ )
17:59:52,031 INFO Dialect:152 - Using dialect: org.hibernate.dialect.MySQLDialect
17:59:52,046 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
17:59:52,046 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
17:59:52,046 INFO SettingsFactory:143 - Automatic flush during beforeCompletion(): disabled
17:59:52,046 INFO SettingsFactory:147 - Automatic session close at end of transaction: disabled
17:59:52,046 INFO SettingsFactory:154 - JDBC batch size: 15
17:59:52,046 INFO SettingsFactory:157 - JDBC batch updates for versioned data: disabled
17:59:52,046 INFO SettingsFactory:162 - Scrollable result sets: enabled
17:59:52,046 INFO SettingsFactory:170 - JDBC3 getGeneratedKeys(): enabled
17:59:52,046 INFO SettingsFactory:178 - Connection release mode: auto
17:59:52,046 INFO SettingsFactory:202 - Maximum outer join fetch depth: 2
17:59:52,046 INFO SettingsFactory:205 - Default batch fetch size: 1
17:59:52,046 INFO SettingsFactory:209 - Generate SQL with comments: disabled
17:59:52,046 INFO SettingsFactory:213 - Order SQL updates by primary key: disabled
17:59:52,046 INFO SettingsFactory:217 - Order SQL inserts for batching: disabled
17:59:52,046 INFO SettingsFactory:386 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
17:59:52,046 INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory
17:59:52,046 INFO SettingsFactory:225 - Query language substitutions: {}
17:59:52,062 INFO SettingsFactory:230 - JPA-QL strict compliance: disabled
17:59:52,062 INFO SettingsFactory:235 - Second-level cache: enabled
17:59:52,062 INFO SettingsFactory:239 - Query cache: disabled
17:59:52,062 INFO SettingsFactory:373 - Cache provider: org.hibernate.cache.NoCacheProvider
17:59:52,062 INFO SettingsFactory:254 - Optimize cache for minimal puts: disabled
17:59:52,062 INFO SettingsFactory:263 - Structured second-level cache entries: disabled
17:59:52,062 INFO SettingsFactory:290 - Statistics: disabled
17:59:52,062 INFO SettingsFactory:294 - Deleted entity synthetic identifier rollback: disabled
17:59:52,062 INFO SettingsFactory:309 - Default entity-mode: pojo
17:59:52,062 INFO SettingsFactory:313 - Named query checking : enabled
17:59:52,109 INFO SessionFactoryImpl:161 - building session factory
17:59:52,421 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
17:59:52,546 DEBUG QueryTranslatorImpl:246 - parse() - HQL: from Owner
17:59:52,562 DEBUG AST:266 - --- HQL AST ---
\-[QUERY] 'query'
\-[SELECT_FROM] 'SELECT_FROM'
\-[FROM] 'from'
\-[RANGE] 'RANGE'
\-[IDENT] 'Owner'
17:59:52,562 DEBUG ErrorCounter:68 - throwQueryException() : no errors
17:59:52,609 DEBUG HqlSqlBaseWalker:111 - select << begin [level=1, statement=select]
17:59:52,640 DEBUG FromElement:108 - FromClause{level=1} : Owner (no alias) -> owner0_
17:59:52,640 DEBUG HqlSqlBaseWalker:117 - select : finishing up [level=1, statement=select]
17:59:52,640 DEBUG HqlSqlWalker:509 - processQuery() : ( SELECT ( FromClause{level=1} OWNER owner0_ ) )
17:59:52,640 DEBUG HqlSqlWalker:716 - Derived SELECT clause created.
17:59:52,656 DEBUG JoinProcessor:148 - Using FROM fragment [OWNER owner0_]
17:59:52,656 DEBUG HqlSqlBaseWalker:123 - select >> end [level=1, statement=select]
17:59:52,656 DEBUG AST:232 - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (OWNER)
+-[SELECT_CLAUSE] SelectClause: '{derived select clause}'
| +-[SELECT_EXPR] SelectExpressionImpl: 'owner0_.ID as ID1_' {FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=OWNER,tableAlias=owner0_,origin=null,colums={,className=Owner}}}
| \-[SQL_TOKEN] SqlFragment: 'owner0_.NAME as NAME1_, owner0_.PET_ID as PET3_1_'
\-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[], fromElementByTableAlias=[owner0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}
\-[FROM_FRAGMENT] FromElement: 'OWNER owner0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=OWNER,tableAlias=owner0_,origin=null,colums={,className=Owner}}
17:59:52,656 DEBUG ErrorCounter:68 - throwQueryException() : no errors
17:59:52,671 DEBUG QueryTranslatorImpl:216 - HQL: from Owner
17:59:52,671 DEBUG QueryTranslatorImpl:217 - SQL: select owner0_.ID as ID1_, owner0_.NAME as NAME1_, owner0_.PET_ID as PET3_1_ from OWNER owner0_
17:59:52,687 DEBUG ErrorCounter:68 - throwQueryException() : no errors
17:59:52,703 DEBUG AbstractBatcher:366 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
17:59:52,703 DEBUG ConnectionManager:421 - opening JDBC connection
17:59:52,703 DEBUG SQL:401 - select owner0_.ID as ID1_, owner0_.NAME as NAME1_, owner0_.PET_ID as PET3_1_ from OWNER owner0_
17:59:52,703 DEBUG AbstractBatcher:484 - preparing statement
17:59:52,718 DEBUG AbstractBatcher:382 - about to open ResultSet (open ResultSets: 0, globally: 0)
17:59:52,734 DEBUG AbstractBatcher:389 - about to close ResultSet (open ResultSets: 1, globally: 1)
17:59:52,734 DEBUG AbstractBatcher:374 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
17:59:52,734 DEBUG AbstractBatcher:533 - closing statement
17:59:52,750 DEBUG JDBCContext:233 - after autocommit
17:59:52,750 DEBUG ConnectionManager:404 - aggressively releasing JDBC connection
17:59:52,750 DEBUG ConnectionManager:441 - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
17:59:52,750 DEBUG AbstractBatcher:366 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
17:59:52,750 DEBUG ConnectionManager:421 - opening JDBC connection
17:59:52,750 DEBUG SQL:401 - select pet0_.ID as ID0_0_, pet0_.NAME as NAME0_0_ from PET pet0_ where pet0_.ID=?
17:59:52,750 DEBUG AbstractBatcher:484 - preparing statement
17:59:52,750 DEBUG AbstractBatcher:382 - about to open ResultSet (open ResultSets: 0, globally: 0)
17:59:52,750 DEBUG AbstractBatcher:389 - about to close ResultSet (open ResultSets: 1, globally: 1)
17:59:52,750 DEBUG AbstractBatcher:374 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
17:59:52,750 DEBUG AbstractBatcher:533 - closing statement
Code:
Code: