java Hibernate/JPA:无法通过反射设置器设置字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4976388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:54:01  来源:igfitidea点击:

Hibernate/JPA: could not set a field value by reflection setter

javahibernatejpa

提问by George Armhold

My JPA/Hibernate odyssey continues...

我的 JPA/Hibernate 奥德赛继续......

I am trying to work around this issue, and so I have had to define primitive @Ids in my class that uses 3 entity fields as a composite key. This seems to get me a bit further, but now I'm getting this when persisting:

我正在尝试解决这个问题,因此我不得不在使用 3 个实体字段作为复合键的类中定义原始 @Ids。这似乎让我更进一步,但现在我在坚持时得到了这个:

javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not set a field value by reflection setter of com.example.model.LanguageSkill.stafferId

Here's my composite class:

这是我的复合类:

public class LanguageSkill implements Serializable
{
    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    @Column(name = "Staffer_ID")
    private Long stafferId;

    @Id
    @ManyToOne(cascade = CascadeType.ALL)
    @MapsId(value = "stafferId")
    private Staffer staffer;

    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    @Column(name = "Language_ID")
    private Long languageId;

    @ManyToOne
    @MapsId(value= "languageId")
    private Language language;

    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    @Column(name = "Language_Proficiency_ID")
    private Long languageProficiencyId;

    @ManyToOne
    @MapsId(value= "languageProficiencyId")
    private LanguageProficiency languageProficiency;
}

I do have proper getters and setters (IDE-generated) for both the primitives as well as the entities.

对于原语和实体,我确实有适当的 getter 和 setter(IDE 生成的)。

Here are my libs. I'm not totally convinced that I'm using a compatible set of persistence libraries (references to a cookbook detailing how to properly mix-and-match these would be highly appreciated.)

这是我的库。我并不完全相信我正在使用一组兼容的持久性库(对详细介绍如何正确混合和匹配这些的食谱的参考将受到高度赞赏。)

  • Hibernate 3.5.6-SNAPSHOT
  • hibernate-jpamodelgen 1.1.0.CR1
  • hibernate-validator 3.1.0.GA
  • MySQL 5.1.6
  • jsr250-api 1.0
  • javax.validation validation-api 1.0.0.GA
  • Hibernate 3.5.6-快照
  • 休眠-jpamodelgen 1.1.0.CR1
  • 休眠验证器 3.1.0.GA
  • MySQL 5.1.6
  • jsr250-api 1.0
  • javax.validation 验证-api 1.0.0.GA

Wow, it's frustrating. 3 days now full time trying to solve various issues like this just for basic ORM. I feel defective. :-(

哇,这令人沮丧。现在 3 天全职尝试解决诸如此类的各种问题,仅适用于基本的 ORM。我觉得有缺陷。:-(

回答by Rafael Ruiz Tabares

It seems a correct code. I had problem with this exception when I used Blob[]

这似乎是一个正确的代码。我在使用 Blob[] 时遇到了这个异常问题

@Lob
@Column(name="DOCUMENTO",nullable=false)
private Blob[] documento;

But changing by Byte[], I solved this problem.

但是通过Byte[]改变,我解决了这个问题。

I have only a occurrence, looking Oracle data types, I have seen this LONG is Character data of variable length (A bigger version the VARCHAR2 datatype).

我只有一次出现,查看 Oracle 数据类型,我看到这个LONG 是可变长度的字符数据(VARCHAR2 数据类型的更大版本)。

I assume that your ID is a Integer....Why not change Long by Integer? You must remember that it only accepts primitive types.

我假设您的 ID 是一个整数....为什么不按整数更改 Long?你必须记住它只接受原始类型。

This is my code and it works fine:

这是我的代码,它工作正常:

@Id
@SequenceGenerator(sequenceName="SQ_DOCUMENTO",name="seqDocumento")
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="seqDocumento")
private Integer idDocumento;

I use Hibernate 3.5.6-final, Spring 3.0.4, Junit 4 and Oracle 11g.

我使用 Hibernate 3.5.6-final、Spring 3.0.4、Junit 4 和 Oracle 11g。

回答by Flavio Troia

You have to remove the @GeneratedValueannotations.

您必须删除@GeneratedValue注释。