1
votes

I am using Spring Data JPA:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>1.7.1.RELEASE</version>
</dependency>

with Spring 4.3.7.RELEASE and Hibernate 5.2.9.Final.

When I query using findAll, the List returns contains null values.

Entity:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@Entity
public class Etudiant implements Serializable {

    /**
     * Serial version UID
     */
    private static final long serialVersionUID = -1982480763983112005L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "idEtudiant")
    private Integer idEtudiant;

    @Column(name = "nomEtudiant")
    private String nomEtudiant;

    @Column(name = "prenomEtudiant")
    private String prenomEtudiant;

    @Column(name = "adresse")
    private String adresse;

    @Column(name = "dateNaissance")
    private Date dateNaissance;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "etudiant_cours", joinColumns = @JoinColumn(name = "idEtudiant", referencedColumnName = "idEtudiant"), inverseJoinColumns = 
    @JoinColumn(name = "idCours", referencedColumnName = "idCours"))
    private List<Cours> cours;
}

NB: Note that the problem is not from lombock, I had tested with getters and setters.

Repository:

@Repository
public interface EtudiantRepository extends JpaRepository<Etudiant, Integer> {     
}

Service:

@Service
public class EtudiantServiceImpl {

    @Autowired
    EtudiantRepository etudiantRepository;

    List<Etudiant> lst = new ArrayList<Etudiant>();

    public List<Etudiant> getAllEtudiant() {
        lst =  this.etudiantRepository.findAll();
        return lst;
    }
}

Debug result

3
What is your @Entity class? (Etudiant) - Brian
Yes, Entity is Etudiant - hmzn
There is something missing or wrong in your question, but I can´t figure out what it is?!? Is the List null or does it contain null values? What for do You need the member lst in the Repository? Why don´t You show us the sourcecode for your Entity Etundiant? How do you start the debugging-session and when do You take the snapshot? - Tobias Otto
Question is updated. - hmzn
The title of the question is misleading in that it mentions that the repository returns null, whereas it is the repository instance itself that is null. - manish

3 Answers

2
votes

In debug screenshot, it can be seen that etudiantRepository is null. Maybe you are missing @EnableJpaRepositories annotation in your Configuration.

EtudiantServiceImpl is being instantiated using Dependency Injection or with the new keyword?

0
votes

you need to use context:component-scan annotation into xml configuration for scanning base package and repository package, you can find code below :

<jpa:repositories base-package="com.demo.test.repository" />
<context:component-scan annotation-config="true"
        base-package="com.demo.test" />

and if findall() return null value that means table dose not having data, it is normal behavior. and also check your datasource and entity manager connection

0
votes

Finally I found the solution :

change spring version from 4.3.7.RELEASE to 4.3.10.RELEASE