-->
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.  [ 1 post ] 
Author Message
 Post subject: one-to-one mapping gives more than one row exceptions
PostPosted: Thu Aug 28, 2008 8:02 am 
Newbie

Joined: Sat May 03, 2008 5:57 pm
Posts: 4
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.