vlad wrote:
The Schedule has a @OneToOne association with UserSchedule via a FK.
But then, the UserSchedule also has @OneToOne to Schedule which is not mappedBy.
That's not how you definie a bidirectional association. Check out
this article for more details of how you should do it.
The use
IDENTITY is also problematic if the underlying DB supports sequences.
The default EAGER fetching for all @OneToOne and @ManyToOne can
cause serious performance problems.
Fix the bidirectional association issue as explained in the linked article, and the problem should be fixed.
I initially had mappedBy on UserSchedule but didn't work and after googleing around, saw a post that have @JoinColumn on both sides but still no luck.
Anyway, I've just made changes to my Schedule and UserSchedule entities.
Code:
@Entity
@Table(name = "schedule")
public class Schedule extends ScheduleBase {
@Setter
@Getter
@OneToOne
@JoinColumn
private ProjectLite project;
@Setter
@Getter
private Long lat;
@Setter
@Getter
private Long lon;
@Setter
@Getter
private String title;
@Setter
@Getter
private String description;
@Getter
@Formula("(select v.status from voucher v where v.schedule_id = id)")
private Integer redemptionStatus;
@Setter
@Getter
@JoinColumn(name = "id", referencedColumnName = "schedule_id")
@OneToOne(fetch = FetchType.LAZY)
private UserSchedule userSchedule;
@Entity
@Table(name = "user_schedule")
public class UserSchedule extends BaseEntity {
@Setter
@Getter(onMethod = @__(@JsonIgnore))
@Column(name = "user_id")
private long userId;
@Setter
@Getter
@OneToOne(fetch = FetchType.LAZY, mappedBy = "userSchedule")
private Schedule schedule;
}
and I still get
Code:
Hibernate: select schedule0_.id as id1_10_, schedule0_.created_at as created_2_10_, schedule0_.end_date as end_date3_10_, schedule0_.end_time as end_time4_10_, schedule0_.frequency as frequenc5_10_, schedule0_.frequency_val as frequenc6_10_, schedule0_.is_active as is_activ7_10_, schedule0_.start_date as start_da8_10_, schedule0_.start_time as start_ti9_10_, schedule0_.updated_at as updated10_10_, schedule0_.description as descrip11_10_, schedule0_.lat as lat12_10_, schedule0_.lon as lon13_10_, schedule0_.project_id as project15_10_, schedule0_.title as title14_10_, (select v.status from voucher v where v.schedule_id = schedule0_.id) as formula1_ from schedule schedule0_ inner join user_schedule userschedu1_ on schedule0_.id=userschedu1_.id limit ?
where I think userschedu1_.id should be userschedu1_.schedule_id,