I'm sure this is a duplicate question, but I can't make it work. I have a one-to-zero-or-one relationship with shared primary key between an entity Home and an entity Address.
My problem is that I keep getting an error
Missing column: id
I'm mapping my entities as shown here, but a difference, the column name for my Address entity is not called id but homeId_pf.
Here are my entities:
@Entity
@Table(name = "homes")
@Getter
@Setter
public class Home implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false, updatable = false)
private Long id;
@OneToMany(fetch = EAGER, cascade = {ALL}, orphanRemoval = true)
@JoinColumn(name = "homeId_fk", referencedColumnName = "id", nullable = false)
private Set<Room> rooms = new HashSet<>();
@OneToOne(fetch = EAGER, cascade = {ALL})
@PrimaryKeyJoinColumn
private Address address;
}
@Entity
@Table(name = "addresses")
@Getter
@Setter
public class Address implements Serializable {
@Id
@Column(name = "homeId_pf")
private Long id;
private Integer city;
@MapsId
@OneToOne
@JoinColumn(name = "id")
private Home home;
}
As said, my addresses table has two columns: homeId_pf, that is set as PK and a FK to homes.id and city.
I've also tried setting @JoinColumn(name = "homeId_pf") on the Address class, and @PrimaryKeyJoinColumn(referencedColumnName = "homeId_pf") on the Home entity, but it doesn't help.
There are lots of problems with one-to-one associations, see
The problem is generally that a query Home.address.id = ? is replaced by Home.id = ? as if it was a shared primary key one-to-one (instead of your one-to-zero-or-one).
Hibernate does the same error when working on a foreign key one-to-one.
Are you doing Criteria or HQL query ? In Criteria, you can avoid the issue creating an alias on Home.address and querying alias.id. In HQL, you can make a subquery.
I did not have time to correct the bug in Hibernate, but if you have a test case, I think the problem comes from that code in org.hibernate.engine.JoinHelper.getAliasedLHSColumnNames() :
I think it should also check that type.getLHSPropertyName() is not filled by a "property-ref" attribute in a XML mapping. If you have a test case, it would be good to submit the issue to Hibernate and suggest that fix.