I have created a one-to-one mapping between two classes (photo and thumbnail). When I load a photo i get a hibernate exception:
Code:
org.springframework.orm.hibernate3.HibernateSystemException: More than one row with the given identifier was found: 1, for class: com.thc.model.Photo; nested exception is org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: com.thc.model.Photo
Caused by: org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: com.thc.model.Photo
I have the following class structure:
Code:
public class Photo {
...
private Thumbnail thumbnail;
...
@OneToOne()
@org.hibernate.annotations.Cascade( { org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE })
public Thumbnail getThumbnail() {
return thumbnail;
}
public void setThumbnail(Thumbnail thumbnail) {
this.thumbnail = thumbnail;
}
}
public class Thumbnail {
...
private Photo photo;
...
@OneToOne(mappedBy="thumbnail")
public Photo getPhoto() {
return photo;
}
public void setPhoto(Photo photo) {
this.photo = photo;
}
}
Here is my dao(PhotoDaoImpl):
Code:
public List findAll() {
return getHibernateTemplate().find("from Photo");
}
Here is the test which triggers the exception(PhotoDaoTest):
Code:
public void testFindAllPhotos() {
List photos = photoDao.findAll();
assertEquals(1, photos.size());
}
Here is the test data (using dbunit)
Code:
<photo id="1" name="photo_1" description="photo_1_desc" thumbnail_id="1" />
<thumbnail id="1" />
I believe the problem lies in the one-to-one mapping between Photo and thumbnail because if I remove this mapping the test runs ok.
I have also noticed this in my logs:
Code:
[INFO] PhotoDaoTest Began transaction (1): transaction manager [org.springframework.orm.hibernate3.HibernateTransactionManager@2ffd3e30]; default rollback = true
Hibernate: select photo0_.id as ...
Hibernate: select thumbnail0_.id as ...
Hibernate: select photo0_.id as ...
[INFO] PhotoDaoTest Rolled back transaction after test execution
So it appears that hibernate is loading the photo then the thumbnail but it then goes on to reload the photo again in the same transaction
Can anyone see anything wrong with my mappings?
Cheers
Toby