0

I am trying to map a non primary key column between 2 tables using one-to-one mapping through JPA. The OneToOne is not performing the join on the mentioned column rather it is picking up the Id field.

Below is the table structure:

Person table id (PK) name college

College table id (PK) clg_name location

Location table id (PK) loc_name

I need to provide OneToOne mapping between College and Location using the columns location and loc_name respectively. I have tried using the @NaturalId, @MapsId and by providing the reference column name. Still it uses the id field

//person

@Entity
@Table(name = "PERSON", schema = "DETAILS")
@SecondaryTables({
@SecondaryTable(name = "COLLEGE", schema = "DETAILS")
})
class Person{

Person(){
    this.college = new College();
}

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

@Column(name = "NAME", nullable = false)
private String name;

@Column(name = "COLLEGE_NAME", table = "COLLEGE", nullable = false)
private String college;

@OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER,   mappedBy = "person")
    @JoinColumn(name = "ID")
   private College college;

//getter setters

}

//college

@Entity
@Table(name = "COLLEGE", schema = "DETAILS")
class College{

College(){

}

@Id
    @MapsId
    @OneToOne()
    @JoinColumn(name = "ID")
    private Person person;

@OneToOne(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, mappedBy = "college")
   @JoinColumn(referencedColumnName = "LOC_NAME")
private Location location;

@Column(name = "LOCATION", nullable = false)
private String loc;

//getter setters

 }

//location

@Entity
@Table(name = "LOCATION", schema = "DETAILS")
class Location{
Location(){}

@Id
@Column(name = "ID")
private Long collegeId;

@MapsId
@OneToOne()
@JoinColumn(name = "LOC_NAME", referencedColumnName ="LOCATION", nullable = false, unique = true)
private College college;

@Column(name = "LOC_NAME", nullable = false)
private String locName;

//getter setters

 }

In the above code, I am facing in OneToOne mapping issue using the location name columns. I am querying the Person object from the JPA repository by querying "from Person p where p.id = :id".

The generated JPA queries in logs for 1to1 mapping appears to be

select from details.college college0_ left outer join details.location location1_ on college0_.id=location1_.locName where college0_.id=?

   Error: 
   Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause


  Caused by: java.sql.SQLSyntaxErrorException: ORA-01722: invalid number

at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:513)

If I remove @MapsId from Location then I get below error:

  org.hibernate.AnnotationException: A Foreign key refering has the wrong number of column. should be 0
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
Pooja
  • 3
  • 2
  • This is all a bit confusing. I am unable to determine exactly what the relationships should be between these entities. Maybe you could add an image of these database schema with the 3 tables and their columns. – Alan Hay Jul 23 '19 at 10:50
  • How can a `@OneToOne` association possibly be designated as mapping the id which is a number *and at the same time* reference a non-id property of the target, which is a string? You can't have the cake and eat it, too – crizzis Jul 23 '19 at 17:30

0 Answers0