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] : 1Full stack of issue:
http://pastebin.com/Av8uirGhHow can you see, I have and this exception:
IllegalArgumentException: Can not set long field ru.mrchebik.model.User.USER_ID to java.lang.LongAnd 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.