Reputation: 2924
I have a class with 2 fields marked with @Id
@Entity
@Table(name="baspas")
class BasPas
@Id
@ManyToOne
@JoinColumn(name="bas_id", referenceColumnName="id")
private Bas basboard;
@Id
@ManyToOne
@JoinColumn(name = "pas_id", referenceColumnName = "id")
private pas pasboard;
//
I refactored them to an Embedded class and pulled the above two @Id fields in the BasPasPK class. This will enable me to create an interface which will extend the JPARepository interface.
@Embeddable
class BasPasPK {
@ManyToOne
@JoinColumn(name="bas_id", referenceColumnName="id")
private Bas basboard;
@ManyToOne
@JoinColumn(name = "pas_id", referenceColumnName = "id")
private pas pasboard;
//
}
As both these fields are annotated @ManyToOne there is another end of the relationship, where in these fields are listed with "mappedBy". for eg.
@Entity
class Another{
.
.
@OneToMany(mappedBy = "basboard" cascade = CascadeType.ALL)
private set<BasPas> basPas;
.
.
.
}
But after refactoring how to access the other end of the class.
What I mean is when I am doing mvn spring-boot:run I am getting the following exception
org.hibernate.AnnotationException: mappedBy reference an unknown target entity property
then what I did was to change the class name in
@Entity
class Another{
.
.
@OneToMany(mappedBy = "basboard" cascade = CascadeType.ALL)
private Set<BasPas> basPas;
.
.
.
}
to this
class Another{
.
.
@OneToMany(mappedBy = "bas" cascade = CascadeType.ALL)
private set<BasPasPk> basPas; //changed the classname in angle brackets to BasPasPk
.
.
.
}
But after this I started getting this following exception.
org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class.
How to fix this, I mean how to access these properties in the other class after pulling those two property in the embedded class.
Upvotes: 0
Views: 673
Reputation: 3423
try it this way: (Assuming the name of the BasPasPK property in your entity is id
)
@Entity
class Bas{
.
.
@OneToMany(mappedBy = "id.basboard" cascade = CascadeType.ALL)
private Set<BasPas> basPas;
.
.
.
}
Upvotes: 2