Схема базы данных
Код: Выделить всё
create table if not exists Room
(
id int primary key generated always as identity,
name text not null
);
create table if not exists RoomInfo
(
id int primary key generated always as identity,
room_id int references Room (id),
info_name text
);
Код: Выделить всё
@Entity
@Table(name = "room")
class Room(
@Column(name = "name")
val name: String,
) {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Int = 0
@OneToOne(cascade = [CascadeType.ALL], fetch = FetchType.EAGER, optional = true, mappedBy = "room")
var roomInfo: RoomInfo? = null
}
@Entity
@Table(name = "roominfo")
class RoomInfo(
@Column(name = "info_name")
val infoName: String,
@OneToOne
@JoinColumn(name = "room_id")
val room: Room
) {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Int = 0
}
Код: Выделить всё
Hibernate: select r1_0.id,r1_0.name,ri1_0.id,ri1_0.info_name from room r1_0 left join roominfo ri1_0 on r1_0.id=ri1_0.room_id where r1_0.id=?
Код: Выделить всё
Hibernate: select r1_0.id,r1_0.name from room r1_0
Hibernate: select ri1_0.id,ri1_0.info_name,r1_0.id,r1_0.name from roominfo ri1_0 left join room r1_0 on r1_0.id=ri1_0.room_id where ri1_0.room_id=?
Hibernate: select ri1_0.id,ri1_0.info_name,r1_0.id,r1_0.name from roominfo ri1_0 left join room r1_0 on r1_0.id=ri1_0.room_id where ri1_0.room_id=?
Как я могу сказать, что Hibernate использует oneToOne в качестве левого соединения во всех операциях?
Подробнее здесь: https://stackoverflow.com/questions/790 ... n1-problem