What causes “java.lang.IllegalArgumentException: Can not set java.lang.Integer field”

送分小仙女□ 提交于 2019-12-13 02:04:32

问题


I cannot resolve these new exceptions

 Can not set java.lang.Integer field GcmRegistraionIdentity.gcmId to GcmRegistraionIdentity

org.hibernate.PropertyAccessException: could not get a field value by reflection getter of GcmRegistraionIdentity.gcmId

My Dynamic Web Project (Jee7) targeted to

GlassFish Server Open Source Edition  4.1  (build 13)

Hibernate

Hibernate Core {4.3.7.Final}

My Persistence.xml

<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="testPU" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>jdbc/testDB</jta-data-source>
        <properties>
            <property name="hibernate.transaction.jta.platform"
                value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>

Heres my EDITED entity class (partial: e.g Getters/Setters NOT SHOWN)

@Entity
@Table(name = "gcm_registration")
public class GcmRegistraionIdentity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "gcm_id", unique = true, nullable = false, insertable = false, updatable = false)
    private Integer gcmId;

    @Column(name = "registration_id")
    private String registraionId = null;

    @Column(name = "created")
    @Temporal(TemporalType.TIMESTAMP)
    private Date created;

    public Integer getGcmId() {
        return gcmId;
    }

    public void setGcmId(final Integer gcmId) {
        this.gcmId = gcmId;
    }

Mysql version is

Version 5.6.22 MySQL Community Server (GPL)

I am running on Mac OS X 10.10.1 (14B25) (Yosemite)

Heres my JAX-RS class

@Path("registrations")
@Stateless
public class RegistrationResource {

    @PersistenceContext
    private EntityManager mEntityManager;

    @POST
    @Path("gcm")
    public void register(final RegistrationIdJson registrationId) {

        final GcmRegistraionIdentity gcmRegistraionIdentity = new GcmRegistraionIdentity();
        gcmRegistraionIdentity.setRegistraionId(registrationId.getRegistrationId());

        mEntityManager.persist(gcmRegistraionIdentity);

    }

}

Heres the DDL for my MySql table

CREATE TABLE `gcm_registration` (
  `gcm_id` int(11) NOT NULL AUTO_INCREMENT,
  `registration_id` varchar(1024) NOT NULL,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`gcm_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

回答1:


I'll recommend a change in the name of id field. Alter the table and update the column to "id". I think that "_id" is doing funny stuff on hibernate.




回答2:


The solution was to replace

Hibernate Core {4.3.7.Final}

with

Hibernate Core {4.3.5.Final}

No other code or configuration changes were required



来源:https://stackoverflow.com/questions/27452749/what-causes-java-lang-illegalargumentexception-can-not-set-java-lang-integer-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!