-->
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.  [ 2 posts ] 
Author Message
 Post subject: Unable to retrieve user list using spring data and hibernate
PostPosted: Sun Dec 10, 2017 7:55 am 
Newbie

Joined: Sun Dec 10, 2017 7:52 am
Posts: 1
To begin with below is the schema i have

Code:
CREATE TABLE sample.users (
    id                   varchar(40)  NOT NULL,
    user_id              varchar(12)  NOT NULL,
    passwd               varchar(200)  ,
    creation_time        timestamp DEFAULT current_timestamp NOT NULL,
    CONSTRAINT pk_users_id PRIMARY KEY ( id ),
    CONSTRAINT user_id_unique UNIQUE ( user_id )
);

CREATE TABLE sample.ban_details (
id                   bigint  NOT NULL,
user_id              varchar(12)  NOT NULL,
ban_no               varchar(12)  NOT NULL,
creation_time        timestamp DEFAULT current_timestamp NOT NULL,
CONSTRAINT pk_user_pan_details_id PRIMARY KEY ( id ),
CONSTRAINT user_only_one_ban UNIQUE ( user_id ) ,
CONSTRAINT ban_no_unique UNIQUE ( pan_no )
);

ALTER TABLE sample.user_ban_details ADD CONSTRAINT fk_user_ban_details_users
FOREIGN KEY ( user_id ) REFERENCES sample.users( user_id );


My Entity Classes are

User.java

Code:
import java.io.Serializable;
import java.time.ZonedDateTime;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedDate;

@Entity
@Table(name = "users")
public class User implements Serializable {

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name="system-uuid",  strategy = "uuid")
    @Column(name = "id")
    private String id;

    @Column(name = "user_id", unique = true, length = 12, nullable = false)
    private String userId; //should make it case insensitive

    @Column(name = "passwd")
    private String password;

    @CreatedDate
    @Column(name = "creation_time")
    @Convert(converter = ZonedDateTimeAttributeConverter.class)
    private ZonedDateTime createdDate = ZonedDateTime.now();

    @OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
    private UserBANDetails banDetails;

    public String getId() {
        return id;
    }

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

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPassword() {
        return password;
    }

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

    public ZonedDateTime getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(ZonedDateTime createdDate) {
        this.createdDate = createdDate;
    }

    public UserBANDetails getBanDetails() {
        return banDetails;
    }

    public void setBanDetails(UserBANDetails banDetails) {
        this.banDetails = banDetails;
    }
}


Mapped Entity

UserBANDetails.java

Code:
import java.time.ZonedDateTime;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.springframework.data.annotation.CreatedDate;

@Entity
@Table(name = "user_ban_details")
public class UserBANDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ban_id_sequence")
    @SequenceGenerator(name = "ban_id_sequence", sequenceName = "ban_id_sequence")
    @Column(name = "id")
    private Long id;

    @Column(name = "ban_no", unique = true, nullable = false)
    private String banNumber;

    @CreatedDate
    @Column(name = "creation_time")
    @Convert(converter = ZonedDateTimeAttributeConverter.class)
    private ZonedDateTime createdDate = ZonedDateTime.now();

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id", referencedColumnName = "user_id")
    private User user;

    public Long getId() {
        return id;
    }

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

    public String getBanNumber() {
        return banNumber;
    }

    public void setBanNumber(String banNumber) {
        this.banNumber = banNumber;
    }

    public ZonedDateTime getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(ZonedDateTime createdDate) {
        this.createdDate = createdDate;
    }

    public User getUser() {
        return user;
    }

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


The user and userbandetails entities have one to one mapping and I created the spring data jpa repository for User entity.

Code:
public interface UserRepository extends JpaRepository<User, String>, JpaSpecificationExecutor<User> {

}


I am using spring boot 1.5.9 version which uses hibernate 5.0.12 version. I have populated the tables user and userbandetails with the data. But when ever I am running findall method on the userrepository I am getting the below error

Sample Code:

Code:
@Autowired
private UserRepository userRepository;

public void run() {
userRepository.findAll();
}


Quote:
Caused by: org.springframework.orm.jpa.JpaSystemException: Error accessing field [private java.lang.String com.samples.ups.domain.models.User.userId] by reflection for persistent property [com.samples.ups.domain.models.User#userId] : 40288182602db9a301602db9e7610000; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.String com.samples.ups.domain.models.User.userId] by reflection for persistent property [com.samples.ups.domain.models.User#userId] : 40288182602db9a301602db9e7610000 at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:333) ~[spring-orm-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244) ~[spring-orm-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:503) ~[spring-orm-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:209) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133) ~[spring-data-jpa-1.11.9.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57) ~[spring-data-commons-1.13.9.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE] at com.sun.proxy.$Proxy127.findAll(Unknown Source) ~[na:na] at com.samples.UserProfileMicroServiceMain.run(UserProfileMicroServiceMain.java:111) [classes/:na] at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE] ... 6 common frames omitted Caused by: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.String com.samples.ups.domain.models.User.userId] by reflection for persistent property [com.samples.ups.domain.models.User#userId] : 40288182602db9a301602db9e7610000 at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:43) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValue(AbstractComponentTuplizer.java:58) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.type.ComponentType.getPropertyValue(ComponentType.java:419) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:242) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.engine.spi.EntityUniqueKey.generateHashCode(EntityUniqueKey.java:67) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.engine.spi.EntityUniqueKey.(EntityUniqueKey.java:48) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:686) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.type.EntityType.resolve(EntityType.java:434) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:154) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:128) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1133) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.loader.Loader.processResultSet(Loader.java:992) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.loader.Loader.doQuery(Loader.java:930) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.loader.Loader.doList(Loader.java:2617) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.loader.Loader.doList(Loader.java:2600) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2429) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.loader.Loader.list(Loader.java:2424) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1326) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:483) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:50) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:329) ~[spring-data-jpa-1.11.9.RELEASE.jar:na] at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:74) ~[spring-data-jpa-1.11.9.RELEASE.jar:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151] at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_151] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:520) ~[spring-data-commons-1.13.9.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:505) ~[spring-data-commons-1.13.9.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:477) ~[spring-data-commons-1.13.9.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56) ~[spring-data-commons-1.13.9.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE] ... 17 common frames omitted Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field com.samples.ups.domain.models.User.userId to java.lang.String at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source) ~[na:1.8.0_151] at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source) ~[na:1.8.0_151] at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source) ~[na:1.8.0_151] at sun.reflect.UnsafeObjectFieldAccessorImpl.get(Unknown Source) ~[na:1.8.0_151] at java.lang.reflect.Field.get(Unknown Source) ~[na:1.8.0_151] at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:39) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] ... 59 common frames omitted


I am really not having any clue why this issue is happening. Any pointers in solving the issue would be welcome.


Top
 Profile  
 
 Post subject: Re: Unable to retrieve user list using spring data and hibernate
PostPosted: Sun Dec 10, 2017 1:40 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
Quote:
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field com.samples.ups.domain.models.User.userId


Try debugging where this exception is thrown because it's not clear from the mapping what it might be wrong.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.