-->
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: @Id + @OneToOne results in incorrect behavior
PostPosted: Thu Jul 01, 2010 9:23 am 
Regular
Regular

Joined: Wed May 05, 2004 8:01 am
Posts: 53
Following the example from Hibernate annotations documentation:
Code:
@Entity
class MedicalHistory implements Serializable {
  @Id @OneToOne
  @JoinColumn(name = "person_id")
  Person patient;
}

@Entity
public class Person implements Serializable {
  @Id @GeneratedValue Integer id;
}


I have created my model:
Code:
@Entity
public class Publication extends DefaultFieldPersistent {
   @Column(nullable = false)
   private Date            creationDate   = Calendar.getInstance().getTime();

   @Column(nullable = false)
   private String            name;

   @Column(nullable = false)
   private String            author;

   @Column(nullable = false)
   private String            identifier;

   // PDF, EPUB, TEXT, HTML
   @Column(nullable = false)
   private String            format;

   private Integer            licenseCount;
   private Integer            leaseTime;

   public Publication() {
   }
}

@Entity
public class PublicationContent implements Persistent<Publication> {
   @Id
   @OneToOne
   private Publication   publication;

   @Lob
   private Blob      content;

   @SuppressWarnings("unused")
   protected PublicationContent() {
   }
}


Schema generation works fine. Persisting works fine but trying to session.load(PublicationContext.class, publication):
Quote:
org.springframework.orm.hibernate3.HibernateSystemException: Provided id of the wrong type for class com.mobilebox.biblio.model.PublicationContent. Expected: class com.mobilebox.biblio.model.PublicationContent, got class com.mobilebox.biblio.model.Publication; nested exception is org.hibernate.TypeMismatchException: Provided id of the wrong type for class com.mobilebox.biblio.model.PublicationContent. Expected: class com.mobilebox.biblio.model.PublicationContent, got class com.mobilebox.biblio.model.Publication

which means that Hibernate thinks that the ID class of PublicationContent is PublicationContent which is clearly wrong.

Rewriting the code to match the other example:
Code:
@Entity
class MedicalHistory implements Serializable {
  @Id Integer id;

  @MapsId @OneToOne
  @JoinColumn(name = "patient_id")
  Person patient;
}

@Entity
class Person {
  @Id @GeneratedValue Integer id;
}

session.load(PublicationContext.class, publication.getId()) works ok in this case.

Am I doing something wrong or is it some kind of a bug?


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.