Title: Change the way hibernate looks up related data?
Tags: java;hibernate;spring-boot;spring-data-jpa
Question: Let's say I have the following "parent" pojo...
@Entity
@Table(name = "parent")
public class Parent{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(fetch=FetchType.LAZY, mappedBy = "parent", cascade = {CascadeType.ALL})
@JsonIgnoreProperties("parent")
List<Child&gt; children;
and I have the following child POJO :
@Entity
@Table(name = "child")
public class Child{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name="parent_id")
private Parent parent;
The result of this will be that I have two tables, and my jpa repository will perform queries using the parent_id field within the child table.
However, What if I want it be like a lookup, where by there is a third table for the relationship, where I have the child id and the parent id as a row, and that would be the relationship? can I modify my spring - jpa / hibernate setup for that? If so, some help would be appreciated!
Comment: What are you looking to gain by having a third table that your current set up cannot handle?

Comment: You're looking for `@JoinTable`.

Comment: @roger I need to be able to handle this lookup scenario because this system is replacing an old one, and I have to be able to handle migrating data from this lookup, pending client spec confirmation, the new system may also keep this structure. I would prefer to use the foreign key lookup that is standard, but alas, it ain't up to me. :(



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:03/11/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><hibernate><spring-boot><spring-data-jpa>', 'Title': 'Change the way hibernate looks up related data?', 'ViewCount': 18, 'id': 55111637, 'user_AboutMe': None, 'user_AccountId': 2033926.0, 'user_DisplayName': 'Slippy', 'user_Reputation': 1123.0, 'user_Views': 144.0, 'user_id': 1816785.0}}