-1

I have a problem in using @ManyToOne relation, because my foreign key and primary key have same name, so Hibernate is considering the one i mapped as the current table's column name. So i thought i will use table attribute of @JoinColumn annotations, but it seems to be not working. I gave actual table for it's value, but it is not taking. Tried the Class-name as well. below are the Entities. You can see PRJ_NBR is same in both tables. So when i try to run the project i'm getting error like Cannot find the expected secondary table: no HEAD available

If I remove table attribute from @JoinColumn I'm getting error - Repeated column in mapping for entity: com.example.jpademo.Detail column: PRJ_NBR (should be mapped with insert="false" update="false")

@Entity(name="Head")
@Table(name = "HEAD")
public class QuoteHead {
@Id
@Column(name = "PRJ_NBR")
private Integer projNumber;
@Column(name = "CUS_SYS_ID")
private Integer cusSysId;
@OneToMany(mappedBy = "head", cascade = CascadeType.ALL, orphanRemoval = 
true)
private List<Detail> details = new ArrayList<Detail>();
}

@Entity
@Table(name = "DETAIL")
@JsonIgnoreProperties
public class Detail {
@Transient
private Integer projectNumber;
@Transient
private Integer itemNumber;
@Transient
private Integer sequenceNumber;

@ManyToOne
@JoinColumn(name = "PRJ_NBR", table = "HEAD")
private Head head;

@JsonIgnore
@EmbeddedId
private DetailCompositeId id;
}
@Embeddable
public class DetailCompositeId implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "PRJ_NBR")
private Integer projectNumber;
@Column(name = "ITM_NBR")
private Integer itemNum;
@Column(name = "SEQ_NBR")
private Integer sequenceNumber;
}
  • Please share your entities (and the relevant db schema as well). It's not quite clear what you mean by: 'my foreign key and primary key have same name' – crizzis Oct 02 '17 at 15:49
  • Will update the description –  Oct 02 '17 at 16:38

2 Answers2

0

Remove 'table' and try putting insertable="false" , updatable="false" then I think it should work

Madhu Reddy
  • 371
  • 1
  • 4
  • 12
  • This worked after adding annotation @PersistenceContext(type = PersistenceContextType.EXTENDED) in DAO :) –  Oct 18 '17 at 06:30
0

You can utilize the @MapsId annotation.

See this post: can someone please explain me @MapsId in hibernate?

You can remove @JoinColumn in your @ManyToOne mapping and replace it with @MapsId, as shown below:

@ManyToOne
@MapsId("projectNumber")
private Head head;
Ish
  • 3,992
  • 1
  • 17
  • 23
  • Didn't work. Since i'm having composite key i'm getting below error. Foreign key (FKDEBBB5FE3D4F36A7: DETAIL [head_PRJ_NBR])) must have same number of columns as the referenced primary key (HEAD [ITM_NBR,head_PRJ_NBR,SEQ_NBR]) –  Oct 03 '17 at 05:29