0

how do i create entity for area_item_table which have foreign key as primary key

the Item entity:

@Entity
@Table(name = Item.ITEM)
public class Item {

  @Id
  @Column(name = ITEM_ID)
  private long itemId;
 
  @OneToOne
  @JoinTable(name = "area_item_table",
      joinColumns = {@JoinColumn(name = ITEM_ID, referencedColumnName = ITEM_ID)},
      inverseJoinColumns = {
          @JoinColumn(name = AREA_ID, referencedColumnName = ID)})
  private Area area;

[removed get set and unrelated fields from entity]
}

the Area entity:

@Entity
@Table(name = Area.AREA)
public class Area {

  @Id
  @Column(name = ID, nullable = false)
  private Long id;

[removed get set and unrelated fields from entity]

the table area_item_table :

item_id area_id
1 121

is there a way to create an Entity for this table without creating new primary key field

kisame
  • 11
  • 5
  • If it is `@OneToOne`, then you don't need an intermediate join table. – XtremeBaumer Jul 19 '22 at 12:36
  • hey XtremeBaumer, without @JoinTable how can specify the table name ? or referenced columns? – kisame Jul 19 '22 at 12:43
  • You drop the table altogether. The target table is defined by the type. Lets say your `Item` table has a `area_id` column. Then in `Item` you do `@OneToOne @JoinColumn(name = "area_id") private Area area;`. If you want the bidirectional mapping, then in `Area` you have a field like `@OneToOne(mappedBy = "area") private Item item;`. Though looking at the entity names, it would rather be `@OneToMany` / `@ManyToOne` – XtremeBaumer Jul 19 '22 at 12:48
  • yes, that works but I didn't want to make any changes to the Item table so I created one table solely for mapping Item to Area. And now I want to create an entity for that table (item_area_table) – kisame Jul 19 '22 at 12:58
  • Why would you need an extra entity for that table? – XtremeBaumer Jul 19 '22 at 13:14
  • I want to implement spring JPA repository for the table, and for that I need the entity. – kisame Jul 20 '22 at 06:30
  • Then I don't really understand your issue. You can simply create an entity, map it to the intermediate table and annotate both columns with `@Id`. Then you just need to adjust fields of `Item` and `Area` to connect to the intermediate entity – XtremeBaumer Jul 20 '22 at 07:59
  • cannot create an entity like this because @Id requires something ```@Entity @Table(name = "item_area_table") public class ItemArea { @Id @Column private Item item; @Column private Area area; } ``` – kisame Jul 20 '22 at 10:23
  • I don't see any reason that wouldn't work – XtremeBaumer Jul 21 '22 at 06:23
  • it expects @IdClass in Entity – kisame Jul 21 '22 at 20:09
  • [I doubt that](https://stackoverflow.com/questions/6833370/jpa-onetoone-with-shared-id-can-i-do-this-better) – XtremeBaumer Jul 22 '22 at 06:24
  • Yes, I also went through that but it didn't help me. but I figured that maybe my spring boot version 1.3.5 is the reason or that question and my problem are different. – kisame Jul 22 '22 at 17:25

0 Answers0