Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA MappingException: Repeated column in mapping for entity

 
Sandeep Murthy
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have 2 columns in a table, both of which refer to a same column in the parent table. This is causing me mapping exception when I'm designing my JPA entities.

For example, In the substitute_item table both item_id and sub_item_id reffer to the item_id in item table, as follows:

substitute_item
-------------------------------
id                      100
item_id               1 [ --> item :: item_id ]
sub_item_id        2 [ --> item :: item_id ]

id                      101
item_id               1 [ --> item :: item_id ]
sub_item_id        3 [ --> item :: item_id ]

item
-------------------------------
item_id                      1
name                      Hard Disk
desc                      Main Storage device

item_id                      2
name                      DVD
desc                      Substitute Storage device

item_id                      3
name                      USB Drive
desc                      Another Substitute Storage device

In the above example, the data in substitute_item table means that the items 2 and 3 are substitute items for item 1.
I know this is a wrong database design, but this is existing schema and I'm helpless here.

My entities are as below:


Now, I'm getting the following exception:



I'm using Spring with JPA, which internally uses Hibernate.
Can anyone please tell me why this exception is occurring? How to do the mapping when 2 columns of a table refer to a same column of another table? How to map with insert="false" update="false"?

Many thanks,
Sandeep
 
Chandrasekhar Elindram
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure, whether the syntax is what you are looking for. If so, here is the syntax:
@JoinColumn(name = column name, insertable=false, updatable=false)

 
xsunil kumar
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why you are using Product in definition of substitute item. Insertable and updatable will not solve this issue. Please try with below code block

@Entity
@Table(name="Substitute_Item")
public class SubstituteItem {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Long id;

@ManyToOne
@JoinColumn(name="item_id")
private Item item1;

@ManyToOne
@JoinColumn(name="sub_item_id")
private Item item2;

Its working and i am able to insert data and pull data.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic