简体   繁体   中英

Hibernate @OneToOne @JoinColumns annotation loading speed

I am trying to create an association between these two entities using Spring Data JPA annotations.

This is my first entity: Actor

@Entity
@IdClass(SharedPrimaryKey.class)
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@Table(name = "ACTOR")
public class Actor implements Serializable {
    @Id
    @Column(name = "ACTOR_NAME")
    private String actorName;

    @Id
    @Column(name = "ACTOR_DOB")
    private String actorDob;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumns({
            @JoinColumn(name="ACTOR_NAME"),
            @JoinColumn(name="ACTOR_DOB")
    })
    private Movie movie;
}

This is my second entity: Movie

@Entity
@IdClass(SharedPrimaryKey.class)
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@Table(name = "MOVIE")
public class Movie implements Serializable {
    @Id
    @Column(name = "ACTOR_NAME")
    private String actorName;

    @Id
    @Column(name = "ACTOR_DOB")
    private String actorDob;

    @Column(name = "MOVIE_TITLE")
    private String movieTitle;
}

The problem I am facing is that when I try to retrieve all movies based on the actor name, the @OneToOne and @JoinColumns functionality is taking too long. I previously had this set as @OneToMany and it was much faster, but this change was required since both tables are using composite primary keys. Which annotations can I use to speed up this query?

" I try to retrieve all movies based on the actor name" It does not see a OneToOne relation to me. Much more a ManyToMany one. Forget about compound id-s. They have no benefit. Lombok @Data annotation is a wrapper around a lot of annotations. @Table does not needed if the table name equals to the entity name.

@Entity
@Data
public class Actor implements Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "ACTOR_ID")
  private String actorId;

  @Column(name = "ACTOR_NAME")
  private String actorName;

  @ManyToMany
  @JoinTable(
  name = "ACTOR_MOVIE", 
  joinColumns = @JoinColumn(name = "actorID", referencedColumnName = "actorID" ), 
  inverseJoinColumns = @JoinColumn(name = "movieID", referencedColumnName = "movieID"))
  private List<Movie> movies = new TArrayList<>();
}

@Entity
@Data
public class Movie implements Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "MOVIE_ID")
  private String movieID;

  @Column(name = "MOVIE_TITLE")
  private String movieTitle;

  @ManyToMany
  private List<Actor> actors = new TArrayList<>();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM