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.  [ 5 posts ] 
Author Message
 Post subject: @OneToOne, unique constraint and redundant? index
PostPosted: Mon Jun 21, 2010 2:19 am 
Newbie

Joined: Sun Jun 20, 2010 5:13 pm
Posts: 3
I have these two classes:

Code:
@Entity
public class Parent implements Serializable {
  @Id
  @GeneratedValue
  private long id;

  @OneToOne(cascade = CascadeType.ALL)
  @PrimaryKeyJoinColumn
  private Child child;
}


and

Code:
@Entity
public class Child implements Serializable {
  @Id
  @GeneratedValue
  long id;

  @OneToOne(cascade = CascadeType.ALL, optional = true)
  private Parent parent;
}



The mapping column "parent_id" in the Child table is not unique. How can I make it unqiue?

If I specify @UniqueConstraint there are 2 indexes in the DB (MySQL) - one from OneToOne relation and one from the constraint. If I go for the foreign key approach with @JoinColumn(unique=true) the MySQL again contains index and unique index.


Top
 Profile  
 
 Post subject: Re: @OneToOne, unique constraint and redundant? index
PostPosted: Mon Jun 21, 2010 3:45 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
you're missing the "mappedBy" property of the @OneToOne annotation, so you're modeling two different relations instead of one bi-directional relation; because of that nothing is stopping the current model from having two Childs pointing to the same Parent.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: @OneToOne, unique constraint and redundant? index
PostPosted: Mon Jun 21, 2010 6:13 am 
Newbie

Joined: Sun Jun 20, 2010 5:13 pm
Posts: 3
You mean adding mappedBy toghether with @PrimaryKeyJoinColumn, or together with @JoinColumn (and FK)?

I'm asking because I already tried @JoinColumn(name="...", unique=true) with mappedBy on the other size and the result was as one would expect - foreign key mapping, but again there were 2 indexes.


Top
 Profile  
 
 Post subject: Re: @OneToOne, unique constraint and redundant? index
PostPosted: Mon Jun 21, 2010 7:05 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
AFAIR you won't need neither @PrimaryKeyJoinColumn nor @JoinColumn, make it working in the simple case first and then see if you still need to "tune" settings.
Just using the 2x@OneToOne properly (having one side marked by mappedBy) should create the effect you're looking for, as far as I understood.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: @OneToOne, unique constraint and redundant? index
PostPosted: Mon Jun 21, 2010 1:50 pm 
Newbie

Joined: Sun Jun 20, 2010 5:13 pm
Posts: 3
@PrimaryKeyJoinColumn was causing incorrect behavior - Parent.getChild would return wrong object - Child with the same id as Parent (due to the performed join I think). Since the relation is 0-1 that's obviously not correct, so I removed it.

I've removed all additional annotations and set mappedBy on the Parent object. Now I have mapping column on Child object, however no unique constraint is created. Is this the intended behavior?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.