通过反射休眠不能获得字段值 [英] could not get a field value by reflection hibernate

查看:93
本文介绍了通过反射休眠不能获得字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jpa中更新对象时遇到问题



我有bean用户

  public class User $ 
$ b @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =id,unique = true,nullable =假)
私人长ID;
@Column(name =name,nullable = false)
私有字符串名称;
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name =fk_program_rating)
private List< Rating> ratingList = new ArrayList< Rating>();
}



<$ p
$ b @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =id)public class Rating extends BaseModel {

@Id
@GeneratedValue ,unique = true,nullable = false)
私人长ID;
@ManyToOne()
@JoinColumn(name =fk_program_rating,nullable = false)
@ForeignKey(name =FK_prog_rate)
@OnDelete(action = OnDeleteAction.CASCADE )
私人节目节目;
}

当尝试更新时,给我例外:无法获得字段值当表评级有行时发生的反射

$ b


ERROR TransactionInterceptor:434 - 由提交异常覆盖的应用程序异常
com .vodafone.visradio.dataaccess.exception.DataAccessException:org.hibernate.PropertyAccessException:无法通过反射获取字段值getter of com.vodafone.visradio.dataaccess.model.Rating.id

blockquote>

有关此问题的任何帮助

thanks

解决方案

尝试更改映射注释。从 ratingList 中移除 @JoinColumn 注释,并添加 mappedBy 属性:

  @OneToMany(fetch = FetchType.EAGER,mappedBy =user)
private List< Rating> ratingList = new ArrayList< Rating>();

其中 user 评级用户具有 @ManyToOne 关联的实体。

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

解决方案

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.

这篇关于通过反射休眠不能获得字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆