Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

JPA MappingException: Repeated column in mapping for entity

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic