-->
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.  [ 3 posts ] 
Author Message
 Post subject: one-to-one with manually assigned identifiers
PostPosted: Thu Sep 08, 2005 3:48 pm 
Newbie

Joined: Thu Sep 08, 2005 2:14 pm
Posts: 7
one-to-one with manually assigned identifiers

I have a troubles with a one-to-one mapping.

I read the half day through the forums, but couldn't find an appropriate answer.

using hibernate 3.1 beta 2 with annotations 3.1 beta 4


I manually assign guids as my id.

I have an unidirectional one-to-one mapping from class A to class B.

The generated schema includes an foreign key constraint in table A to the primary key in table B.

I configured versioning with int as type. During object instanciation the guid as id is generated by the constructor and zero value ist assigned to version.

In the one-to-one mapping I have defined Cascade.ALL

when i try the following a foreign key violation occurs:

B b = new B();

A a = new A();
a.setB(b);

saverOrUpdate(A);

From what I read from the reference guide about automatic state detection, I would not expect any trouble. Hibernate should recognize, that A AND B are transient and insert it.

Here is the code:

Code:

@Entity
@Table
public class A {
   protected String id;
   protected int version;
   protected B b;
   
   public A() {
      super();
      id = java.util.UUID.randomUUID().toString();
      version = 0;
   }
   
   
   /**
    * I also tried CascadeType.ALL
    */
   @OneToOne(targetEntity=B.class,cascade = {CascadeType.PERSIST,CascadeType.MERGE})
   @JoinColumn(name="b_fk")
   public B getB() {
      return b;
   }

   public void setB(B b) {
      this.b = b;
   }

   @Id
   @Column(name="id", length=36)
   public String getId() {
      return id;
   }
   /**
    * @param id The id to set.
    */
   public void setId(String id) {
      this.id = id;
   }
   
   @Version
   public int getVersion() {
      return version;
   }
   
   public void setVersion(int version) {
      this.version = version;
   }
   
   public final boolean equals(Object other) {
      if(other == this)return true;
      if (other==null ||!(other instanceof A)) return false;
        A castOther = (A) other;
        return id.equals(castOther.getId());
      
   }

   @Override
   public final int hashCode() {
      
      return getId().hashCode();
   }
}


@Entity
@Table
public class B {
   protected String id;
   protected int version;
   
   public B() {
      super();
      id = java.util.UUID.randomUUID().toString();
      version = 0;
   }
   
   @Id
   @Column(name="id", length=36)
   public String getId() {
      return id;
   }

   public void setId(String id) {
      this.id = id;
   }
   
   @Version
   public int getVersion() {
      return version;
   }


   public void setVersion(int version) {
      this.version = version;
   }
   
   public final boolean equals(Object other) {
      if(other == this)return true;
      if (other==null || !(other instanceof B) ) return false;
        B castOther = (B) other;
        return id.equals(castOther.getId());
      
   }
   
   public final int hashCode() {
      
      return getId().hashCode();
   }
}



Any ideas what I am doing wrong?

Thx
Nils


Top
 Profile  
 
 Post subject: nullable=true
PostPosted: Thu Sep 08, 2005 4:01 pm 
Newbie

Joined: Thu Sep 08, 2005 2:14 pm
Posts: 7
I also tried it with nullable=true

Code:
.....
   @OneToOne(targetEntity=B.class,cascade = {CascadeType.PERSIST,CascadeType.MERGE})
   @JoinColumn(name="b_fk",nullable=true,insertable=true)
   public B getB() {
      return b;
   }

.....



Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 6:21 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Before checking in DB, Hibernate compare the id value with an newly instanciated object, if ids differs, then the object is considered as detached and not transiant.
Since you init the id in the constructor, Hibernate is defeated.

_________________
Emmanuel


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

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.