Issue
I have problem when update object in jpa
i have bean user
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
@Column(name = "name", nullable = false)
private String name;
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "fk_program_rating")
private List<Rating> ratingList = new ArrayList<Rating>();
}
and
public class Rating extends BaseModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
@ManyToOne()
@JoinColumn(name = "fk_program_rating", nullable = false)
@ForeignKey(name = "FK_prog_rate")
@OnDelete(action = OnDeleteAction.CASCADE)
private Program program;
}
when try to update that give me exception : could not get a field value by reflection that happen when table rating have rows
ERROR TransactionInterceptor:434 - Application exception overridden by commit exception com.vodafone.visradio.dataaccess.exception.DataAccessException: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.vodafone.visradio.dataaccess.model.Rating.id
any help about this problem
thanks
Solution
Try changing the mapping annotations. Remove the @JoinColumn
annotation from ratingList
and add mappedBy
attribute:
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
private List<Rating> ratingList = new ArrayList<Rating>();
where user
is the property name in the Rating
entity that has a @ManyToOne
association with User
.
Answered By - Debojit Saikia
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)