-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: JpaSystemException - Error accessing field
PostPosted: Fri Dec 02, 2016 12:51 pm 
Newbie

Joined: Sat Nov 26, 2016 3:20 am
Posts: 10
I want to make a query that I can find notes on the user ID (database DDL is at the very bottom), but I have issue: org.springframework.orm.jpa.JpaSystemException: Error accessing field [private long ru.mrchebik.model.User.USER_ID] by reflection for persistent property [ru.mrchebik.model.User#USER_ID] : 1; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private long ru.mrchebik.model.User.USER_ID] by reflection for persistent property [ru.mrchebik.model.User#USER_ID] : 1

Full stack of issue: http://pastebin.com/Av8uirGh

How can you see, I have and this exception: IllegalArgumentException: Can not set long field ru.mrchebik.model.User.USER_ID to java.lang.Long

And this, a class of Note, User and method Spring Data:

Note:
Code:
package ru.mrchebik.model;

import javax.persistence.*;

/**
* Created by mrchebik on 23.07.16.
*/
@Entity
@Table(name = "Notes")
public class Note {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique = true)
    private long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "USER_ID", nullable = false)
    private User user;

    @Column(nullable = false, length = 25)
    private String title;

    @Column(nullable = false)
    private String text;

    public Note() {

    }

    public Note(final long id, final String title, final String text) {
        this.id = id;
        this.title = title;
        this.text = text;
    }

    public Note(final User user, final String title, final String text) {
        this.user = user;
        this.title = title;
        this.text = text;
    }

    public User getUser() {
        return user;
    }

    public void setUser(final User user) {
        this.user = user;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(final String title) {
        this.title = title;
    }

    public String getText() {
        return text;
    }

    public void setText(final String text) {
        this.text = text;
    }

    public long getId() {
        return id;
    }

    public void setId(final long id) {
        this.id = id;
    }

}


User:
Code:
package ru.mrchebik.model;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
* Created by mrchebik on 22.07.16.
*/
@Entity
@Table(name = "Users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique = true, nullable = false)
    private long USER_ID;

    @Column(unique = true, nullable = false, length = 12)
    private String username;

    @Column(nullable = false, length = 16)
    private String password;

    @OneToMany(fetch = FetchType.LAZY, orphanRemoval = true)
    private Set<Note> notes = new HashSet<Note>(0);

    public User() {

    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(final String name) {
        this.username = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(final String password) {
        this.password = password;
    }

    public long getUSER_ID() {
        return USER_ID;
    }

    public void setUSER_ID(final long id) {
        this.USER_ID = id;
    }

    public Set<Note> getNotes() {
        return this.notes;
    }

    public void setNotes(Set<Note> notes) {
        this.notes = notes;
    }
}


NoteRepository:
Code:
package ru.mrchebik.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;
import ru.mrchebik.model.Note;

import java.util.List;

/**
* Created by mrchebik on 07.08.16.
*/
public interface NoteRepository extends JpaRepository<Note, Long> {
    List<Note> findByUser(@Param("USER_ID") long id);
}


Database DDL:
Code:
CREATE TABLE Users
(
    USER_ID BIGINT(20) PRIMARY KEY NOT NULL AUTO_INCREMENT,
    username VARCHAR(12) NOT NULL,
    password VARCHAR(16) NOT NULL
);
CREATE UNIQUE INDEX username ON Users (username);
CREATE TABLE Notes
(
    id BIGINT(20) PRIMARY KEY NOT NULL AUTO_INCREMENT,
    USER_ID BIGINT(20) NOT NULL,
    title VARCHAR(25) NOT NULL,
    text VARCHAR(255) NOT NULL,
    CONSTRAINT FK47F5EA160804239 FOREIGN KEY (USER_ID) REFERENCES Users (USER_ID)
);
CREATE INDEX FK47F5EA160804239 ON Notes (USER_ID);


Thanks.


Top
 Profile  
 
 Post subject: Re: JpaSystemException - Error accessing field
PostPosted: Fri Dec 02, 2016 2:56 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Try changing the @Id type from long primitive to Long wrapper.


Top
 Profile  
 
 Post subject: Re: JpaSystemException - Error accessing field
PostPosted: Fri Dec 02, 2016 4:13 pm 
Newbie

Joined: Sat Nov 26, 2016 3:20 am
Posts: 10
I get the same effect.


Top
 Profile  
 
 Post subject: Re: JpaSystemException - Error accessing field
PostPosted: Fri Dec 02, 2016 6:48 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Try renaming the USER_ID @Id field better and setter name to userId which is more Java Bean friendly. Maybe it's a problem with the getter to field resolution mechanism


Top
 Profile  
 
 Post subject: Re: JpaSystemException - Error accessing field
PostPosted: Sat Dec 03, 2016 11:46 am 
Newbie

Joined: Sat Nov 26, 2016 3:20 am
Posts: 10
Thank you, when I renamed the field, got the same result. But I decided to look at the query and see the extra parameters. I decided to make the request itself, it all fits, but there is a error: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to ru.mrchebik.model.Note

It's my query, then I catch ...cannot be cast...:
Code:
@Query("select note.title, note.text from ru.mrchebik.model.Note note where note.user.userId = :userId")


It's a "auto" query, then I catch error accessing field...:
Code:
select note0_.id as id1_0_, note0_.text as text2_0_, note0_.title as title3_0_, note0_.userId as userId4_0_ from Notes note0_ left outer join Users user1_ on note0_.userId=user1_.userId where user1_.userId=?


--
I know this error, and I can to fix it if I use DAO, but I use Spring Data, i.e:
Code:
@Override
    public List<Note> findNotes(final long userId) {
        return commandFactory.transaction(() -> {
            Query query = getSession().createQuery("select N.id, N.title, N.text from ru.mrchebik.model.Note N where USER_ID = :USER_ID").setLong("USER_ID", userId);
            List<Note> note = new ArrayList<Note>();
            Iterator itr = query.list().iterator();
            while (itr.hasNext()) {
                Object[] objects = (Object[]) itr.next();

                long id = Long.parseLong(String.valueOf(objects[0]));
                String title = String.valueOf(objects[1]);
                String text = String.valueOf(objects[2]);

                note.add(new Note(id, null, title, text));
            }
            return note;
        });
    }


Top
 Profile  
 
 Post subject: Re: JpaSystemException - Error accessing field
PostPosted: Sat Dec 03, 2016 12:24 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Chek out this StackOverflow answer.


Top
 Profile  
 
 Post subject: Re: JpaSystemException - Error accessing field
PostPosted: Sat Dec 03, 2016 2:46 pm 
Newbie

Joined: Sat Nov 26, 2016 3:20 am
Posts: 10
Thank you, good man!

The problem remains the same, I don't understand why these exceptions. It's hard to be motivated to program after such exceptions.

UPD: OMG! I accidentally deleted the request, almost not noticed, returned and it worked!
Thank you!!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.