0

i have three entities as below:

  1. Property
  2. Property_Document
  3. Property_User

Property and Property_Document have a One to Many relationship.
Property and Property_User has One to One relationship.

At the database level, Property_User has foreign key "property_id", back to the Property table.

I am using annotations to define the mappings and relationships between the entities.

Property.java

public class Property {
    //.....
    @OneToMany(cascade=CascadeType.ALL)
    @JoinColumn(name="property_id")
    private List<PropertyDocuments> docs;

    @OneToOne(cascade=CascadeType.ALL)
    private PropertyUser owner;
    //....
}

PropertyUser.java

public class PropertyUser {
    @Id
    private int id;

    @column
    private String name;
    //......
}

When it fetches the property, document list fetches successfully but when it tries to fetch the user information it shows below error message.

column: owner_id doesn't exist.

Please help. Thanks.

Perception
  • 79,279
  • 19
  • 185
  • 195
Ankur Raiyani
  • 1,509
  • 5
  • 21
  • 49

1 Answers1

2

You need a column in the "property" table to hold the ID of the User it references. Since you didn't specify any column in your mapping, Hibernate uses the default column name, which is owner_id (the name of the property followed by the name of the column it references). If you want to use another column name, you have to tell Hibernate: it can't guess it magically:

@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "the_column_containing_the_id_of_the_referenced_user")
private PropertyUser owner;
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • i have already assign a foreign key in Property_User table. If i specify that key here it did not work. – Ankur Raiyani Jun 10 '12 at 13:16
  • *How* didn't it work. When something "doesn't work", you have an exception with a message and a stack trace which helps diagnosing the problem. Or at least something happens which is different from what you expect. "It doesn't work" is not sufficient to diagnose what the problem might be. – JB Nizet Jun 10 '12 at 13:19
  • i mean that when i specify like @JoinColumn("property_id") the select query for property throws an exception that property.property_id doesn't exist. Because Property table doesn't have that column. – Ankur Raiyani Jun 10 '12 at 13:21
  • Of course, if you specify column names which don't exist, it won't work. Specify the name of the actual column, which exists, and references the user ID. And if there is no column in the table to hold the user ID, then create one. – JB Nizet Jun 10 '12 at 13:23
  • But i don't wan to create a reference for the user in Property table. As i have already specified a reference in Property_User table for property. – Ankur Raiyani Jun 10 '12 at 13:25
  • Then the OneToOne association must be mapped in the PropertyUser entity, instead of, or in addition to the Property entity. The owning side must be PropertyUser, because it's the side that has the join column. – JB Nizet Jun 10 '12 at 13:42