0

I have a simple entity Configuration, which has an ID and a String property.

@Data
@AllArgsConstructor
@NoArgsConstructor
@Wither
@Entity
@Table(name = "CONFIGURATION")
public class Configuration {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;

    @Column(name = "CONFIG_OBJECT")
    private String configItem1;
}

I have simple JPA repository

public interface ConfigurationRepository extends JpaRepository<Configuration, Long> {

}

And in a Cucumber test I try to access the field

final Configuration configuration = configurationRepository.getOne(1L);//id 1 exists
configuration.getConfigItem1(); //the exception is thrown here

But I'm getting this exception could not initialize proxy [com.mk.myapp.model.Configuration#1] - no Session

I assume, that the property configItem1 is being lazily loaded, but why? I would expect this when I have some entitites in a relation, but why should a String property be lazily loaded?

0

1 Answer 1

1

The method you used to retreive your Configuration entity getOne(1) is always returning a proxy with uninitialzed fields. Only the id field is populated afaik.

Try the findbyId method of your repository to get your Configuration and you will find your String field populated.

This question has also been answered before e.g. When use getOne and findOne methods Spring Data JPA so I think this can be closed as a duplicate.

Not the answer you're looking for? Browse other questions tagged or ask your own question.