0

i have two tables which has one-to-one mapping on a column. i'd like to set up unidirectional one-to-one hibernate mapping using annotations because i want to be able to get results from joined tables, but do not want to do foreign constraint check for child table.

here's my quick code.

@Entity
@Table(name = "student")
public class student {

// primary key and other column definition here..

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="address_id")
    private Address address;

// getters and //setters
}

@Entity
@Tale(name = "address") 
public class address {
    @Id
    @GeneratedValue(generator = "hibernate-uuid")
    @GenericGenerator(name = "hibernate-uuid", strategy = uuid")
    @Column(name = "id", unique = true, length = 36, nullable = false)
    private String id;

    @Column(name="address_id")
    private String address_id;

    // getters and setters
}

above gives me following exception when i try to insert into student (but not the address).

Caused by: org.hibernate.AnnotationException: A Foreign key refering com.test.Student from com.test.Address has the wrong number of column. should be 2

what am i doing wrong here?

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
user3590506
  • 171
  • 1
  • 4
  • 16
  • If you are new to Hibernate you might find my tutorial [here](http://stackoverflow.com/questions/24257449/how-do-i-use-annotations-to-define-x-relationship-in-hibernate-4-and-spring) to be of some use. – JamesENL Jul 17 '14 at 04:19

1 Answers1

2

First, change cascade type to REFRESH in student class (better to remove it). Then if it is unidirectional, means the other part won't know anything of it. So you should add referenceColumnName to your @OneToOne

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PK_OF_THE_OTHER_TABLE", referencedColumnName = "CURRENT_TABLE_COLUMN_REFERENCE", nullable = false, insertable = false, updatable = false)

By this implementation, you will not update the other part but joining and get information you need

UPDATE -- PK_OF_THE_OTHER_TABLE is the primary key of address table. Also CURRENT_TABLE_COLUMN_REFERENCE is your Foreign Key (current table's field which is pointing to address table) - I assumed you put your one to one relationship in Student entity.

pms
  • 944
  • 12
  • 28
  • thanks for the suggestion. i updated my code with your suggestion except i excluded nullable=false part. but i get exception because returning result set from join table asks for PK_OF_THE_OTHER_TABLE from student table which doesn't exist. – user3590506 Jul 16 '14 at 23:16
  • i eventually googled and found out that name and referenceColumnName value was swapped in your first posting. but nevertheless i took your approach and so far it seems to work. only issue i'm facing now is that when i try to fetch a record from joint table when there's no records to join, hibernate complains about unknown column from child table. but that's a different issue. thanks. – user3590506 Jul 17 '14 at 20:51
  • not a problem. By the way, referencedColumnName is described here: http://www.objectdb.com/api/java/jpa/JoinColumn/referencedColumnName – pms Jul 17 '14 at 20:58